@@ -3,15 +3,16 @@ package main
33import (
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
1313func 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-
3632func 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
0 commit comments