Factor Mime Type guessing into fs.MimeType()

This commit is contained in:
Nick Craig-Wood 2015-03-01 12:38:31 +00:00
parent fe68737268
commit 46d39ebaf7
4 changed files with 15 additions and 27 deletions

View File

@ -10,9 +10,7 @@ package drive
import (
"fmt"
"io"
"mime"
"net/http"
"path"
"strings"
"sync"
"time"
@ -705,20 +703,13 @@ func (f *FsDrive) Put(in io.Reader, remote string, modTime time.Time, size int64
return o, fmt.Errorf("Couldn't find or make directory: %s", err)
}
// Guess the mime type
mimeType := mime.TypeByExtension(path.Ext(o.remote))
if mimeType == "" {
mimeType = "application/octet-stream"
}
modifiedDate := modTime.Format(timeFormatOut)
// Define the metadata for the file we are going to create.
createInfo := &drive.File{
Title: leaf,
Description: leaf,
Parents: []*drive.ParentReference{{Id: directoryId}},
MimeType: mimeType,
ModifiedDate: modifiedDate,
MimeType: fs.MimeType(o),
ModifiedDate: modTime.Format(timeFormatOut),
}
// Make the API request to upload metadata and file data.

View File

@ -5,6 +5,8 @@ package fs
import (
"fmt"
"io"
"mime"
"path"
"sync"
)
@ -97,6 +99,15 @@ func Equal(src, dst Object) bool {
return true
}
// Returns a guess at the mime type from the extension
func MimeType(o Object) string {
mimeType := mime.TypeByExtension(path.Ext(o.Remote()))
if mimeType == "" {
mimeType = "application/octet-stream"
}
return mimeType
}
// Used to remove a failed copy
func removeFailedCopy(dst Object) {
if dst != nil {

View File

@ -17,7 +17,6 @@ import (
"encoding/hex"
"fmt"
"io"
"mime"
"net/http"
"path"
"regexp"
@ -549,16 +548,10 @@ func (o *FsObjectStorage) Open() (in io.ReadCloser, err error) {
//
// The new object may have been created if an error is returned
func (o *FsObjectStorage) Update(in io.Reader, modTime time.Time, size int64) error {
// Guess the content type
contentType := mime.TypeByExtension(path.Ext(o.remote))
if contentType == "" {
contentType = "application/octet-stream"
}
object := storage.Object{
Bucket: o.storage.bucket,
Name: o.storage.root + o.remote,
ContentType: contentType,
ContentType: fs.MimeType(o),
Size: uint64(size),
Updated: modTime.Format(timeFormatOut), // Doesn't get set
Metadata: metadataFromModTime(modTime),

View File

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"mime"
"net/http"
"path"
"regexp"
@ -536,13 +535,7 @@ func (o *FsObjectS3) Update(in io.Reader, modTime time.Time, size int64) error {
metaMtime: swift.TimeToFloatString(modTime),
}
// Guess the content type
contentType := mime.TypeByExtension(path.Ext(o.remote))
if contentType == "" {
contentType = "application/octet-stream"
}
_, err := o.s3.b.PutReaderHeaders(o.s3.root+o.remote, in, size, contentType, o.s3.perm, headers)
_, err := o.s3.b.PutReaderHeaders(o.s3.root+o.remote, in, size, fs.MimeType(o), o.s3.perm, headers)
if err != nil {
return err
}