2020-11-04 20:51:39 +01:00
|
|
|
package webcontroller
|
|
|
|
|
|
|
|
import (
|
2020-11-11 00:00:54 +01:00
|
|
|
"fmt"
|
2020-11-04 20:51:39 +01:00
|
|
|
"net/http"
|
2023-05-17 19:27:46 +02:00
|
|
|
"net/url"
|
2020-11-04 20:51:39 +01:00
|
|
|
"strings"
|
|
|
|
|
2021-11-16 15:20:15 +01:00
|
|
|
"fornaxian.tech/log"
|
2023-05-17 19:27:46 +02:00
|
|
|
"fornaxian.tech/pixeldrain_api_client/pixelapi"
|
2023-05-10 15:08:29 +02:00
|
|
|
"fornaxian.tech/util"
|
2020-11-04 20:51:39 +01:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
2020-11-11 00:00:54 +01:00
|
|
|
func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
var err error
|
|
|
|
var td = wc.newTemplateData(w, r)
|
|
|
|
var path = strings.TrimPrefix(p.ByName("path"), "/")
|
|
|
|
|
2022-06-21 14:00:03 +02:00
|
|
|
if path == "" {
|
|
|
|
wc.templates.Get().ExecuteTemplate(w, "404", td)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-11 00:00:54 +01:00
|
|
|
node, err := td.PixelAPI.GetFilesystemPath(path)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "not_found" || err.Error() == "path_not_found" {
|
2023-05-19 21:45:42 +02:00
|
|
|
wc.serveNotFound(w, r)
|
|
|
|
} else if err.Error() == "forbidden" {
|
|
|
|
wc.serveForbidden(w, r)
|
2023-04-19 00:04:06 +02:00
|
|
|
} else if err.Error() == "authentication_required" {
|
|
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
2024-05-17 16:32:53 +02:00
|
|
|
} else if err.Error() == "unavailable_for_legal_reasons" {
|
|
|
|
wc.serveUnavailableForLegalReasons(w, r)
|
2020-11-11 00:00:54 +01:00
|
|
|
} else {
|
|
|
|
log.Error("Failed to get path: %s", err)
|
|
|
|
wc.templates.Get().ExecuteTemplate(w, "500", td)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-11 23:44:50 +02:00
|
|
|
td.Title = fmt.Sprintf("%s ~ pixeldrain", node.Path[node.BaseIndex].Name)
|
2020-11-11 00:00:54 +01:00
|
|
|
td.Other = node
|
2023-05-17 19:27:46 +02:00
|
|
|
td.OGData = wc.metadataFromFilesystem(node)
|
2021-02-23 16:50:13 +01:00
|
|
|
err = wc.templates.Get().ExecuteTemplate(w, "filesystem", td)
|
2023-05-10 15:08:29 +02:00
|
|
|
if err != nil && !util.IsNetError(err) {
|
2020-11-11 00:00:54 +01:00
|
|
|
log.Error("Error executing template filesystem: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2023-05-17 19:27:46 +02:00
|
|
|
|
|
|
|
func (wc *WebController) metadataFromFilesystem(f pixelapi.FilesystemPath) (og ogData) {
|
|
|
|
var (
|
|
|
|
base = f.Path[f.BaseIndex]
|
|
|
|
name = base.Name
|
|
|
|
filetype = base.Type
|
|
|
|
filepath = url.PathEscape(f.Path[0].ID + 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")
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|