Skip to content

Commit

Permalink
Add additional content types. Add extra tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio committed Sep 9, 2024
1 parent d32fbf0 commit 0cfd13d
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 85 deletions.
199 changes: 114 additions & 85 deletions internal/server/ctypes.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,126 @@
package server

import "log"

var ctypes = []struct {
Extension string
Extension []string
ContentType string
}{
{".aac", "audio/aac"},
{".abw", "application/x-abiword"},
{".apng", "image/apng"},
{".arc", "application/x-freearc"},
{".avif", "image/avif"},
{".avi", "video/x-msvideo"},
{".azw", "application/vnd.amazon.ebook"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".bz", "application/x-bzip"},
{".bz2", "application/x-bzip2"},
{".cda", "application/x-cdf"},
{".csh", "application/x-csh"},
{".css", "text/css"},
{".csv", "text/csv"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".eot", "application/vnd.ms-fontobject"},
{".epub", "application/epub+zip"},
{".gz", "application/gzip"},
{".gif", "image/gif"},
{".htm", "text/html"},
{".html", "text/html"},
{".ico", "image/vnd.microsoft.icon"},
{".ics", "text/calendar"},
{".jar", "application/java-archive"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "text/javascript"},
{".json", "application/json"},
{".jsonld", "application/ld+json"},
{".mid", "audio/midi"},
{".midi", "audio/midi"},
{".mjs", "text/javascript"},
{".mp3", "audio/mpeg"},
{".mp4", "video/mp4"},
{".mpeg", "video/mpeg"},
{".mpkg", "application/vnd.apple.installer+xml"},
{".odp", "application/vnd.oasis.opendocument.presentation"},
{".ods", "application/vnd.oasis.opendocument.spreadsheet"},
{".odt", "application/vnd.oasis.opendocument.text"},
{".oga", "audio/ogg"},
{".ogv", "video/ogg"},
{".ogx", "application/ogg"},
{".opus", "audio/ogg"},
{".otf", "font/otf"},
{".png", "image/png"},
{".pdf", "application/pdf"},
{".php", "application/x-httpd-php"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".rar", "application/vnd.rar"},
{".rtf", "application/rtf"},
{".sh", "application/x-sh"},
{".svg", "image/svg+xml"},
{".tar", "application/x-tar"},
{".tif", "image/tiff"},
{".tiff", "image/tiff"},
{".ts", "video/mp2t"},
{".ttf", "font/ttf"},
{".txt", "text/plain"},
{".vsd", "application/vnd.visio"},
{".wav", "audio/wav"},
{".weba", "audio/webm"},
{".webm", "video/webm"},
{".webp", "image/webp"},
{".woff", "font/woff"},
{".woff2", "font/woff2"},
{".xhtml", "application/xhtml+xml"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".xml", "application/xml"},
{".xul", "application/vnd.mozilla.xul+xml"},
{".zip", "application/zip"},
{".3gp", "video/3gpp"},
{".3gpp", "video/3gpp"},
{".3g2", "video/3gpp2"},
{".3gpp2", "video/3gpp2"},
{".7z", "application/x-7z-compressed"},
// Generic types
{[]string{".aac"}, "audio/aac"},
{[]string{".abw"}, "application/x-abiword"},
{[]string{".apng"}, "image/apng"},
{[]string{".arc"}, "application/x-freearc"},
{[]string{".avif"}, "image/avif"},
{[]string{".avi"}, "video/x-msvideo"},
{[]string{".azw"}, "application/vnd.amazon.ebook"},
{[]string{".bin"}, "application/octet-stream"},
{[]string{".bmp"}, "image/bmp"},
{[]string{".bz"}, "application/x-bzip"},
{[]string{".bz2"}, "application/x-bzip2"},
{[]string{".cda"}, "application/x-cdf"},
{[]string{".csh"}, "application/x-csh"},
{[]string{".css"}, "text/css"},
{[]string{".csv"}, "text/csv"},
{[]string{".doc"}, "application/msword"},
{[]string{".docx"}, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{[]string{".eot"}, "application/vnd.ms-fontobject"},
{[]string{".epub"}, "application/epub+zip"},
{[]string{".gz"}, "application/gzip"},
{[]string{".gif"}, "image/gif"},
{[]string{".htm", ".html"}, "text/html"},
{[]string{".ico"}, "image/vnd.microsoft.icon"},
{[]string{".ics"}, "text/calendar"},
{[]string{".jar"}, "application/java-archive"},
{[]string{".jpeg", ".jpg"}, "image/jpeg"},
{[]string{".js", ".mjs"}, "text/javascript"},
{[]string{".jsx"}, "text/jsx"},
{[]string{".json"}, "application/json"},
{[]string{".jsonld"}, "application/ld+json"},
{[]string{".md"}, "text/markdown"},
{[]string{".mid", ".midi"}, "audio/midi"},
{[]string{".mp3"}, "audio/mpeg"},
{[]string{".mp4"}, "video/mp4"},
{[]string{".mpeg"}, "video/mpeg"},
{[]string{".mpkg"}, "application/vnd.apple.installer+xml"},
{[]string{".odp"}, "application/vnd.oasis.opendocument.presentation"},
{[]string{".ods"}, "application/vnd.oasis.opendocument.spreadsheet"},
{[]string{".odt"}, "application/vnd.oasis.opendocument.text"},
{[]string{".oga"}, "audio/ogg"},
{[]string{".ogv"}, "video/ogg"},
{[]string{".ogx"}, "application/ogg"},
{[]string{".opus"}, "audio/ogg"},
{[]string{".otf"}, "font/otf"},
{[]string{".png"}, "image/png"},
{[]string{".pdf"}, "application/pdf"},
{[]string{".php"}, "application/x-httpd-php"},
{[]string{".ppt"}, "application/vnd.ms-powerpoint"},
{[]string{".pptx"}, "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{[]string{".rar"}, "application/vnd.rar"},
{[]string{".rtf"}, "application/rtf"},
{[]string{".sh"}, "application/x-sh"},
{[]string{".svg"}, "image/svg+xml"},
{[]string{".tar"}, "application/x-tar"},
{[]string{".tif", ".tiff"}, "image/tiff"},
{[]string{".ts"}, "video/mp2t"},
{[]string{".tsx"}, "text/tsx"},
{[]string{".ttf"}, "font/ttf"},
{[]string{".txt", ".ini", ".env", ".lock", ".conf", ".gitignore", ".dockerfile"}, "text/plain"},
{[]string{".vsd"}, "application/vnd.visio"},
{[]string{".wav"}, "audio/wav"},
{[]string{".weba"}, "audio/webm"},
{[]string{".webm"}, "video/webm"},
{[]string{".webp"}, "image/webp"},
{[]string{".woff"}, "font/woff"},
{[]string{".woff2"}, "font/woff2"},
{[]string{".xhtml"}, "application/xhtml+xml"},
{[]string{".xls"}, "application/vnd.ms-excel"},
{[]string{".xlsx"}, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{[]string{".xml"}, "application/xml"},
{[]string{".xul"}, "application/vnd.mozilla.xul+xml"},
{[]string{".yaml", ".yml"}, "application/x-yaml"},
{[]string{".zip"}, "application/zip"},
{[]string{".3gp", ".3gpp"}, "video/3gpp"},
{[]string{".3g2", ".3gpp2"}, "video/3gpp2"},
{[]string{".7z"}, "application/x-7z-compressed"},
{[]string{".scss"}, "text/x-scss"},
{[]string{".sass"}, "text/x-sass"},
{[]string{".less"}, "text/css"},
{[]string{".bat"}, "application/x-msdos-program"},
{[]string{".bashrc"}, "application/x-shellscript"},

// Programming languages
{[]string{".c"}, "text/x-csrc"},
{[]string{".h"}, "text/x-chdr"},
{[]string{".cpp"}, "text/x-c++src"},
{[]string{".hpp"}, "text/x-c++hdr"},
{[]string{".java"}, "text/x-java-source"},
{[]string{".py"}, "text/x-python"},
{[]string{".go"}, "text/x-go"},
{[]string{".rb"}, "application/x-ruby"},
{[]string{".pl"}, "application/x-perl"},
{[]string{".php"}, "application/x-httpd-php"},
{[]string{".rs"}, "text/rust"},
{[]string{".swift"}, "text/x-swift"},
{[]string{".kt"}, "text/x-kotlin"},
{[]string{".scala"}, "text/x-scala"},
{[]string{".sh"}, "application/x-sh"},
{[]string{".bash"}, "application/x-shellscript"},

// Markup languages
{[]string{".tex"}, "application/x-tex"},
{[]string{".bib"}, "application/x-bibtex"},

// Version control, configurations
{[]string{"Dockerfile"}, "text/x-dockerfile"},
{[]string{".gitignore"}, "text/plain"},
{[]string{"Makefile"}, "text/x-makefile"},
}

func getContentTypeForExtension(extension string) string {
for _, ct := range ctypes {
log.Printf("Comparing %q vs %q", ct.Extension, extension)
if ct.Extension == extension {
return ct.ContentType
for _, ext := range ct.Extension {
if ext == extension {
return ct.ContentType
}
}
}

Expand Down
58 changes: 58 additions & 0 deletions internal/server/ctypes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package server

import "testing"

func Test_getContentTypeForExtension(t *testing.T) {
tests := []struct {
extension string
want string
}{
{
extension: ".css",
want: "text/css",
},
{
extension: ".html",
want: "text/html",
},
{
extension: ".js",
want: "text/javascript",
},
{
extension: ".json",
want: "application/json",
},
{
extension: ".jpg",
want: "image/jpeg",
},
{
extension: ".jpeg",
want: "image/jpeg",
},
{
extension: ".png",
want: "image/png",
},
{
extension: ".svg",
want: "image/svg+xml",
},
{
extension: ".gif",
want: "image/gif",
},
{
extension: ".webp",
want: "image/webp",
},
}
for _, tt := range tests {
t.Run(tt.extension, func(t *testing.T) {
if got := getContentTypeForExtension(tt.extension); got != tt.want {
t.Errorf("getContentTypeForExtension() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 0cfd13d

Please sign in to comment.