Added UTF-8 validation for files when using --text
This commit is contained in:
parent
3c21946351
commit
26bdedb5a2
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version string = "0.68.0"
|
Version string = "0.69.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -94,7 +94,7 @@ func init() {
|
||||||
rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting")
|
rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting")
|
||||||
rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint")
|
rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint")
|
||||||
rootCmd.Flags().StringVar(&statisticsFile, "stats-file", "", "path to optional persistent stats file")
|
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(&verbose, "verbose", "v", false, "log accessed files to stdout")
|
||||||
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit")
|
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "display version and exit")
|
||||||
rootCmd.Flags().BoolVar(&videos, "video", false, "enable support for video files")
|
rootCmd.Flags().BoolVar(&videos, "video", false, "enable support for video files")
|
||||||
|
|
|
@ -34,5 +34,8 @@ func RegisterAudioFormats() *SupportedFormat {
|
||||||
`audio/ogg`,
|
`audio/ogg`,
|
||||||
`audio/wav`,
|
`audio/wav`,
|
||||||
},
|
},
|
||||||
|
Validate: func(filePath string) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,9 @@ func RegisterImageFormats() *SupportedFormat {
|
||||||
`image/png`,
|
`image/png`,
|
||||||
`image/webp`,
|
`image/webp`,
|
||||||
},
|
},
|
||||||
|
Validate: func(filePath string) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ Copyright © 2023 Seednode <seednode@seedno.de>
|
||||||
package formats
|
package formats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
@ -24,10 +25,6 @@ func RegisterTextFormats() *SupportedFormat {
|
||||||
body = []byte{}
|
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>`,
|
return fmt.Sprintf(`<a href="/%s"><pre>%s</pre></a>`,
|
||||||
queryParams,
|
queryParams,
|
||||||
body)
|
body)
|
||||||
|
@ -52,5 +49,20 @@ func RegisterTextFormats() *SupportedFormat {
|
||||||
`text/plain`,
|
`text/plain`,
|
||||||
`text/plain; charset=utf-8`,
|
`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)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type FormatFunction func(queryParams, fileUri, filePath, fileName, mime string) string
|
type FormatFunction func(queryParams, fileUri, filePath, fileName, mime string) string
|
||||||
|
type ValidateFunction func(filePath string) bool
|
||||||
|
|
||||||
type SupportedFormat struct {
|
type SupportedFormat struct {
|
||||||
Css FormatFunction
|
Css FormatFunction
|
||||||
|
@ -18,6 +19,7 @@ type SupportedFormat struct {
|
||||||
Body FormatFunction
|
Body FormatFunction
|
||||||
Extensions []string
|
Extensions []string
|
||||||
MimeTypes []string
|
MimeTypes []string
|
||||||
|
Validate ValidateFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
type SupportedFormats struct {
|
type SupportedFormats struct {
|
||||||
|
@ -79,7 +81,7 @@ func FileType(path string, types *SupportedFormats) (bool, *SupportedFormat, str
|
||||||
if mimeType == v {
|
if mimeType == v {
|
||||||
fileType := types.Type(mimeType)
|
fileType := types.Type(mimeType)
|
||||||
|
|
||||||
return true, fileType, mimeType, nil
|
return fileType.Validate(path), fileType, mimeType, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,5 +33,8 @@ func RegisterVideoFormats() *SupportedFormat {
|
||||||
`video/ogg`,
|
`video/ogg`,
|
||||||
`video/webm`,
|
`video/webm`,
|
||||||
},
|
},
|
||||||
|
Validate: func(filePath string) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue