Add oembed tags. Fix sharing and copy link button. Add text and file viewer

This commit is contained in:
2023-05-17 19:27:46 +02:00
parent 277637511c
commit bd9359de44
16 changed files with 459 additions and 192 deletions

View File

@@ -1,77 +0,0 @@
package webcontroller
import (
"io"
"net/http"
"strings"
"fornaxian.tech/log"
"fornaxian.tech/util"
"github.com/julienschmidt/httprouter"
"github.com/microcosm-cc/bluemonday"
blackfriday "github.com/russross/blackfriday/v2"
)
// ServeFilePreview controller for GET /u/:id/preview
func (wc *WebController) serveFilePreview(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
if p.ByName("id") == "demo" || p.ByName("id") == "adsplus" || p.ByName("id") == "pixfuture" {
serveFilePreviewDemo(w) // Required for a-ads.com quality check
return
}
apiKey, _ := wc.getAPIKey(r)
api := wc.api.Login(apiKey).RealIP(util.RemoteAddress(r)).RealAgent(r.UserAgent())
file, err := api.GetFileInfo(p.ByName("id")) // TODO: Error handling
if err != nil {
wc.serveNotFound(w, r)
return
}
if strings.HasPrefix(file.MimeType, "text") &&
(strings.HasSuffix(file.Name, ".md") || strings.HasSuffix(file.Name, ".markdown")) {
if file.Size > 1<<22 { // Prevent out of memory errors
w.Write([]byte("File is too large to view online.\nPlease download and view it locally."))
return
}
body, err := api.GetFile(file.ID)
if err != nil {
log.Error("Can't download text file for preview: %s", err)
w.Write([]byte("An error occurred while downloading this file."))
return
}
defer body.Close()
bodyBytes, err := io.ReadAll(body)
if err != nil {
log.Error("Can't read text file for preview: %s", err)
w.Write([]byte("An error occurred while reading this file."))
return
}
w.Write(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.Run(bodyBytes)))
}
}
// ServeFilePreviewDemo serves the content of the demo file. It contains a nice
// message to the human reviewers of the a-ads ad network so they can properly
// categorize the website.
func serveFilePreviewDemo(w http.ResponseWriter) {
io.WriteString(w,
`<pre style="line-height: 1em; white-space: pre-wrap; overflow: hidden;">
, __ _
/|/ \o | | | o
|___/ _ | | __| ,_ __, _ _
| | /\/ |/ |/ / | / | / | | / |/ |
| |_/ /\_/|__/|__/\_/|_/ |_/\_/|_/|_/ | |_/
This is a demonstration of pixeldrain's file viewer.
The website automatically detects what kind of file you requested and prepares a page for viewing it properly. This is what a text file would look like on pixeldrain. You can upload your own text file at pixeldrain.com/t.
Pixeldrain is a free service for sharing files with large or small groups of people. For more information visit the home page by pressing the home button on the toolbar at the left side of the screen.
Press the Details button or "i" for more info about pixeldrain's file viewer.
</pre>`)
}

View File

@@ -3,9 +3,11 @@ 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"
)
@@ -35,8 +37,70 @@ 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)
err = wc.templates.Get().ExecuteTemplate(w, "filesystem", td)
if err != nil && !util.IsNetError(err) {
log.Error("Error executing template filesystem: %s", err)
}
}
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
}

View File

@@ -137,7 +137,6 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
{GET, "" /* */, wc.serveTemplate("home", handlerOpts{})},
{GET, "api" /* */, wc.serveMarkdown("api.md", handlerOpts{})},
{GET, "history" /* */, wc.serveTemplate("history_cookies", handlerOpts{})},
{GET, "u/:id/preview" /* */, wc.serveFilePreview},
{GET, "u/:id" /* */, wc.serveFileViewer},
{GET, "l/:id" /* */, wc.serveListViewer},
{GET, "d/*path" /* */, wc.serveDirectory},