From bb8c4b78844aed4d60f315117adf60933d0484db Mon Sep 17 00:00:00 2001 From: "kissudad@outlook.com" Date: Wed, 10 Apr 2024 16:53:08 +0800 Subject: [PATCH] refactor: Extract "image" as FileType enum - Extracted "image" string occurrences into a FileType enum to resolve goconst warnings --- files/file.go | 35 +++++++++++++++++++++++++---------- http/http.go | 11 ++++++++++- http/preview.go | 3 ++- http/public.go | 3 ++- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/files/file.go b/files/file.go index 03b3a6f9..cfb6053e 100644 --- a/files/file.go +++ b/files/file.go @@ -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 diff --git a/http/http.go b/http/http.go index 9ed79b48..aee81f40 100644 --- a/http/http.go +++ b/http/http.go @@ -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 } diff --git a/http/preview.go b/http/preview.go index 2fbefa9b..80f768f4 100644 --- a/http/preview.go +++ b/http/preview.go @@ -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) diff --git a/http/public.go b/http/public.go index 964a9c03..c1dc2902 100644 --- a/http/public.go +++ b/http/public.go @@ -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)