Files
fnx_web/webcontroller/opengraph.go

119 lines
3.6 KiB
Go
Raw Permalink Normal View History

2017-12-17 18:10:59 +01:00
package webcontroller
import (
2024-09-09 14:48:10 +02:00
"net/http"
"net/url"
2019-07-06 19:36:04 +02:00
"strings"
2021-03-10 20:13:32 +01:00
"fornaxian.tech/pixeldrain_api_client/pixelapi"
2017-12-17 18:10:59 +01:00
)
2024-09-09 14:48:10 +02:00
const defaultThemeColour = "#220735"
2024-09-09 14:51:12 +02:00
const defaultHost = "https://pixeldrain.com"
2024-09-09 14:48:10 +02:00
2020-12-07 22:23:29 +01:00
type ogData struct {
2021-04-20 16:36:58 +02:00
MetaPropRules []ogProp
MetaNameRules []ogProp
LinkRules []ogProp
2019-07-06 19:36:04 +02:00
}
2020-12-07 22:23:29 +01:00
type ogProp struct {
Key string
Value string
2019-07-06 19:36:04 +02:00
}
2021-04-20 16:36:58 +02:00
func (og *ogData) addProp(k, v string) { og.MetaPropRules = append(og.MetaPropRules, ogProp{k, v}) }
func (og *ogData) addName(k, v string) { og.MetaNameRules = append(og.MetaNameRules, ogProp{k, v}) }
func (og *ogData) addLink(k, v string) { og.LinkRules = append(og.LinkRules, ogProp{k, v}) }
2019-07-06 19:36:04 +02:00
2024-09-09 14:48:10 +02:00
func generateOGData(name, filetype, pageurl, fileurl, thumbnailurl, themecolour string) (og ogData) {
og.addProp("og:title", name)
2021-04-20 16:36:58 +02:00
og.addProp("og:site_name", "pixeldrain")
og.addProp("og:description", "This file has been shared with you on pixeldrain")
2024-09-09 14:48:10 +02:00
og.addProp("og:url", pageurl)
2021-04-20 16:36:58 +02:00
og.addProp("description", "This file has been shared with you on pixeldrain")
og.addName("description", "This file has been shared with you on pixeldrain")
og.addName("keywords", "pixeldrain,shared,sharing,upload,file,free")
2024-09-09 14:48:10 +02:00
og.addName("twitter:title", name)
2021-04-20 16:36:58 +02:00
og.addName("twitter:site", "@Fornax96")
og.addName("twitter:domain", "pixeldrain.com")
2024-09-09 14:48:10 +02:00
og.addName("theme-color", themecolour)
2019-07-06 19:36:04 +02:00
2024-09-09 14:48:10 +02:00
if strings.HasPrefix(filetype, "image") {
2021-04-20 16:36:58 +02:00
og.addProp("og:type", "article")
2024-09-09 14:48:10 +02:00
og.addProp("og:image", fileurl)
og.addProp("og:image:url", fileurl)
og.addProp("og:image:secure_url", fileurl)
og.addProp("og:image:type", filetype)
2019-07-06 19:36:04 +02:00
2021-04-20 16:36:58 +02:00
og.addName("twitter:card", "summary_large_image")
2024-09-09 14:48:10 +02:00
og.addName("twitter:image", fileurl)
og.addLink("image_src", fileurl)
} else if strings.HasPrefix(filetype, "video") {
2021-04-20 16:36:58 +02:00
og.addProp("og:type", "video.other")
2024-09-09 14:48:10 +02:00
og.addProp("og:image", thumbnailurl)
og.addProp("og:video", fileurl)
og.addProp("og:video:url", fileurl)
og.addProp("og:video:secure_url", fileurl)
og.addProp("og:video:type", filetype)
2019-07-06 19:36:04 +02:00
2021-04-20 16:36:58 +02:00
og.addName("twitter:card", "player")
2024-09-09 14:48:10 +02:00
og.addName("twitter:image", thumbnailurl)
og.addName("twitter:player", fileurl)
og.addName("twitter:player:stream", fileurl)
og.addName("twitter:player:stream:content_type", filetype)
og.addLink("image_src", thumbnailurl)
} else if strings.HasPrefix(filetype, "audio") {
2021-04-20 16:36:58 +02:00
og.addProp("og:type", "music.song")
2024-09-09 14:48:10 +02:00
og.addProp("og:image", thumbnailurl)
og.addProp("og:audio", fileurl)
og.addProp("og:audio:secure_url", fileurl)
og.addProp("og:audio:type", filetype)
og.addLink("image_src", thumbnailurl)
2019-07-06 19:36:04 +02:00
} else {
2021-04-20 16:36:58 +02:00
og.addProp("og:type", "website")
2024-09-09 14:48:10 +02:00
og.addProp("og:image", thumbnailurl)
og.addLink("image_src", thumbnailurl)
2019-07-06 19:36:04 +02:00
}
2020-12-07 22:23:29 +01:00
return og
2019-07-06 19:36:04 +02:00
}
2024-09-09 14:48:10 +02:00
func getRequestAddress(r *http.Request) (addr string) {
2024-09-09 14:51:12 +02:00
if r.Host == "" {
return defaultHost
} else if r.TLS == nil {
return "http://" + r.Host
2024-09-09 14:48:10 +02:00
} else {
2024-09-09 14:51:12 +02:00
return "https://" + r.Host
2024-09-09 14:48:10 +02:00
}
}
func (wc *WebController) metadataFromFilesystem(r *http.Request, f pixelapi.FilesystemPath) (og ogData) {
var addr = getRequestAddress(r)
var base = &f.Path[f.BaseIndex]
// Encode the file path
var split = strings.Split(base.Path, "/")
for i := range split {
split[i] = url.PathEscape(split[i])
}
var filepath = strings.Join(split, "/")
// Get the theme colour
var colour = defaultThemeColour
for _, node := range f.Path {
if node.Properties["branding_enabled"] == "true" &&
node.Properties["brand_highlight_color"] != "" {
colour = node.Properties["brand_highlight_color"]
}
}
return generateOGData(
base.Name,
base.FileType,
addr+"/d"+filepath,
addr+"/api/filesystem"+filepath,
addr+"/api/filesystem"+filepath+"?thumbnail",
colour,
)
}