Simplify opengraph tag generation
This commit is contained in:
@@ -3,17 +3,13 @@ package webcontroller
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"fornaxian.tech/log"
|
||||
"fornaxian.tech/pixeldrain_api_client/pixelapi"
|
||||
"fornaxian.tech/util"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
const defaultThemeColour = "#220735"
|
||||
|
||||
func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
var err error
|
||||
var td = wc.newTemplateData(w, r)
|
||||
@@ -46,91 +42,9 @@ func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request,
|
||||
|
||||
td.Title = fmt.Sprintf("%s ~ pixeldrain", node.Path[node.BaseIndex].Name)
|
||||
td.Other = node
|
||||
td.OGData = wc.metadataFromFilesystem(node)
|
||||
td.OGData = wc.metadataFromFilesystem(r, node)
|
||||
err = wc.templates.Get().ExecuteTemplate(w, "filesystem", td)
|
||||
if err != nil && !util.IsNetError(err) {
|
||||
log.Error("Error executing template filesystem: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func urlEncodePath(path string) string {
|
||||
var split = strings.Split(path, "/")
|
||||
for i := range split {
|
||||
split[i] = url.PathEscape(split[i])
|
||||
}
|
||||
return strings.Join(split, "/")
|
||||
}
|
||||
|
||||
func findThemeColour(f pixelapi.FilesystemPath) string {
|
||||
// We walk the patch backward because lower entries can override the
|
||||
// properties of higher entries
|
||||
for i := len(f.Path) - 1; i >= 0; i-- {
|
||||
if f.Path[i].Properties["branding_enabled"] != "true" &&
|
||||
f.Path[i].Properties["brand_highlight_color"] != "" {
|
||||
return f.Path[i].Properties["brand_highlight_color"]
|
||||
}
|
||||
}
|
||||
return defaultThemeColour
|
||||
}
|
||||
|
||||
func (wc *WebController) metadataFromFilesystem(f pixelapi.FilesystemPath) (og ogData) {
|
||||
var (
|
||||
base = f.Path[f.BaseIndex]
|
||||
name = base.Name
|
||||
filetype = base.FileType
|
||||
filepath = urlEncodePath(base.Path)
|
||||
pageurl = wc.config.WebsiteAddress + "/d" + filepath
|
||||
fileurl = wc.config.WebsiteAddress + "/api/filesystem" + filepath
|
||||
thumbnailurl = wc.config.WebsiteAddress + "/api/filesystem" + filepath + "?thumbnail"
|
||||
)
|
||||
|
||||
og.addProp("og:title", name)
|
||||
og.addProp("og:site_name", "pixeldrain")
|
||||
og.addProp("og:description", "This file has been shared with you on pixeldrain")
|
||||
og.addProp("og:url", pageurl)
|
||||
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")
|
||||
og.addName("twitter:title", name)
|
||||
og.addName("twitter:site", "@Fornax96")
|
||||
og.addName("twitter:domain", "pixeldrain.com")
|
||||
og.addName("theme-color", findThemeColour(f))
|
||||
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user