Skip to content

Commit

Permalink
cleanup: Remove redundant funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT2 committed Jan 8, 2024
1 parent 02fdfd2 commit 4143daa
Showing 1 changed file with 2 additions and 47 deletions.
49 changes: 2 additions & 47 deletions fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"encoding/json"
"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)
Expand All @@ -28,11 +27,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 {
Expand Down Expand Up @@ -72,45 +66,6 @@ func IsDirectory(path string) (bool, error) {
}
}

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

switch ext {
case ".txt", ".text":
return "text/plain; charset=utf-8", nil
case ".htm", ".html":
return "text/html", nil
case ".css":
return "text/css", nil
case ".js", ".mjs":
return "application/javascript", nil
case ".mov":
return "video/quicktime", nil
case ".json":
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
}

// ReadLines reads a whole file into memory
// and returns a slice of its lines.
func ReadLines(path string) ([]string, error) {
Expand Down

0 comments on commit 4143daa

Please sign in to comment.