organize
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"strings"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
)
|
||||
|
||||
func detectMimeType(file io.ReadSeeker) (string, error) {
|
||||
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
||||
return "", fmt.Errorf("rewind file: %w", err)
|
||||
}
|
||||
|
||||
mtype, dtcErr := mimetype.DetectReader(file)
|
||||
|
||||
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
||||
return "", fmt.Errorf("rewind file: %w", err)
|
||||
}
|
||||
|
||||
if dtcErr != nil {
|
||||
return "", fmt.Errorf("detect MIME type: %w", dtcErr)
|
||||
}
|
||||
|
||||
return mtype.String(), nil
|
||||
}
|
||||
|
||||
func normalizeMIMEType(contentType string) string {
|
||||
const plaintext = "text/plain; charset=utf-8"
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil {
|
||||
return plaintext
|
||||
}
|
||||
if strings.HasSuffix(mediaType, "+xml") {
|
||||
return plaintext
|
||||
}
|
||||
switch mediaType {
|
||||
case "text/html", "text/javascript", "application/javascript",
|
||||
"application/ecmascript", "text/ecmascript", "application/xml",
|
||||
"text/xml", "text/xsl",
|
||||
"text/css", "application/wasm":
|
||||
return plaintext
|
||||
case "application/pdf":
|
||||
return "application/octet-stream"
|
||||
default:
|
||||
return contentType
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user