Skip to content

Commit

Permalink
revert: Switch to h2non/filetype as it is not production-ready yet
Browse files Browse the repository at this point in the history
This reverts commits 156e776..96286b2.
  • Loading branch information
5HT2 committed Jan 13, 2024
1 parent 156e776 commit c16e13b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
33 changes: 27 additions & 6 deletions fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ 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 {
b, err := os.ReadFile(file)
content := string(b)
content, err := ReadFile(file)

if err != nil {
log.Printf("- Failed to read '%s'", file)
Expand All @@ -29,6 +28,11 @@ 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 {
Expand Down Expand Up @@ -68,7 +72,7 @@ func IsDirectory(path string) (bool, error) {
}
}

func GetFileContentTypeExt(content []byte, file string) (string, error) {
func GetFileContentTypeExt(out *os.File, file string) (string, error) {
ext := filepath.Ext(file)

switch ext {
Expand All @@ -86,8 +90,25 @@ func GetFileContentTypeExt(content []byte, file string) (string, error) {
return "application/json; charset=utf-8", nil
}

kind, err := filetype.Match(content)
return kind.MIME.Type, err
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
}

// ReadLines reads a whole file into memory
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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=
Expand Down
29 changes: 16 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,33 +308,37 @@ func HandleServeFile(ctx *fasthttp.RequestCtx, path string, public bool) {
return
}

content, err := os.ReadFile(path)
if err != nil {
HandleInternalServerError(ctx, err)
return
}
content, err := ReadFile(path)

// File is empty
if len(content) == 0 {
ctx.Response.SetStatusCode(fasthttp.StatusNoContent)
return
}

kind, err := GetFileContentTypeExt(content, path)
if err != nil {
HandleInternalServerError(ctx, err)
return
}

// Open the file and handle errors
f, err := os.Open(path)
if err != nil {
HandleInternalServerError(ctx, err)
return
}
defer f.Close()

if len(kind) == 0 {
HandleGeneric(ctx, fasthttp.StatusInternalServerError, "Unknown file type")
// Get the contentType
contentType, err := GetFileContentTypeExt(f, path)
if err != nil {
HandleInternalServerError(ctx, err)
return
}

// Serve the file itself
ctx.Response.Header.Set(fasthttp.HeaderContentType, kind)
fmt.Fprint(ctx, string(content))
ctx.Response.Header.Set(fasthttp.HeaderContentType, contentType)
fmt.Fprint(ctx, content)
}

func HandleAppendFile(ctx *fasthttp.RequestCtx, path string) {
Expand All @@ -351,11 +355,10 @@ func HandleAppendFile(ctx *fasthttp.RequestCtx, path string) {
}

contentStr := string(content) + "\n"
oldContent, err := os.ReadFile(path)
oldContent, err := 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 = string(oldContent) + contentStr
contentStr = oldContent + contentStr
}

err = WriteToFile(path, contentStr)
Expand Down

0 comments on commit c16e13b

Please sign in to comment.