summaryrefslogtreecommitdiff
path: root/gopher/response.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-09-06 09:02:37 -0600
committertjpcc <tjp@ctrl-c.club>2023-09-06 09:02:37 -0600
commit9330d8546fff5e0397b4eec1a24bf37277a6b745 (patch)
tree9ea08c97dc7ed69a7ce9c1e062c366d46d235408 /gopher/response.go
parent9a68591255747c82fd4ce99351bca6d43349cafa (diff)
contrib/fs.GuessGopherItemType -> gopher.GuessItemType
Diffstat (limited to 'gopher/response.go')
-rw-r--r--gopher/response.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/gopher/response.go b/gopher/response.go
index 042feba..1ad7f1d 100644
--- a/gopher/response.go
+++ b/gopher/response.go
@@ -4,6 +4,9 @@ import (
"bytes"
"fmt"
"io"
+ "mime"
+ "path"
+ "strings"
"sync"
"tildegit.org/tjp/sliderule/internal/types"
@@ -160,3 +163,47 @@ func (rdr *responseReader) ensureReader() {
rdr.reader = io.MultiReader(rdr.Body)
})
}
+
+// GuessItemType attempts to find the best gopher item type for a file based on its name.
+func GuessItemType(filepath string) types.Status {
+ ext := path.Ext(filepath)
+ switch ext {
+ case ".gophermap":
+ return MenuType
+ case ".txt", ".gmi", ".md":
+ return TextFileType
+ case ".gif":
+ return GifFileType
+ case ".png":
+ return PngImageFileType
+ case ".jpg", ".jpeg", ".tif", ".tiff":
+ return ImageFileType
+ case ".mp4", ".mov":
+ return MovieFileType
+ case ".pcm", ".mp3", ".aiff", ".aif", ".aac", ".ogg", ".flac", ".alac", ".wma":
+ return SoundFileType
+ case ".bmp":
+ return BitmapType
+ case ".doc", ".docx", ".odt", ".fodt":
+ return DocumentType
+ case ".html", ".htm":
+ return HTMLType
+ case ".rtf":
+ return RtfDocumentType
+ case ".wav":
+ return WavSoundFileType
+ case ".pdf":
+ return PdfDocumentType
+ case ".xml", ".atom":
+ return XmlDocumentType
+ case ".exe", ".bin", ".out", ".dylib", ".dll", ".so", ".a", ".o":
+ return BinaryFileType
+ }
+
+ mtype := mime.TypeByExtension(ext)
+ if strings.HasPrefix(mtype, "text/") {
+ return TextFileType
+ }
+
+ return BinaryFileType
+}