Added UTF-8 validation for files when using --text

This commit is contained in:
Seednode 2023-09-11 19:38:38 -05:00
parent 3c21946351
commit 26bdedb5a2
6 changed files with 30 additions and 7 deletions

View File

@ -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")

View File

@ -34,5 +34,8 @@ func RegisterAudioFormats() *SupportedFormat {
`audio/ogg`,
`audio/wav`,
},
Validate: func(filePath string) bool {
return true
},
}
}

View File

@ -67,6 +67,9 @@ func RegisterImageFormats() *SupportedFormat {
`image/png`,
`image/webp`,
},
Validate: func(filePath string) bool {
return true
},
}
}

View File

@ -5,6 +5,7 @@ Copyright © 2023 Seednode <seednode@seedno.de>
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(`<a href="/%s"><pre>%s</pre></a>`,
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)
},
}
}

View File

@ -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
}
}

View File

@ -33,5 +33,8 @@ func RegisterVideoFormats() *SupportedFormat {
`video/ogg`,
`video/webm`,
},
Validate: func(filePath string) bool {
return true
},
}
}