Download and view graphs

This commit is contained in:
2019-07-07 01:03:44 +02:00
parent 1cd754aa53
commit 15bc3b7826
6 changed files with 124 additions and 1 deletions

View File

@@ -13,9 +13,12 @@ import (
"fornaxian.com/pixeldrain-api/util"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
"github.com/microcosm-cc/bluemonday"
"github.com/julienschmidt/httprouter"
"github.com/timakin/gonvert"
"gopkg.in/russross/blackfriday.v2"
)
// ServeFilePreview controller for GET /u/:id/preview
@@ -61,6 +64,9 @@ func (f filePreview) run(inf *pixelapi.FileInfo) string {
} else if strings.HasPrefix(f.FileInfo.MimeType, "audio") {
return f.audio()
} else if strings.HasPrefix(f.FileInfo.MimeType, "text") {
if strings.HasSuffix(f.FileInfo.Name, ".md") || strings.HasSuffix(f.FileInfo.Name, ".markdown") {
return f.markdown()
}
return f.text()
}
@@ -180,6 +186,37 @@ func (f filePreview) text() string {
return fmt.Sprintf(htmlOut, prettyPrint, result)
}
func (f filePreview) markdown() string {
htmlOut := `<div class="text-container">%s</div>`
if f.FileInfo.Size > 1e6 { // Prevent out of memory errors
return fmt.Sprintf(htmlOut, "",
"File is too large to view online.\nPlease download and view it locally.",
)
}
body, err := f.PixelAPI.GetFile(f.FileInfo.ID)
if err != nil {
log.Error("Can't download text file for preview: %s", err)
return fmt.Sprintf(htmlOut, "",
"An error occurred while downloading this file.",
)
}
defer body.Close()
bodyBytes, err := ioutil.ReadAll(body)
if err != nil {
log.Error("Can't read text file for preview: %s", err)
return fmt.Sprintf(htmlOut, "",
"An error occurred while reading this file.",
)
}
md := bluemonday.UGCPolicy().SanitizeBytes(blackfriday.Run(bodyBytes))
return fmt.Sprintf(htmlOut, md)
}
func (f filePreview) frame(url string) string {
return fmt.Sprintf(`<iframe src="%s" class="image-container"
seamless="seamless" frameborder="0" allowtransparency="true"