github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/mimetype.go (about)

     1  package fs
     2  
     3  import (
     4  	"context"
     5  	"mime"
     6  	"path"
     7  	"strings"
     8  )
     9  
    10  // MimeTypeFromName returns a guess at the mime type from the name
    11  func MimeTypeFromName(remote string) (mimeType string) {
    12  	mimeType = mime.TypeByExtension(path.Ext(remote))
    13  	if !strings.ContainsRune(mimeType, '/') {
    14  		mimeType = "application/octet-stream"
    15  	}
    16  	return mimeType
    17  }
    18  
    19  // MimeType returns the MimeType from the object, either by calling
    20  // the MimeTyper interface or using MimeTypeFromName
    21  func MimeType(ctx context.Context, o ObjectInfo) (mimeType string) {
    22  	// Read the MimeType from the optional interface if available
    23  	if do, ok := o.(MimeTyper); ok {
    24  		mimeType = do.MimeType(ctx)
    25  		// Debugf(o, "Read MimeType as %q", mimeType)
    26  		if mimeType != "" {
    27  			return mimeType
    28  		}
    29  	}
    30  	return MimeTypeFromName(o.Remote())
    31  }
    32  
    33  // MimeTypeDirEntry returns the MimeType of a DirEntry
    34  //
    35  // It returns "inode/directory" for directories, or uses
    36  // MimeType(Object)
    37  func MimeTypeDirEntry(ctx context.Context, item DirEntry) string {
    38  	switch x := item.(type) {
    39  	case Object:
    40  		return MimeType(ctx, x)
    41  	case Directory:
    42  		return "inode/directory"
    43  	}
    44  	return ""
    45  }