From b5eb25c19fb95508117a1cf47ed2303994ef9b1a Mon Sep 17 00:00:00 2001 From: 5HT2 Date: Fri, 12 Jan 2024 19:21:27 -0800 Subject: [PATCH] impr: Start work on switching to `h2non/filetype` This reverts commit c16e13bbd763abb896d15b749573daebe78075a9. --- fileutil.go | 33 ++++++--------------------------- go.mod | 1 + go.sum | 2 ++ main.go | 29 +++++++++++++---------------- 4 files changed, 22 insertions(+), 43 deletions(-) diff --git a/fileutil.go b/fileutil.go index 8b18845..d5531df 100644 --- a/fileutil.go +++ b/fileutil.go @@ -3,15 +3,16 @@ package main import ( "bufio" "encoding/json" + "github.com/h2non/filetype" "io/fs" "log" - "net/http" "os" "path/filepath" ) func ReadFileUnsafe(file string, removeNewline bool) string { - content, err := ReadFile(file) + b, err := os.ReadFile(file) + content := string(b) if err != nil { log.Printf("- Failed to read '%s'", file) @@ -28,11 +29,6 @@ func ReadFileUnsafe(file string, removeNewline bool) string { return content } -func ReadFile(file string) (string, error) { - dat, err := os.ReadFile(file) - return string(dat), err -} - func ReadUserTokens() map[string]UserToken { dat, err := os.ReadFile("user_tokens.json") if err != nil { @@ -72,7 +68,7 @@ func IsDirectory(path string) (bool, error) { } } -func GetFileContentTypeExt(out *os.File, file string) (string, error) { +func GetFileContentTypeExt(content []byte, file string) (string, error) { ext := filepath.Ext(file) switch ext { @@ -90,25 +86,8 @@ func GetFileContentTypeExt(out *os.File, file string) (string, error) { return "application/json; charset=utf-8", nil } - return GetFileContentType(out) -} - -// GetFileContentType detects the content type -// and returns a valid MIME type -func GetFileContentType(out *os.File) (string, error) { - // Only the first 512 bytes are used to sniff the content type. - buffer := make([]byte, 512) - - _, err := out.Read(buffer) - if err != nil { - return "", err - } - - // Use the net/http package's handy DetectContentType function. Always returns a valid - // content-type by returning "application/octet-stream" if no others seemed to match. - contentType := http.DetectContentType(buffer) - - return contentType, nil + kind, err := filetype.Match(content) + return kind.MIME.Type, err } // ReadLines reads a whole file into memory diff --git a/go.mod b/go.mod index f1167be..44cdf65 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/ferluci/fast-realip v1.0.1 + github.com/h2non/filetype v1.1.3 github.com/valyala/fasthttp v1.50.0 ) diff --git a/go.sum b/go.sum index 8c19c43..5fec9f1 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sx github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/ferluci/fast-realip v1.0.1 h1:zPi0iv7zgOOlM/qJt9mozLz5IjVRAezaqHJoQ0JJ/yI= github.com/ferluci/fast-realip v1.0.1/go.mod h1:Ag7xdRQ9GOCL/pwbDe4zJv6SlfYdROArc8O+qIKhRc4= +github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= +github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g= diff --git a/main.go b/main.go index 4f6078f..405b5be 100644 --- a/main.go +++ b/main.go @@ -308,7 +308,11 @@ func HandleServeFile(ctx *fasthttp.RequestCtx, path string, public bool) { return } - content, err := ReadFile(path) + content, err := os.ReadFile(path) + if err != nil { + HandleInternalServerError(ctx, err) + return + } // File is empty if len(content) == 0 { @@ -316,29 +320,21 @@ func HandleServeFile(ctx *fasthttp.RequestCtx, path string, public bool) { return } - if err != nil { - HandleInternalServerError(ctx, err) - return - } + kind, err := GetFileContentTypeExt(content, path) - // Open the file and handle errors - f, err := os.Open(path) if err != nil { HandleInternalServerError(ctx, err) return } - defer f.Close() - // Get the contentType - contentType, err := GetFileContentTypeExt(f, path) - if err != nil { - HandleInternalServerError(ctx, err) + if len(kind) == 0 { + HandleGeneric(ctx, fasthttp.StatusInternalServerError, "Unknown file type") return } // Serve the file itself - ctx.Response.Header.Set(fasthttp.HeaderContentType, contentType) - fmt.Fprint(ctx, content) + ctx.Response.Header.Set(fasthttp.HeaderContentType, kind) + fmt.Fprint(ctx, string(content)) } func HandleAppendFile(ctx *fasthttp.RequestCtx, path string) { @@ -355,10 +351,11 @@ func HandleAppendFile(ctx *fasthttp.RequestCtx, path string) { } contentStr := string(content) + "\n" - oldContent, err := ReadFile(path) + oldContent, err := os.ReadFile(path) + // Append to old content only if the file didn't error, otherwise we just write to the file directly if err == nil { - contentStr = oldContent + contentStr + contentStr = string(oldContent) + contentStr } err = WriteToFile(path, contentStr)