github.com/divyam234/rclone@v1.64.1/fs/mimetype.go (about) 1 package fs 2 3 import ( 4 "context" 5 "mime" 6 "path" 7 "strings" 8 ) 9 10 // Add a minimal number of mime types to augment go's built in types 11 // for environments which don't have access to a mime.types file (e.g. 12 // Termux on android) 13 func init() { 14 for _, t := range []struct { 15 mimeType string 16 extensions string 17 }{ 18 {"audio/flac", ".flac"}, 19 {"audio/mpeg", ".mpga,.mpega,.mp2,.mp3,.m4a"}, 20 {"audio/ogg", ".oga,.ogg,.opus,.spx"}, 21 {"audio/x-wav", ".wav"}, 22 {"image/tiff", ".tiff,.tif"}, 23 {"video/dv", ".dif,.dv"}, 24 {"video/fli", ".fli"}, 25 {"video/mpeg", ".mpeg,.mpg,.mpe"}, 26 {"video/MP2T", ".ts"}, 27 {"video/mp4", ".mp4"}, 28 {"video/quicktime", ".qt,.mov"}, 29 {"video/ogg", ".ogv"}, 30 {"video/webm", ".webm"}, 31 {"video/x-msvideo", ".avi"}, 32 {"video/x-matroska", ".mpv,.mkv"}, 33 {"text/srt", ".srt"}, 34 } { 35 for _, ext := range strings.Split(t.extensions, ",") { 36 if mime.TypeByExtension(ext) == "" { 37 err := mime.AddExtensionType(ext, t.mimeType) 38 if err != nil { 39 panic(err) 40 } 41 } 42 } 43 } 44 } 45 46 // MimeTypeFromName returns a guess at the mime type from the name 47 func MimeTypeFromName(remote string) (mimeType string) { 48 mimeType = mime.TypeByExtension(path.Ext(remote)) 49 if !strings.ContainsRune(mimeType, '/') { 50 mimeType = "application/octet-stream" 51 } 52 return mimeType 53 } 54 55 // MimeType returns the MimeType from the object, either by calling 56 // the MimeTyper interface or using MimeTypeFromName 57 func MimeType(ctx context.Context, o ObjectInfo) (mimeType string) { 58 // Read the MimeType from the optional interface if available 59 if do, ok := o.(MimeTyper); ok { 60 mimeType = do.MimeType(ctx) 61 // Debugf(o, "Read MimeType as %q", mimeType) 62 if mimeType != "" { 63 return mimeType 64 } 65 } 66 return MimeTypeFromName(o.Remote()) 67 } 68 69 // MimeTypeDirEntry returns the MimeType of a DirEntry 70 // 71 // It returns "inode/directory" for directories, or uses 72 // MimeType(Object) 73 func MimeTypeDirEntry(ctx context.Context, item DirEntry) string { 74 switch x := item.(type) { 75 case Object: 76 return MimeType(ctx, x) 77 case Directory: 78 return "inode/directory" 79 } 80 return "" 81 }