2020-06-25 07:37:13 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-07-23 00:41:19 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2020-06-25 07:37:13 +00:00
|
|
|
"fmt"
|
2020-07-23 00:41:19 +00:00
|
|
|
"io"
|
2020-06-25 07:37:13 +00:00
|
|
|
"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"
|
2020-06-25 07:37:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sizeThumb = "thumb"
|
|
|
|
sizeBig = "big"
|
|
|
|
)
|
|
|
|
|
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-06-25 07:37:13 +00:00
|
|
|
|
2020-07-23 10:38:03 +00:00
|
|
|
func previewHandler(imgSvc ImgService, 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)
|
|
|
|
size := vars["size"]
|
|
|
|
if size != sizeBig && size != sizeThumb {
|
|
|
|
return http.StatusNotImplemented, nil
|
|
|
|
}
|
2020-06-25 07:37:13 +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,
|
|
|
|
Checker: d,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errToStatus(err), err
|
|
|
|
}
|
2020-06-25 07:37:13 +00:00
|
|
|
|
2020-07-23 00:41:19 +00:00
|
|
|
setContentDisposition(w, r, file)
|
2020-06-25 07:37:13 +00:00
|
|
|
|
2020-07-23 00:41:19 +00:00
|
|
|
switch file.Type {
|
|
|
|
case "image":
|
2020-07-23 10:38:03 +00:00
|
|
|
return handleImagePreview(imgSvc, w, r, file, size, 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-06-25 07:37:13 +00:00
|
|
|
|
2020-07-23 10:38:03 +00:00
|
|
|
func handleImagePreview(imgSvc ImgService, w http.ResponseWriter, r *http.Request,
|
|
|
|
file *files.FileInfo, size string, enableThumbnails, resizePreview bool) (int, error) {
|
2020-07-23 00:41:19 +00:00
|
|
|
format, err := imgSvc.FormatFromExtension(file.Extension)
|
2020-06-25 07:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
// Unsupported extensions directly return the raw data
|
2020-07-23 00:41:19 +00:00
|
|
|
if err == img.ErrUnsupportedFormat {
|
2020-06-25 07:37:13 +00:00
|
|
|
return rawFileHandler(w, r, file)
|
|
|
|
}
|
|
|
|
return errToStatus(err), err
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:47:11 +00:00
|
|
|
fd, err := file.Fs.Open(file.Path)
|
|
|
|
if err != nil {
|
|
|
|
return errToStatus(err), err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2020-07-23 00:41:19 +00:00
|
|
|
var (
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
options []img.Option
|
|
|
|
)
|
|
|
|
|
2020-07-23 10:38:03 +00:00
|
|
|
switch {
|
|
|
|
case size == sizeBig && resizePreview && format != img.FormatGif:
|
2020-07-23 00:41:19 +00:00
|
|
|
width = 1080
|
|
|
|
height = 1080
|
2020-07-23 10:38:03 +00:00
|
|
|
options = append(options, img.WithMode(img.ResizeModeFit), img.WithQuality(img.QualityMedium))
|
|
|
|
case size == sizeThumb && enableThumbnails:
|
2020-07-23 00:41:19 +00:00
|
|
|
width = 128
|
|
|
|
height = 128
|
2020-07-23 10:38:03 +00:00
|
|
|
options = append(options, img.WithMode(img.ResizeModeFill), img.WithQuality(img.QualityLow), img.WithFormat(img.FormatJpeg))
|
2020-06-25 07:37:13 +00:00
|
|
|
default:
|
2020-07-23 10:38:03 +00:00
|
|
|
if _, err := rawFileHandler(w, r, file); err != nil {
|
|
|
|
return errToStatus(err), err
|
|
|
|
}
|
|
|
|
return 0, nil
|
2020-06-25 07:37:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 00:41:19 +00:00
|
|
|
if err := imgSvc.Resize(r.Context(), fd, width, height, w, options...); err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, context.DeadlineExceeded), errors.Is(err, context.Canceled):
|
|
|
|
default:
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-06-25 07:37:13 +00:00
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|