Code
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"mime"
|
||||
"strings"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type GopherTypeCharParams struct {
|
||||
GophermapFiles []string
|
||||
GophermapExtensions []string
|
||||
PromptFileExtensions []string
|
||||
}
|
||||
|
||||
var plainTextExtensions = []string{
|
||||
".txt", ".md", ".markdown", ".rst", ".adoc", ".text", ".asc", ".utxt",
|
||||
".json", ".jsonl", ".yaml", ".yml", ".toml", ".xml", ".xsd", ".xsl", ".xslt",
|
||||
".ini", ".conf", ".cfg", ".cnf", ".env", ".properties", ".prefs", ".opts",
|
||||
".csv", ".tsv", ".tab", ".log", ".sql", ".ddl", ".dump",
|
||||
".go", ".c", ".cpp", ".h", ".hpp", ".cc", ".hh", ".cxx", ".hxx", ".h++",
|
||||
".cs", ".fs", ".fsx", ".fsi", ".java", ".kt", ".kts", ".rs", ".swift",
|
||||
".py", ".pyw", ".rb", ".rbw", ".pl", ".pm", ".tcl", ".lua", ".php", ".phtml",
|
||||
".js", ".mjs", ".cjs", ".jsx", ".ts", ".tsx", ".vue", ".svelte", ".dart",
|
||||
".sh", ".bash", ".zsh", ".fish", ".bat", ".cmd", ".ps1", ".psm1", ".psd1",
|
||||
".css", ".scss", ".sass", ".less", ".styl", ".html", ".htm", ".shtml", ".xhtml",
|
||||
".dockerfile", ".makefile", ".cmake", ".gradle", ".gitignore", ".gitconfig",
|
||||
".gitattributes", ".editorconfig", ".npmrc", ".babelrc", ".eslintrc",
|
||||
".diff", ".patch", ".latex", ".tex", ".bib", ".cls", ".sty",
|
||||
".erl", ".hrl", ".ex", ".exs", ".hs", ".lhs", ".scala", ".sc", ".clj", ".cljs",
|
||||
".edn", ".lisp", ".lsp", ".scm", ".ss", ".r", ".rmd", ".f", ".f90", ".f95",
|
||||
".m", ".matlab", ".jl", ".v", ".vhdl", ".sv", ".proto", ".thrift",
|
||||
".tpl", ".tmpl", ".twig", ".blade", ".erb", ".haml", ".pug", ".jade",
|
||||
".srt", ".vtt", ".sub",
|
||||
}
|
||||
var areAdditionalMimeTypesRegistered = false
|
||||
|
||||
var archives = []string{
|
||||
"application/x-archive", "application/x-cpio", "application/x-shar",
|
||||
"application/x-iso9660-image", "application/x-sbx", "application/x-tar",
|
||||
"application/x-brotli", "application/x-bzip2", "application/vnd.genozip",
|
||||
"application/gzip", "application/lzip", "application/x-lzma",
|
||||
"application/x-lzop", "application/x-snappy-framed", "application/x-xz",
|
||||
"application/x-compress", "application/x-compress", "application/zstd",
|
||||
"application/x-7z-compressed", "application/x-7z-compressed",
|
||||
"application/x-ace-compressed", "application/x-astrotite-afa",
|
||||
"application/x-alz-compressed", "application/x-freearc", "application/x-arj",
|
||||
"application/x-b1", "application/vnd.ms-cab-compressed",
|
||||
"application/x-cfs-compressed", "application/x-dar", "application/x-dgc-compressed",
|
||||
"application/x-apple-diskimage", "application/x-gca-compressed",
|
||||
"application/x-lzh", "application/x-lzx", "application/x-rar-compressed",
|
||||
"application/x-stuffit", "application/x-stuffitx", "application/x-gtar",
|
||||
"application/x-ms-wim", "application/x-xar", "application/zip", "application/x-zoo",
|
||||
}
|
||||
|
||||
func (params *GopherTypeCharParams) GopherTypeChar(path string, isDir bool) string {
|
||||
switch {
|
||||
case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"):
|
||||
return "h"
|
||||
case strings.HasPrefix(path, "telnet://"):
|
||||
return "8"
|
||||
}
|
||||
|
||||
|
||||
extension := filepath.Ext(path)
|
||||
if isDir || slices.Contains(params.GophermapFiles, filepath.Base(path)) || slices.Contains(params.GophermapExtensions, extension) {
|
||||
return "1"
|
||||
}
|
||||
if slices.Contains(params.PromptFileExtensions, extension) {
|
||||
return "7"
|
||||
}
|
||||
|
||||
if !areAdditionalMimeTypesRegistered {
|
||||
for _, ext := range plainTextExtensions {
|
||||
mime.AddExtensionType(ext, "text/plain")
|
||||
}
|
||||
areAdditionalMimeTypesRegistered = true
|
||||
}
|
||||
|
||||
mimeType := mime.TypeByExtension(extension)
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(mimeType, "text/plain"):
|
||||
return "0"
|
||||
case slices.Contains(archives, mimeType):
|
||||
return "5"
|
||||
case mimeType == "image/gif":
|
||||
return "g"
|
||||
case strings.HasPrefix(mimeType, "image/"):
|
||||
return "I"
|
||||
case strings.HasPrefix(mimeType, "audio/"):
|
||||
return "s"
|
||||
case mimeType == "application/pdf":
|
||||
return "P"
|
||||
case strings.HasPrefix(mimeType, "text/html"):
|
||||
return "h"
|
||||
case strings.HasPrefix(mimeType, "text/rtf"):
|
||||
return "r"
|
||||
case strings.HasPrefix(mimeType, "text/xml") || strings.HasPrefix(mimeType, "application/xml"):
|
||||
return "X"
|
||||
default:
|
||||
return "9"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user