Fork pd_web, remove everything we don't need
This commit is contained in:
@@ -1,241 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fornaxian.tech/log"
|
||||
"fornaxian.tech/pixeldrain_api_client/pixelapi"
|
||||
"fornaxian.tech/util"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
blackfriday "github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
func browserCompat(ua string) bool {
|
||||
return strings.Contains(ua, "MSIE") || strings.Contains(ua, "Trident/7.0")
|
||||
}
|
||||
|
||||
type fileViewerData struct {
|
||||
Type string `json:"type"` // file or list
|
||||
APIResponse interface{} `json:"api_response"`
|
||||
CaptchaKey string `json:"captcha_key"`
|
||||
Embedded bool `json:"embedded"`
|
||||
UserAdsEnabled bool `json:"user_ads_enabled"`
|
||||
ThemeURI template.URL `json:"theme_uri"`
|
||||
}
|
||||
|
||||
func (vd *fileViewerData) themeOverride(r *http.Request, files []pixelapi.ListFile) {
|
||||
vd.ThemeURI = "/theme.css"
|
||||
var theme = r.URL.Query().Get("style")
|
||||
var hue = r.URL.Query().Get("hue")
|
||||
|
||||
if files[0].Branding != nil {
|
||||
if theme == "" {
|
||||
theme = files[0].Branding["theme"]
|
||||
}
|
||||
if hue == "" {
|
||||
hue = files[0].Branding["hue"]
|
||||
}
|
||||
}
|
||||
|
||||
if theme != "" {
|
||||
vd.ThemeURI += template.URL("?style=" + theme)
|
||||
if hue != "" {
|
||||
vd.ThemeURI += template.URL("&hue=" + hue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ServeFileViewer controller for GET /u/:id
|
||||
func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
// If the user agent is Wget we redirect it to the API so that the file can
|
||||
// be downloaded directly
|
||||
if strings.HasPrefix(r.UserAgent(), "Wget/") {
|
||||
http.Redirect(w, r, "/api/file/"+p.ByName("id"), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Prevent search engines from indexing this page for privacy reasons
|
||||
w.Header().Set("X-Robots-Tag", "noindex, nofollow")
|
||||
|
||||
var err error
|
||||
var ids = strings.Split(p.ByName("id"), ",")
|
||||
var templateData = wc.newTemplateData(w, r)
|
||||
|
||||
var files []pixelapi.ListFile
|
||||
for _, id := range ids {
|
||||
inf, err := templateData.PixelAPI.GetFileInfo(id)
|
||||
if err != nil {
|
||||
if pixelapi.ErrIsServerError(err) {
|
||||
wc.templates.Run(w, r, "500", templateData)
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
files = append(files, pixelapi.ListFile{FileInfo: inf})
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
wc.templates.Run(w, r, "file_not_found", templateData)
|
||||
return
|
||||
}
|
||||
|
||||
if files[0].SkipFileViewer {
|
||||
http.Redirect(w, r, "/api/file/"+p.ByName("id"), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
templateData.OGData = wc.metadataFromFile(r, files[0].FileInfo)
|
||||
|
||||
var vd = fileViewerData{
|
||||
CaptchaKey: wc.captchaKey(),
|
||||
UserAdsEnabled: templateData.User.Subscription.ID == "",
|
||||
}
|
||||
|
||||
if len(ids) > 1 {
|
||||
templateData.Title = fmt.Sprintf("%d files on pixeldrain", len(files))
|
||||
vd.Type = "list"
|
||||
vd.APIResponse = pixelapi.ListInfo{
|
||||
Title: "Multiple files",
|
||||
DateCreated: time.Now(),
|
||||
Files: files,
|
||||
}
|
||||
} else {
|
||||
templateData.Title = fmt.Sprintf("%s ~ pixeldrain", files[0].Name)
|
||||
vd.Type = "file"
|
||||
vd.APIResponse = files[0].FileInfo
|
||||
}
|
||||
|
||||
if _, ok := r.URL.Query()["embed"]; ok {
|
||||
vd.Embedded = true
|
||||
}
|
||||
|
||||
vd.themeOverride(r, files)
|
||||
templateData.Other = vd
|
||||
|
||||
for _, file := range files {
|
||||
if file.AbuseType != "" {
|
||||
w.WriteHeader(http.StatusUnavailableForLegalReasons)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var templateName = "file_viewer_svelte"
|
||||
if browserCompat(r.UserAgent()) {
|
||||
templateName = "file_viewer_compat"
|
||||
}
|
||||
|
||||
err = wc.templates.Run(w, r, templateName, templateData)
|
||||
if err != nil && !util.IsNetError(err) {
|
||||
log.Error("Error executing template file_viewer: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
// If the user agent is Wget we redirect it to the API so that the file can
|
||||
// be downloaded directly
|
||||
if strings.HasPrefix(r.UserAgent(), "Wget/") {
|
||||
http.Redirect(w, r, "/api/list/"+p.ByName("id")+"/zip", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Prevent search engines from indexing this page for privacy reasons
|
||||
w.Header().Set("X-Robots-Tag", "noindex, nofollow")
|
||||
|
||||
var templateData = wc.newTemplateData(w, r)
|
||||
var list, err = templateData.PixelAPI.GetListID(p.ByName("id"))
|
||||
if err != nil {
|
||||
if apiErr, ok := err.(pixelapi.Error); ok && apiErr.Status == http.StatusNotFound {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
wc.templates.Run(w, r, "list_not_found", templateData)
|
||||
} else if strings.HasSuffix(err.Error(), "invalid control character in URL") {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
wc.templates.Run(w, r, "list_not_found", templateData)
|
||||
} else {
|
||||
log.Error("API request error occurred: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
wc.templates.Run(w, r, "500", templateData)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(list.Files) == 0 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
wc.templates.Run(w, r, "list_not_found", templateData)
|
||||
return
|
||||
}
|
||||
|
||||
templateData.Title = fmt.Sprintf("%s ~ pixeldrain", list.Title)
|
||||
templateData.OGData = wc.metadataFromList(r, list)
|
||||
var vd = fileViewerData{
|
||||
Type: "list",
|
||||
CaptchaKey: wc.captchaSiteKey,
|
||||
UserAdsEnabled: templateData.User.Subscription.ID == "",
|
||||
APIResponse: list,
|
||||
}
|
||||
|
||||
if _, ok := r.URL.Query()["embed"]; ok {
|
||||
vd.Embedded = true
|
||||
}
|
||||
|
||||
vd.themeOverride(r, list.Files)
|
||||
templateData.Other = vd
|
||||
|
||||
for _, file := range list.Files {
|
||||
if file.AbuseType != "" {
|
||||
w.WriteHeader(http.StatusUnavailableForLegalReasons)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var templateName = "file_viewer_svelte"
|
||||
if browserCompat(r.UserAgent()) {
|
||||
templateName = "file_viewer_compat"
|
||||
}
|
||||
|
||||
err = wc.templates.Run(w, r, templateName, templateData)
|
||||
if err != nil && !util.IsNetError(err) {
|
||||
log.Error("Error executing template file_viewer: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WebController) serveFilePreview(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
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)))
|
||||
}
|
||||
}
|
@@ -1,59 +0,0 @@
|
||||
package webcontroller
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"fornaxian.tech/log"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// ServeFileViewerDemo is a dummy API response that responds with info about a
|
||||
// non-existent demo file. This is required by the a-ads ad network to allow for
|
||||
// automatic checking of the presence of the ad unit on this page.
|
||||
func (wc *WebController) serveShareXConfig(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
templateData := wc.newTemplateData(w, r)
|
||||
|
||||
w.Header().Add("Content-Disposition", "attachment; filename=pixeldrain.com.sxcu")
|
||||
if templateData.Authenticated {
|
||||
sess, err := templateData.PixelAPI.PostUserSession("sharex")
|
||||
if err != nil {
|
||||
log.Error("Failed to create user session: %s", err)
|
||||
wc.templates.Run(w, r, "500", templateData)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w,
|
||||
`{
|
||||
"Version": "18.0.1",
|
||||
"DestinationType": "ImageUploader, TextUploader, FileUploader",
|
||||
"RequestMethod": "PUT",
|
||||
"RequestURL": "https://pixeldrain.com/api/file/{filename}",
|
||||
"Headers": {
|
||||
"Authorization": "Basic %s"
|
||||
},
|
||||
"Body": "Binary",
|
||||
"URL": "https://pixeldrain.com/u/{json:id}",
|
||||
"ThumbnailURL": "https://pixeldrain.com/api/file/{json:id}/thumbnail",
|
||||
"DeletionURL": "https://pixeldrain.com/u/{json:id}"
|
||||
}
|
||||
`,
|
||||
base64.StdEncoding.EncodeToString([]byte(
|
||||
templateData.User.Username+":"+sess.AuthKey.String(),
|
||||
)))
|
||||
} else {
|
||||
w.Write([]byte(
|
||||
`{
|
||||
"Version": "18.0.1",
|
||||
"DestinationType": "ImageUploader, TextUploader, FileUploader",
|
||||
"RequestMethod": "PUT",
|
||||
"RequestURL": "https://pixeldrain.com/api/file/{filename}",
|
||||
"Body": "Binary",
|
||||
"URL": "https://pixeldrain.com/u/{json:id}",
|
||||
"ThumbnailURL": "https://pixeldrain.com/api/file/{json:id}/thumbnail"
|
||||
}
|
||||
`,
|
||||
))
|
||||
}
|
||||
}
|
@@ -87,43 +87,6 @@ func getRequestAddress(r *http.Request) (addr string) {
|
||||
return "https://" + r.Host
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WebController) metadataFromFile(r *http.Request, f pixelapi.FileInfo) ogData {
|
||||
var addr = getRequestAddress(r)
|
||||
return generateOGData(
|
||||
f.Name,
|
||||
f.MimeType,
|
||||
addr+"/u/"+f.ID,
|
||||
addr+"/api/file/"+f.ID,
|
||||
addr+"/api/file/"+f.ID+"/thumbnail",
|
||||
defaultThemeColour,
|
||||
)
|
||||
}
|
||||
func (wc *WebController) metadataFromList(r *http.Request, l pixelapi.ListInfo) ogData {
|
||||
var addr = getRequestAddress(r)
|
||||
if l.FileCount > 0 {
|
||||
return generateOGData(
|
||||
l.Title,
|
||||
l.Files[0].MimeType,
|
||||
addr+"/l/"+l.ID,
|
||||
addr+"/api/file/"+l.Files[0].ID,
|
||||
addr+"/api/file/"+l.Files[0].ID+"/thumbnail",
|
||||
defaultThemeColour,
|
||||
)
|
||||
}
|
||||
|
||||
var og = ogData{}
|
||||
og.addProp("og:type", "website")
|
||||
og.addProp("og:title", l.Title)
|
||||
og.addProp("og:site_name", "pixeldrain")
|
||||
og.addProp("og:description", "A collection of files on pixeldrain")
|
||||
og.addProp("description", "A collection of files on pixeldrain")
|
||||
og.addName("description", "A collection of files on pixeldrain")
|
||||
og.addProp("og:url", addr+"/l/"+l.ID)
|
||||
og.addName("twitter:title", l.Title)
|
||||
return og
|
||||
}
|
||||
|
||||
func (wc *WebController) metadataFromFilesystem(r *http.Request, f pixelapi.FilesystemPath) (og ogData) {
|
||||
var addr = getRequestAddress(r)
|
||||
var base = &f.Path[f.BaseIndex]
|
||||
|
@@ -39,9 +39,6 @@ type WebController struct {
|
||||
// Server hostname, displayed in the footer of every web page
|
||||
hostname string
|
||||
|
||||
// page-specific variables
|
||||
captchaSiteKey string
|
||||
|
||||
httpClient *http.Client
|
||||
|
||||
// API client to use for all requests. If the user is authenticated you
|
||||
@@ -135,13 +132,7 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
|
||||
{GET, "" /* */, wc.serveLandingPage()},
|
||||
{GET, "home" /* */, wc.serveTemplate("home", handlerOpts{})},
|
||||
{GET, "api" /* */, wc.serveMarkdown("api.md", handlerOpts{})},
|
||||
{GET, "history" /* */, wc.serveTemplate("upload_history", handlerOpts{})},
|
||||
{GET, "u/:id" /* */, wc.serveFileViewer},
|
||||
{GET, "u/:id/preview" /* */, wc.serveFilePreview},
|
||||
{GET, "l/:id" /* */, wc.serveListViewer},
|
||||
{GET, "d/*path" /* */, wc.serveDirectory},
|
||||
{GET, "t" /* */, wc.serveTemplate("text_upload", handlerOpts{})},
|
||||
{GET, "donation" /* */, wc.serveMarkdown("donation.md", handlerOpts{})},
|
||||
{GET, "widgets" /* */, wc.serveTemplate("widgets", handlerOpts{})},
|
||||
{GET, "about" /* */, wc.serveMarkdown("about.md", handlerOpts{})},
|
||||
{GET, "appearance" /* */, wc.serveTemplate("appearance", handlerOpts{})},
|
||||
@@ -170,7 +161,6 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
|
||||
{GET, "admin/*p", wc.serveTemplate("admin", handlerOpts{Auth: true})},
|
||||
|
||||
// Misc
|
||||
{GET, "misc/sharex/pixeldrain.com.sxcu", wc.serveShareXConfig},
|
||||
{GET, "theme.css", wc.themeHandler},
|
||||
} {
|
||||
r.Handle(h.method, prefix+"/"+h.path, middleware(h.handler))
|
||||
@@ -187,10 +177,10 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
|
||||
func middleware(handle httprouter.Handle) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
// Redirect the user to the correct domain
|
||||
if strings.HasPrefix(r.Host, "www.") {
|
||||
if hostname, found := strings.CutPrefix(r.Host, "www."); found {
|
||||
http.Redirect(
|
||||
w, r,
|
||||
"https://"+strings.TrimPrefix(r.Host, "www.")+r.URL.String(),
|
||||
"https://"+hostname+r.URL.String(),
|
||||
http.StatusMovedPermanently,
|
||||
)
|
||||
return
|
||||
@@ -348,21 +338,3 @@ func (wc *WebController) getAPIKey(r *http.Request) (key string, err error) {
|
||||
}
|
||||
return "", errors.New("not a valid pixeldrain authentication cookie")
|
||||
}
|
||||
|
||||
func (wc *WebController) captchaKey() string {
|
||||
// This only runs on the first request
|
||||
if wc.captchaSiteKey == "" {
|
||||
capt, err := wc.api.GetMiscRecaptcha()
|
||||
if err != nil {
|
||||
log.Error("Error getting recaptcha key: %s", err)
|
||||
return ""
|
||||
}
|
||||
if capt.SiteKey == "" {
|
||||
wc.captchaSiteKey = "none"
|
||||
} else {
|
||||
wc.captchaSiteKey = capt.SiteKey
|
||||
}
|
||||
}
|
||||
|
||||
return wc.captchaSiteKey
|
||||
}
|
||||
|
Reference in New Issue
Block a user