From 26bdedb5a25460e986bd791306998dbcf4ba0b8a Mon Sep 17 00:00:00 2001 From: Seednode Date: Mon, 11 Sep 2023 19:38:38 -0500 Subject: [PATCH] Added UTF-8 validation for files when using --text --- cmd/root.go | 4 ++-- formats/audio.go | 3 +++ formats/images.go | 3 +++ formats/text.go | 20 ++++++++++++++++---- formats/types.go | 4 +++- formats/video.go | 3 +++ 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f807860..c2d70db 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - Version string = "0.68.0" + Version string = "0.69.0" ) var ( @@ -94,7 +94,7 @@ func init() { rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting") rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint") rootCmd.Flags().StringVar(&statisticsFile, "stats-file", "", "path to optional persistent stats file") - rootCmd.Flags().BoolVar(&text, "text", true, "enable support for text files") + rootCmd.Flags().BoolVar(&text, "text", false, "enable support for text files") rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "log accessed files to stdout") rootCmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit") rootCmd.Flags().BoolVar(&videos, "video", false, "enable support for video files") diff --git a/formats/audio.go b/formats/audio.go index 5ed2bdd..7b90d17 100644 --- a/formats/audio.go +++ b/formats/audio.go @@ -34,5 +34,8 @@ func RegisterAudioFormats() *SupportedFormat { `audio/ogg`, `audio/wav`, }, + Validate: func(filePath string) bool { + return true + }, } } diff --git a/formats/images.go b/formats/images.go index bed3713..0ef1931 100644 --- a/formats/images.go +++ b/formats/images.go @@ -67,6 +67,9 @@ func RegisterImageFormats() *SupportedFormat { `image/png`, `image/webp`, }, + Validate: func(filePath string) bool { + return true + }, } } diff --git a/formats/text.go b/formats/text.go index 6d9c474..64ee81e 100644 --- a/formats/text.go +++ b/formats/text.go @@ -5,6 +5,7 @@ Copyright © 2023 Seednode package formats import ( + "errors" "fmt" "os" "unicode/utf8" @@ -24,10 +25,6 @@ func RegisterTextFormats() *SupportedFormat { body = []byte{} } - if !utf8.Valid(body) { - body = []byte(`Unable to parse binary file as text.`) - } - return fmt.Sprintf(`
%s
`, queryParams, body) @@ -52,5 +49,20 @@ func RegisterTextFormats() *SupportedFormat { `text/plain`, `text/plain; charset=utf-8`, }, + Validate: func(path string) bool { + file, err := os.Open(path) + switch { + case errors.Is(err, os.ErrNotExist): + return false + case err != nil: + return false + } + defer file.Close() + + head := make([]byte, 512) + file.Read(head) + + return utf8.Valid(head) + }, } } diff --git a/formats/types.go b/formats/types.go index b09b659..579dcbf 100644 --- a/formats/types.go +++ b/formats/types.go @@ -11,6 +11,7 @@ import ( ) type FormatFunction func(queryParams, fileUri, filePath, fileName, mime string) string +type ValidateFunction func(filePath string) bool type SupportedFormat struct { Css FormatFunction @@ -18,6 +19,7 @@ type SupportedFormat struct { Body FormatFunction Extensions []string MimeTypes []string + Validate ValidateFunction } type SupportedFormats struct { @@ -79,7 +81,7 @@ func FileType(path string, types *SupportedFormats) (bool, *SupportedFormat, str if mimeType == v { fileType := types.Type(mimeType) - return true, fileType, mimeType, nil + return fileType.Validate(path), fileType, mimeType, nil } } diff --git a/formats/video.go b/formats/video.go index 04b3207..d7565f4 100644 --- a/formats/video.go +++ b/formats/video.go @@ -33,5 +33,8 @@ func RegisterVideoFormats() *SupportedFormat { `video/ogg`, `video/webm`, }, + Validate: func(filePath string) bool { + return true + }, } }