summaryrefslogtreecommitdiff
path: root/gopher/response.go
diff options
context:
space:
mode:
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
+}