144 lines
4.5 KiB
Go
144 lines
4.5 KiB
Go
package webcontroller
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"fornaxian.tech/nova_server/api"
|
|
)
|
|
|
|
const defaultThemeColour = "#49dfa0"
|
|
const defaultHost = "https://nova.storage"
|
|
const siteDescription = "Cost-effective cloud storage and content delivery"
|
|
|
|
type ogData struct {
|
|
MetaPropRules []ogProp
|
|
MetaNameRules []ogProp
|
|
LinkRules []ogProp
|
|
}
|
|
|
|
type ogProp struct {
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
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}) }
|
|
|
|
func generateOGData(name, filetype, pageurl, fileurl, thumbnailurl, themecolour string) (og ogData) {
|
|
og.addProp("og:title", name)
|
|
og.addProp("og:site_name", "Nova.storage")
|
|
og.addProp("og:description", "This file has been shared with you on Nova")
|
|
og.addProp("og:url", pageurl)
|
|
og.addProp("description", "This file has been shared with you on Nova")
|
|
og.addName("description", "This file has been shared with you on Nova")
|
|
og.addName("keywords", "shared,sharing,upload,file")
|
|
og.addName("twitter:title", name)
|
|
og.addName("twitter:site", "@Fornax96")
|
|
og.addName("twitter:domain", "nova.storage")
|
|
og.addName("theme-color", themecolour)
|
|
|
|
if strings.HasPrefix(filetype, "image") {
|
|
og.addProp("og:type", "article")
|
|
og.addProp("og:image", fileurl)
|
|
og.addProp("og:image:url", fileurl)
|
|
og.addProp("og:image:secure_url", fileurl)
|
|
og.addProp("og:image:type", filetype)
|
|
|
|
og.addName("twitter:card", "summary_large_image")
|
|
og.addName("twitter:image", fileurl)
|
|
og.addLink("image_src", fileurl)
|
|
} else if strings.HasPrefix(filetype, "video") {
|
|
og.addProp("og:type", "video.other")
|
|
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)
|
|
|
|
og.addName("twitter:card", "player")
|
|
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") {
|
|
og.addProp("og:type", "music.song")
|
|
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)
|
|
} else {
|
|
og.addProp("og:type", "website")
|
|
og.addProp("og:image", thumbnailurl)
|
|
og.addLink("image_src", thumbnailurl)
|
|
}
|
|
return og
|
|
}
|
|
|
|
// siteOGData describes the site itself, for link previews of pages which are
|
|
// not a shared file
|
|
func siteOGData(r *http.Request) (og ogData) {
|
|
var addr = getRequestAddress(r)
|
|
og.addProp("og:type", "website")
|
|
og.addProp("og:site_name", "Nova.storage")
|
|
og.addProp("og:title", "Nova.storage")
|
|
og.addProp("og:description", siteDescription)
|
|
og.addProp("og:url", addr+r.URL.Path)
|
|
og.addProp("og:image", addr+"/res/img/nova_512.png")
|
|
og.addName("description", siteDescription)
|
|
og.addName("theme-color", defaultThemeColour)
|
|
og.addName("twitter:card", "summary")
|
|
return og
|
|
}
|
|
|
|
func getRequestAddress(r *http.Request) (addr string) {
|
|
if r.Host == "" {
|
|
return defaultHost
|
|
} else if r.TLS == nil {
|
|
return "http://" + r.Host
|
|
} else {
|
|
return "https://" + r.Host
|
|
}
|
|
}
|
|
func (wc *WebController) metadataFromFilesystem(r *http.Request, f api.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_accent_color"] != "" {
|
|
colour = node.Properties["brand_accent_color"]
|
|
}
|
|
}
|
|
|
|
// The services which make link previews don't keep cookies, so a password
|
|
// in the link has to be in every URL they open
|
|
var query, thumbnail = "", "?thumbnail"
|
|
if password := r.URL.Query().Get("password"); password != "" {
|
|
password = "password=" + url.QueryEscape(password)
|
|
query, thumbnail = "?"+password, thumbnail+"&"+password
|
|
}
|
|
|
|
return generateOGData(
|
|
base.Name,
|
|
base.FileType,
|
|
addr+"/d"+filepath+query,
|
|
addr+"/api/filesystem"+filepath+query,
|
|
addr+"/api/filesystem"+filepath+thumbnail,
|
|
colour,
|
|
)
|
|
}
|