Use extension map instead of switch case, improve contength parsing, func docs

This commit is contained in:
2024-01-30 22:20:09 -06:00
parent a1b6fa9a01
commit 5db0919956

View File

@@ -114,21 +114,27 @@ func Nonce() string {
func DoRequest(req *http.Request) (*http.Response, error) { func DoRequest(req *http.Request) (*http.Response, error) {
log.Debug().Str("method", strings.TrimRight(req.Method, " ")).Str("url", req.URL.String()).Str("query", req.URL.RawQuery).Str("content-type", req.Header.Get("Content-Type")).Msg("Request") log.Debug().Str("method", strings.TrimRight(req.Method, " ")).Str("url", req.URL.String()).Str("query", req.URL.RawQuery).Str("content-type", req.Header.Get("Content-Type")).Msg("Request")
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
log.Err(err).Str("method", req.Method).Msg("Request Failed") log.Err(err).Str("method", req.Method).Msg("Request Failed")
} else { } else {
contentLengthHeader := res.Header.Get("Content-Length")
contentLength := int64(-1)
// Get the content length // Get the content length
contentLength, err := strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64) if contentLengthHeader != "" {
contentLength, err = strconv.ParseInt(contentLengthHeader, 10, 64)
if err != nil { if err != nil {
log.Err(err).Msg("Failed to parse content length")
contentLength = -1 contentLength = -1
} }
}
log.Debug().Int("status", res.StatusCode).Int64("content-length", contentLength).Strs("content-type", res.Header["Content-Type"]).Msg("Response") log.Debug().Int("status", res.StatusCode).Int64("content-length", contentLength).Strs("content-type", res.Header["Content-Type"]).Msg("Response")
} }
return res, err return res, err
} }
// Plural is a simple helper function that returns an empty string if n is 1, and "s" otherwise.
func Plural(n int) string { func Plural(n int) string {
if n == 1 { if n == 1 {
return "" return ""
@@ -224,64 +230,42 @@ func GetPointer(value int) *int {
return &value return &value
} }
// GuessExtension returns the extension of a file based on the given content type var extensionMap = map[string]string{
func GuessExtension(contentType string) string { "text/plain": "txt",
switch strings.ToLower(contentType) { "application/json": "json",
case "text/plain": "text/html": "html",
return "txt" "text/css": "css",
case "application/json": "text/csv": "csv",
return "json" "text/calendar": "ics",
case "text/html": "text/markdown": "md",
return "html" "text/xml": "xml",
case "text/css": "text/yaml": "yaml",
return "css" "text/javascript": "js",
case "text/csv": "text/vtt": "vtt",
return "csv" "image/jpeg": "jpg",
case "text/calendar": "image/png": "png",
return "ics" "image/gif": "gif",
case "text/markdown": "image/webp": "webp",
return "md" "image/tiff": "tiff",
case "text/xml": "image/svg+xml": "svg",
return "xml" "image/bmp": "bmp",
case "text/yaml": "image/vnd.microsoft.icon": "ico",
return "yaml" "image/x-icon": "ico",
case "text/javascript": "image/x-xbitmap": "xbm",
return "js" "image/x-xpixmap": "xpm",
case "text/vtt": "image/x-xwindowdump": "xwd",
return "vtt" "image/avif": "avif",
case "image/jpeg": "image/apng": "apng",
return "jpg" "image/jxl": "jxl",
case "image/png":
return "png"
case "image/gif":
return "gif"
case "image/webp":
return "webp"
case "image/tiff":
return "tiff"
case "image/svg+xml":
return "svg"
case "image/bmp":
return "bmp"
case "image/vnd.microsoft.icon":
return "ico"
case "image/x-icon":
return "ico"
case "image/x-xbitmap":
return "xbm"
case "image/x-xpixmap":
return "xpm"
case "image/x-xwindowdump":
return "xwd"
case "image/avif":
return "avif"
case "image/apng":
return "apng"
case "image/jxl":
return "jxl"
} }
func GuessExtension(contentType string) string {
ext, ok := extensionMap[strings.ToLower(contentType)]
if !ok {
return "" return ""
} }
return ext
}
// DumpResponse dumps a response body to a file for debugging purposes // DumpResponse dumps a response body to a file for debugging purposes
func DumpResponse(res *http.Response) { func DumpResponse(res *http.Response) {