From 1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b Mon Sep 17 00:00:00 2001 From: tjp Date: Mon, 13 Nov 2023 07:25:39 -0700 Subject: refactor contribs to work with a Protocol interface --- contrib/fs/file.go | 52 ---------------------------------------------------- 1 file changed, 52 deletions(-) (limited to 'contrib/fs/file.go') diff --git a/contrib/fs/file.go b/contrib/fs/file.go index 9f11f4f..4d79fea 100644 --- a/contrib/fs/file.go +++ b/contrib/fs/file.go @@ -1,36 +1,9 @@ package fs import ( - "mime" - "os" "strings" - "unicode/utf8" ) -func mediaType(filePath string) string { - if strings.HasSuffix(filePath, ".gmi") { - // This may not be present in the listings searched by mime.TypeByExtension, - // so provide a dedicated fast path for it here. - return "text/gemini" - } - - slashIdx := strings.LastIndex(filePath, "/") - dotIdx := strings.LastIndex(filePath[slashIdx+1:], ".") - if dotIdx == -1 { - return "application/octet-stream" - } - ext := filePath[slashIdx+1+dotIdx:] - - mtype := mime.TypeByExtension(ext) - if mtype == "" { - if contentsAreText(filePath) { - return "text/plain" - } - return "application/octet-stream" - } - return mtype -} - func isPrivate(fullpath string) bool { for _, segment := range strings.Split(fullpath, "/") { if len(segment) > 1 && segment[0] == '.' { @@ -39,28 +12,3 @@ func isPrivate(fullpath string) bool { } return false } - -func contentsAreText(filepath string) bool { - f, err := os.Open(filepath) - if err != nil { - return false - } - defer func() { _ = f.Close() }() - - var buf [1024]byte - n, err := f.Read(buf[:]) - if err != nil { - return false - } - - for i, c := range string(buf[:n]) { - if i+utf8.UTFMax > n { - // incomplete last char - break - } - if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' { - return false - } - } - return true -} -- cgit v1.2.3