2018-01-12 17:30:54 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2018-01-12 17:30:54 +01:00
|
|
|
"mime"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-09-24 19:47:04 +02:00
|
|
|
// Add a minimal number of mime types to augment go's built in types
|
|
|
|
// for environments which don't have access to a mime.types file (e.g.
|
|
|
|
// Termux on android)
|
|
|
|
func init() {
|
|
|
|
for _, t := range []struct {
|
|
|
|
mimeType string
|
|
|
|
extensions string
|
|
|
|
}{
|
|
|
|
{"audio/flac", ".flac"},
|
|
|
|
{"audio/mpeg", ".mpga,.mpega,.mp2,.mp3,.m4a"},
|
|
|
|
{"audio/ogg", ".oga,.ogg,.opus,.spx"},
|
|
|
|
{"audio/x-wav", ".wav"},
|
|
|
|
{"image/tiff", ".tiff,.tif"},
|
|
|
|
{"video/dv", ".dif,.dv"},
|
|
|
|
{"video/fli", ".fli"},
|
|
|
|
{"video/mpeg", ".mpeg,.mpg,.mpe"},
|
|
|
|
{"video/MP2T", ".ts"},
|
|
|
|
{"video/mp4", ".mp4"},
|
|
|
|
{"video/quicktime", ".qt,.mov"},
|
|
|
|
{"video/ogg", ".ogv"},
|
|
|
|
{"video/webm", ".webm"},
|
|
|
|
{"video/x-msvideo", ".avi"},
|
|
|
|
{"video/x-matroska", ".mpv,.mkv"},
|
|
|
|
{"text/srt", ".srt"},
|
|
|
|
} {
|
|
|
|
for _, ext := range strings.Split(t.extensions, ",") {
|
|
|
|
if mime.TypeByExtension(ext) == "" {
|
|
|
|
err := mime.AddExtensionType(ext, t.mimeType)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// MimeTypeFromName returns a guess at the mime type from the name
|
|
|
|
func MimeTypeFromName(remote string) (mimeType string) {
|
|
|
|
mimeType = mime.TypeByExtension(path.Ext(remote))
|
|
|
|
if !strings.ContainsRune(mimeType, '/') {
|
|
|
|
mimeType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
return mimeType
|
|
|
|
}
|
|
|
|
|
|
|
|
// MimeType returns the MimeType from the object, either by calling
|
|
|
|
// the MimeTyper interface or using MimeTypeFromName
|
2024-02-07 16:00:23 +01:00
|
|
|
func MimeType(ctx context.Context, o DirEntry) (mimeType string) {
|
2018-01-12 17:30:54 +01:00
|
|
|
// Read the MimeType from the optional interface if available
|
|
|
|
if do, ok := o.(MimeTyper); ok {
|
2019-06-17 10:34:30 +02:00
|
|
|
mimeType = do.MimeType(ctx)
|
2018-01-12 17:30:54 +01:00
|
|
|
// Debugf(o, "Read MimeType as %q", mimeType)
|
|
|
|
if mimeType != "" {
|
|
|
|
return mimeType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MimeTypeFromName(o.Remote())
|
|
|
|
}
|
2018-05-13 11:36:49 +02:00
|
|
|
|
|
|
|
// MimeTypeDirEntry returns the MimeType of a DirEntry
|
|
|
|
//
|
|
|
|
// It returns "inode/directory" for directories, or uses
|
|
|
|
// MimeType(Object)
|
2019-06-17 10:34:30 +02:00
|
|
|
func MimeTypeDirEntry(ctx context.Context, item DirEntry) string {
|
2018-05-13 11:36:49 +02:00
|
|
|
switch x := item.(type) {
|
|
|
|
case Object:
|
2019-06-17 10:34:30 +02:00
|
|
|
return MimeType(ctx, x)
|
2018-05-13 11:36:49 +02:00
|
|
|
case Directory:
|
|
|
|
return "inode/directory"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|