diff options
| author | tjpcc <tjp@ctrl-c.club> | 2023-09-06 09:02:37 -0600 | 
|---|---|---|
| committer | tjpcc <tjp@ctrl-c.club> | 2023-09-06 09:02:37 -0600 | 
| commit | 9330d8546fff5e0397b4eec1a24bf37277a6b745 (patch) | |
| tree | 9ea08c97dc7ed69a7ce9c1e062c366d46d235408 | |
| parent | 9a68591255747c82fd4ce99351bca6d43349cafa (diff) | |
contrib/fs.GuessGopherItemType -> gopher.GuessItemType
| -rw-r--r-- | contrib/fs/gopher.go | 46 | ||||
| -rw-r--r-- | gopher/response.go | 47 | 
2 files changed, 49 insertions, 44 deletions
| diff --git a/contrib/fs/gopher.go b/contrib/fs/gopher.go index e902b4e..0594730 100644 --- a/contrib/fs/gopher.go +++ b/contrib/fs/gopher.go @@ -3,8 +3,6 @@ package fs  import (  	"context"  	"io/fs" -	"mime" -	"path"  	"strings"  	"text/template" @@ -26,7 +24,7 @@ func GopherFileHandler(fileSystem fs.FS) sr.Handler {  			return nil  		} -		return gopher.File(GuessGopherItemType(filepath), file) +		return gopher.File(gopher.GuessItemType(filepath), file)  	})  } @@ -99,7 +97,7 @@ func GopherDirectoryListing(fileSystem fs.FS, tpl *template.Template) sr.Handler  // - GuessItemType: return a gopher item type for a file based on its path/name.  var GopherTemplateFunctions = template.FuncMap{  	"GuessItemType": func(filepath string) string { -		return string([]byte{byte(GuessGopherItemType(filepath))}) +		return string([]byte{byte(gopher.GuessItemType(filepath))})  	},  } @@ -128,43 +126,3 @@ i		{{ $hostname }}	{{ $port }}  		),  	),  ) - -// GuessGopherItemType attempts to find the best gopher item type for a file based on its name. -func GuessGopherItemType(filepath string) sr.Status { -	ext := path.Ext(filepath) -	switch ext { -	case ".gophermap": -		return gopher.MenuType -	case ".txt", ".gmi": -		return gopher.TextFileType -	case ".gif", ".png", ".jpg", ".jpeg": -		return gopher.ImageFileType -	case ".mp4", ".mov": -		return gopher.MovieFileType -	case ".mp3", ".aiff", ".aif", ".aac", ".ogg", ".flac", ".alac", ".wma": -		return gopher.SoundFileType -	case ".bmp": -		return gopher.BitmapType -	case ".doc", ".docx", ".odt": -		return gopher.DocumentType -	case ".html", ".htm": -		return gopher.HTMLType -	case ".rtf": -		return gopher.RtfDocumentType -	case ".wav": -		return gopher.WavSoundFileType -	case ".pdf": -		return gopher.PdfDocumentType -	case ".xml": -		return gopher.XmlDocumentType -	case ".": -		return gopher.BinaryFileType -	} - -	mtype := mime.TypeByExtension(ext) -	if strings.HasPrefix(mtype, "text/") { -		return gopher.TextFileType -	} - -	return gopher.BinaryFileType -} 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 +} | 
