filebrowser/http/preview.go

153 lines
4.0 KiB
Go
Raw Normal View History

//go:generate go-enum --sql --marshal --names --file $GOFILE
package http
import (
2020-07-27 17:01:02 +00:00
"bytes"
2020-07-23 00:41:19 +00:00
"context"
"fmt"
2020-07-23 00:41:19 +00:00
"io"
"net/http"
"github.com/gorilla/mux"
"github.com/filebrowser/filebrowser/v2/files"
2020-07-23 00:41:19 +00:00
"github.com/filebrowser/filebrowser/v2/img"
)
/*
ENUM(
thumb
big
)
*/
type PreviewSize int
2020-07-23 00:41:19 +00:00
type ImgService interface {
FormatFromExtension(ext string) (img.Format, error)
2020-07-24 18:08:26 +00:00
Resize(ctx context.Context, in io.Reader, width, height int, out io.Writer, options ...img.Option) error
2020-07-23 00:41:19 +00:00
}
2020-07-27 17:01:02 +00:00
type FileCache interface {
Store(ctx context.Context, key string, value []byte) error
Load(ctx context.Context, key string) ([]byte, bool, error)
Delete(ctx context.Context, key string) error
2020-07-27 17:01:02 +00:00
}
func previewHandler(imgSvc ImgService, fileCache FileCache, enableThumbnails, resizePreview bool) handleFunc {
2020-07-23 00:41:19 +00:00
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if !d.user.Perm.Download {
return http.StatusAccepted, nil
}
vars := mux.Vars(r)
previewSize, err := ParsePreviewSize(vars["size"])
if err != nil {
return http.StatusBadRequest, err
2020-07-23 00:41:19 +00:00
}
2020-07-23 00:41:19 +00:00
file, err := files.NewFileInfo(files.FileOptions{
Fs: d.user.Fs,
Path: "/" + vars["path"],
Modify: d.user.Perm.Modify,
Expand: true,
ReadHeader: d.server.TypeDetectionByHeader,
Checker: d,
2020-07-23 00:41:19 +00:00
})
if err != nil {
return errToStatus(err), err
}
2020-07-23 00:41:19 +00:00
setContentDisposition(w, r, file)
2020-07-23 00:41:19 +00:00
switch file.Type {
case "image":
return handleImagePreview(w, r, imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview)
2020-07-23 00:41:19 +00:00
default:
return http.StatusNotImplemented, fmt.Errorf("can't create preview for %s type", file.Type)
}
})
}
2020-07-27 17:01:02 +00:00
func handleImagePreview(w http.ResponseWriter, r *http.Request, imgSvc ImgService, fileCache FileCache,
file *files.FileInfo, previewSize PreviewSize, enableThumbnails, resizePreview bool) (int, error) {
2020-07-23 00:41:19 +00:00
format, err := imgSvc.FormatFromExtension(file.Extension)
2021-04-19 13:16:48 +00:00
// Unsupported extensions directly return the raw data
if err == img.ErrUnsupportedFormat || format == img.FormatGif {
return rawFileHandler(w, r, file)
}
if err != nil {
return errToStatus(err), err
}
2021-04-19 12:49:40 +00:00
isFresh := checkEtag(w, r, file.ModTime.Unix(), file.Size)
if isFresh {
return http.StatusNotModified, nil
}
2021-03-10 15:14:01 +00:00
cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize)
2020-07-27 17:01:02 +00:00
cachedFile, ok, err := fileCache.Load(r.Context(), cacheKey)
if err != nil {
return errToStatus(err), err
}
if ok {
_, _ = w.Write(cachedFile)
return 0, nil
}
2021-04-19 13:16:48 +00:00
resizedImage, err := createPreview(imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview)
if err != nil {
return errToStatus(err), err
}
2021-04-19 13:16:48 +00:00
_, _ = w.Write(resizedImage.Bytes())
return 0, nil
}
func createPreview(imgSvc ImgService, fileCache FileCache,
file *files.FileInfo, previewSize PreviewSize, enableThumbnails, resizePreview bool) (*bytes.Buffer, error) {
fd, err := file.Fs.Open(file.Path)
if err != nil {
return nil, err
}
defer fd.Close()
2020-07-23 00:41:19 +00:00
var (
width int
height int
options []img.Option
)
switch {
2021-04-19 13:16:48 +00:00
case previewSize == PreviewSizeBig && resizePreview:
2020-07-23 00:41:19 +00:00
width = 1080
height = 1080
options = append(options, img.WithMode(img.ResizeModeFit), img.WithQuality(img.QualityMedium))
case previewSize == PreviewSizeThumb && enableThumbnails:
2020-07-23 00:41:19 +00:00
width = 128
height = 128
options = append(options, img.WithMode(img.ResizeModeFill), img.WithQuality(img.QualityLow), img.WithFormat(img.FormatJpeg))
default:
2021-04-19 13:16:48 +00:00
return nil, img.ErrUnsupportedFormat
}
2020-07-27 17:01:02 +00:00
buf := &bytes.Buffer{}
if err := imgSvc.Resize(context.Background(), fd, width, height, buf, options...); err != nil {
2021-04-19 13:16:48 +00:00
return nil, err
}
2020-07-27 17:01:02 +00:00
go func() {
2021-04-19 13:16:48 +00:00
cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize)
2020-07-27 17:01:02 +00:00
if err := fileCache.Store(context.Background(), cacheKey, buf.Bytes()); err != nil {
fmt.Printf("failed to cache resized image: %v", err)
}
}()
2021-04-19 13:16:48 +00:00
return buf, nil
}
2021-03-10 15:14:01 +00:00
func previewCacheKey(fPath string, fTime int64, previewSize PreviewSize) string {
return fmt.Sprintf("%x%x%x", fPath, fTime, previewSize)
}