Factor Mime Type guessing into fs.MimeType()
This commit is contained in:
parent
fe68737268
commit
46d39ebaf7
4 changed files with 15 additions and 27 deletions
|
@ -10,9 +10,7 @@ package drive
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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)
|
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.
|
// Define the metadata for the file we are going to create.
|
||||||
createInfo := &drive.File{
|
createInfo := &drive.File{
|
||||||
Title: leaf,
|
Title: leaf,
|
||||||
Description: leaf,
|
Description: leaf,
|
||||||
Parents: []*drive.ParentReference{{Id: directoryId}},
|
Parents: []*drive.ParentReference{{Id: directoryId}},
|
||||||
MimeType: mimeType,
|
MimeType: fs.MimeType(o),
|
||||||
ModifiedDate: modifiedDate,
|
ModifiedDate: modTime.Format(timeFormatOut),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the API request to upload metadata and file data.
|
// Make the API request to upload metadata and file data.
|
||||||
|
|
|
@ -5,6 +5,8 @@ package fs
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mime"
|
||||||
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -97,6 +99,15 @@ func Equal(src, dst Object) bool {
|
||||||
return true
|
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
|
// Used to remove a failed copy
|
||||||
func removeFailedCopy(dst Object) {
|
func removeFailedCopy(dst Object) {
|
||||||
if dst != nil {
|
if dst != nil {
|
||||||
|
|
|
@ -17,7 +17,6 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"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
|
// 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 {
|
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{
|
object := storage.Object{
|
||||||
Bucket: o.storage.bucket,
|
Bucket: o.storage.bucket,
|
||||||
Name: o.storage.root + o.remote,
|
Name: o.storage.root + o.remote,
|
||||||
ContentType: contentType,
|
ContentType: fs.MimeType(o),
|
||||||
Size: uint64(size),
|
Size: uint64(size),
|
||||||
Updated: modTime.Format(timeFormatOut), // Doesn't get set
|
Updated: modTime.Format(timeFormatOut), // Doesn't get set
|
||||||
Metadata: metadataFromModTime(modTime),
|
Metadata: metadataFromModTime(modTime),
|
||||||
|
|
9
s3/s3.go
9
s3/s3.go
|
@ -7,7 +7,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -536,13 +535,7 @@ func (o *FsObjectS3) Update(in io.Reader, modTime time.Time, size int64) error {
|
||||||
metaMtime: swift.TimeToFloatString(modTime),
|
metaMtime: swift.TimeToFloatString(modTime),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guess the content type
|
_, err := o.s3.b.PutReaderHeaders(o.s3.root+o.remote, in, size, fs.MimeType(o), o.s3.perm, headers)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue