Resore unused function which was not actually unused

This commit is contained in:
2023-05-31 00:26:51 +02:00
parent b2a5d42cdb
commit a0ee6ece1a
4 changed files with 49 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package webcontroller
import (
"fmt"
"html/template"
"io"
"net/http"
"strings"
"time"
@@ -11,6 +12,8 @@ import (
"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 (wc *WebController) viewTokenOrBust() (t string) {
@@ -202,3 +205,39 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request,
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)))
}
}

View File

@@ -138,6 +138,7 @@ func New(r *httprouter.Router, prefix string, conf Config) (wc *WebController) {
{GET, "api" /* */, wc.serveMarkdown("api.md", handlerOpts{})},
{GET, "history" /* */, wc.serveTemplate("history_cookies", 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{})},