refactor: Extract "image" as FileType enum

- Extracted "image" string occurrences into a FileType enum to resolve goconst warnings
This commit is contained in:
kissudad@outlook.com 2024-04-10 16:53:08 +08:00 committed by llxxxdd
parent 3de1c4f0ca
commit bb8c4b7884
4 changed files with 39 additions and 13 deletions

View File

@ -27,14 +27,29 @@ import (
"github.com/filebrowser/filebrowser/v2/rules"
)
const PermFile = 0644
const PermDir = 0755
const (
PermFile = 0o644
PermDir = 0o755
)
var (
reSubDirs = regexp.MustCompile("(?i)^sub(s|titles)$")
reSubExts = regexp.MustCompile("(?i)(.vtt|.srt|.ass|.ssa)$")
)
type FileType string
const (
Blob FileType = "blob"
Video FileType = "video"
Audio FileType = "audio"
Image FileType = "image"
PDF FileType = "pdf"
Text FileType = "text"
TextImmutable FileType = "textImmutable"
InvalidLink FileType = "invalid_link"
)
// FileInfo describes a file.
type FileInfo struct {
*Listing
@ -47,7 +62,7 @@ type FileInfo struct {
Mode os.FileMode `json:"mode"`
IsDir bool `json:"isDir"`
IsSymlink bool `json:"isSymlink"`
Type string `json:"type"`
Type FileType `json:"type"`
Subtitles []string `json:"subtitles,omitempty"`
Content string `json:"content,omitempty"`
Checksums map[string]string `json:"checksums,omitempty"`
@ -241,14 +256,14 @@ func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error {
switch {
case strings.HasPrefix(mimetype, "video"):
i.Type = "video"
i.Type = Video
i.detectSubtitles()
return nil
case strings.HasPrefix(mimetype, "audio"):
i.Type = "audio"
i.Type = Audio
return nil
case strings.HasPrefix(mimetype, "image"):
i.Type = "image"
i.Type = Image
resolution, err := calculateImageResolution(i.Fs, i.Path)
if err != nil {
log.Printf("Error calculating image resolution: %v", err)
@ -257,13 +272,13 @@ func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error {
}
return nil
case strings.HasSuffix(mimetype, "pdf"):
i.Type = "pdf"
i.Type = PDF
return nil
case (strings.HasPrefix(mimetype, "text") || !isBinary(buffer)) && i.Size <= 10*1024*1024: // 10 MB
i.Type = "text"
i.Type = Text
if !modify {
i.Type = "textImmutable"
i.Type = TextImmutable
}
if saveContent {
@ -277,7 +292,7 @@ func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error {
}
return nil
default:
i.Type = "blob"
i.Type = Blob
}
return nil

View File

@ -91,7 +91,16 @@ func NewHandler(
public.PathPrefix("/dl").Handler(monkey(publicDlHandler, "/api/public/dl/")).Methods("GET")
public.PathPrefix("/share").Handler(monkey(publicShareHandler, "/api/public/share/")).Methods("GET")
public.PathPrefix("/preview/{size}/{path:.*}").
Handler(monkey(publicPreviewHandler(imgSvc, fileCache, server.EnableThumbnails, server.ResizePreview), "/api/public/preview")).Methods("GET")
Handler(
monkey(
publicPreviewHandler(
imgSvc,
fileCache,
server.EnableThumbnails,
server.ResizePreview,
),
"/api/public/preview"),
).Methods("GET")
return stripPrefix(server.BaseURL, r), nil
}

View File

@ -60,8 +60,9 @@ func previewHandler(imgSvc ImgService, fileCache FileCache, enableThumbnails, re
setContentDisposition(w, r, file)
//nolint:exhaustive //only Image type is handled
switch file.Type {
case "image":
case files.Image:
return handleImagePreview(w, r, imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview)
default:
return http.StatusNotImplemented, fmt.Errorf("can't create preview for %s type", file.Type)

View File

@ -138,8 +138,9 @@ func publicPreviewHandler(imgSvc ImgService, fileCache FileCache, enableThumbnai
file := d.raw.(*files.FileInfo)
setContentDisposition(w, r, file)
//nolint:exhaustive //only Image type is handled
switch file.Type {
case "image":
case files.Image:
return handleImagePreview(w, r, imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview)
default:
return http.StatusNotImplemented, fmt.Errorf("can't create preview for %s type", file.Type)