Skip to content

Commit f9fff9a

Browse files
committed
impr: Start work on switching to \h2non/filetype\
This reverts commit c16e13b.
1 parent c16e13b commit f9fff9a

File tree

4 files changed

+22
-43
lines changed

4 files changed

+22
-43
lines changed

fileutil.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"bufio"
55
"encoding/json"
6+
"github.com/h2non/filetype"
67
"io/fs"
78
"log"
8-
"net/http"
99
"os"
1010
"path/filepath"
1111
)
1212

1313
func ReadFileUnsafe(file string, removeNewline bool) string {
14-
content, err := ReadFile(file)
14+
b, err := os.ReadFile(file)
15+
content := string(b)
1516

1617
if err != nil {
1718
log.Printf("- Failed to read '%s'", file)
@@ -28,11 +29,6 @@ func ReadFileUnsafe(file string, removeNewline bool) string {
2829
return content
2930
}
3031

31-
func ReadFile(file string) (string, error) {
32-
dat, err := os.ReadFile(file)
33-
return string(dat), err
34-
}
35-
3632
func ReadUserTokens() map[string]UserToken {
3733
dat, err := os.ReadFile("user_tokens.json")
3834
if err != nil {
@@ -72,7 +68,7 @@ func IsDirectory(path string) (bool, error) {
7268
}
7369
}
7470

75-
func GetFileContentTypeExt(out *os.File, file string) (string, error) {
71+
func GetFileContentTypeExt(content []byte, file string) (string, error) {
7672
ext := filepath.Ext(file)
7773

7874
switch ext {
@@ -90,25 +86,8 @@ func GetFileContentTypeExt(out *os.File, file string) (string, error) {
9086
return "application/json; charset=utf-8", nil
9187
}
9288

93-
return GetFileContentType(out)
94-
}
95-
96-
// GetFileContentType detects the content type
97-
// and returns a valid MIME type
98-
func GetFileContentType(out *os.File) (string, error) {
99-
// Only the first 512 bytes are used to sniff the content type.
100-
buffer := make([]byte, 512)
101-
102-
_, err := out.Read(buffer)
103-
if err != nil {
104-
return "", err
105-
}
106-
107-
// Use the net/http package's handy DetectContentType function. Always returns a valid
108-
// content-type by returning "application/octet-stream" if no others seemed to match.
109-
contentType := http.DetectContentType(buffer)
110-
111-
return contentType, nil
89+
kind, err := filetype.Match(content)
90+
return kind.MIME.Type, err
11291
}
11392

11493
// ReadLines reads a whole file into memory

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.17
44

55
require (
66
github.com/ferluci/fast-realip v1.0.1
7+
github.com/h2non/filetype v1.1.3
78
github.com/valyala/fasthttp v1.50.0
89
)
910

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sx
33
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
44
github.com/ferluci/fast-realip v1.0.1 h1:zPi0iv7zgOOlM/qJt9mozLz5IjVRAezaqHJoQ0JJ/yI=
55
github.com/ferluci/fast-realip v1.0.1/go.mod h1:Ag7xdRQ9GOCL/pwbDe4zJv6SlfYdROArc8O+qIKhRc4=
6+
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
7+
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
68
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
79
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
810
github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=

main.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,37 +308,33 @@ func HandleServeFile(ctx *fasthttp.RequestCtx, path string, public bool) {
308308
return
309309
}
310310

311-
content, err := ReadFile(path)
311+
content, err := os.ReadFile(path)
312+
if err != nil {
313+
HandleInternalServerError(ctx, err)
314+
return
315+
}
312316

313317
// File is empty
314318
if len(content) == 0 {
315319
ctx.Response.SetStatusCode(fasthttp.StatusNoContent)
316320
return
317321
}
318322

319-
if err != nil {
320-
HandleInternalServerError(ctx, err)
321-
return
322-
}
323+
kind, err := GetFileContentTypeExt(content, path)
323324

324-
// Open the file and handle errors
325-
f, err := os.Open(path)
326325
if err != nil {
327326
HandleInternalServerError(ctx, err)
328327
return
329328
}
330-
defer f.Close()
331329

332-
// Get the contentType
333-
contentType, err := GetFileContentTypeExt(f, path)
334-
if err != nil {
335-
HandleInternalServerError(ctx, err)
330+
if len(kind) == 0 {
331+
HandleGeneric(ctx, fasthttp.StatusInternalServerError, "Unknown file type")
336332
return
337333
}
338334

339335
// Serve the file itself
340-
ctx.Response.Header.Set(fasthttp.HeaderContentType, contentType)
341-
fmt.Fprint(ctx, content)
336+
ctx.Response.Header.Set(fasthttp.HeaderContentType, kind)
337+
fmt.Fprint(ctx, string(content))
342338
}
343339

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

357353
contentStr := string(content) + "\n"
358-
oldContent, err := ReadFile(path)
354+
oldContent, err := os.ReadFile(path)
359355

356+
// Append to old content only if the file didn't error, otherwise we just write to the file directly
360357
if err == nil {
361-
contentStr = oldContent + contentStr
358+
contentStr = string(oldContent) + contentStr
362359
}
363360

364361
err = WriteToFile(path, contentStr)

0 commit comments

Comments
 (0)