some file preview fixes
This commit is contained in:
@@ -28,11 +28,6 @@ type FileInfo struct {
|
||||
}
|
||||
|
||||
// GetFileInfo gets the FileInfo from the pixeldrain API
|
||||
func (p *PixelAPI) GetFileInfo(id string, urlParam string) (resp *FileInfo, err error) {
|
||||
resp = &FileInfo{}
|
||||
err = p.jsonRequest("GET", p.apiEndpoint+"/file/"+id+"/info"+urlParam, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
func (p *PixelAPI) GetFileInfo(id string) (resp FileInfo, err error) {
|
||||
return resp, p.jsonRequest("GET", p.apiEndpoint+"/file/"+id+"/info", &resp)
|
||||
}
|
||||
|
@@ -22,64 +22,40 @@ func (wc *WebController) serveFilePreview(w http.ResponseWriter, r *http.Request
|
||||
serveFilePreviewDemo(w) // Required for a-ads.com quality check
|
||||
return
|
||||
}
|
||||
|
||||
apikey, _ := wc.getAPIKey(r)
|
||||
var api = pixelapi.New(wc.apiURLInternal, apikey)
|
||||
api := pixelapi.New(wc.apiURLInternal, apikey)
|
||||
api.RealIP = util.RemoteAddress(r)
|
||||
inf, err := api.GetFileInfo(p.ByName("id"), "?should_a_view_be_added=yes_gimme") // TODO: Error handling
|
||||
file, err := api.GetFileInfo(p.ByName("id")) // TODO: Error handling
|
||||
if err != nil {
|
||||
wc.serveNotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var fp = filePreview{
|
||||
APIURL: wc.apiURLExternal,
|
||||
PixelAPI: api,
|
||||
}
|
||||
io.WriteString(w, fp.run(inf))
|
||||
if strings.HasPrefix(file.MimeType, "text") &&
|
||||
(strings.HasSuffix(file.Name, ".md") || strings.HasSuffix(file.Name, ".markdown")) {
|
||||
if file.Size > 1e6 { // Prevent out of memory errors
|
||||
w.Write([]byte("File is too large to view online.\nPlease download and view it locally."))
|
||||
return
|
||||
}
|
||||
|
||||
type filePreview struct {
|
||||
FileInfo *pixelapi.FileInfo
|
||||
FileURL string
|
||||
DownloadURL string
|
||||
|
||||
APIURL string
|
||||
PixelAPI *pixelapi.PixelAPI
|
||||
}
|
||||
|
||||
func (f filePreview) run(inf *pixelapi.FileInfo) string {
|
||||
f.FileInfo = inf
|
||||
f.FileURL = f.APIURL + "/file/" + f.FileInfo.ID
|
||||
f.DownloadURL = f.APIURL + "/file/" + f.FileInfo.ID + "?download"
|
||||
|
||||
if strings.HasPrefix(f.FileInfo.MimeType, "text") &&
|
||||
(strings.HasSuffix(f.FileInfo.Name, ".md") || strings.HasSuffix(f.FileInfo.Name, ".markdown")) {
|
||||
return f.markdown()
|
||||
}
|
||||
|
||||
// none of the mime type checks triggered, so we return the default page
|
||||
return ""
|
||||
}
|
||||
|
||||
func (f filePreview) markdown() string {
|
||||
if f.FileInfo.Size > 1e6 { // Prevent out of memory errors
|
||||
return "File is too large to view online.\nPlease download and view it locally."
|
||||
}
|
||||
|
||||
body, err := f.PixelAPI.GetFile(f.FileInfo.ID)
|
||||
body, err := api.GetFile(file.ID)
|
||||
if err != nil {
|
||||
log.Error("Can't download text file for preview: %s", err)
|
||||
return "An error occurred while downloading this file."
|
||||
w.Write([]byte("An error occurred while downloading this file."))
|
||||
return
|
||||
}
|
||||
defer body.Close()
|
||||
|
||||
bodyBytes, err := ioutil.ReadAll(body)
|
||||
if err != nil {
|
||||
log.Error("Can't read text file for preview: %s", err)
|
||||
return "An error occurred while reading this file."
|
||||
w.Write([]byte("An error occurred while reading this file."))
|
||||
return
|
||||
}
|
||||
|
||||
return string(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.Run(bodyBytes)))
|
||||
w.Write(bluemonday.UGCPolicy().SanitizeBytes(blackfriday.Run(bodyBytes)))
|
||||
}
|
||||
}
|
||||
|
||||
// ServeFilePreviewDemo serves the content of the demo file. It contains a nice
|
||||
|
@@ -44,9 +44,9 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
|
||||
|
||||
templateData := wc.newTemplateData(w, r)
|
||||
|
||||
var finfo []*pixelapi.FileInfo
|
||||
var finfo []pixelapi.FileInfo
|
||||
for _, id := range ids {
|
||||
inf, err := templateData.PixelAPI.GetFileInfo(id, "")
|
||||
inf, err := templateData.PixelAPI.GetFileInfo(id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
|
||||
return
|
||||
}
|
||||
|
||||
templateData.OGData = metadataFromFile(*finfo[0])
|
||||
templateData.OGData = metadataFromFile(finfo[0])
|
||||
if list {
|
||||
templateData.Title = fmt.Sprintf("%d files on pixeldrain", len(finfo))
|
||||
templateData.Other = viewerData{
|
||||
|
Reference in New Issue
Block a user