Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

Factor Mime Type guessing into fs.MimeType()

Nick Craig-Wood46d39ebaf753
diff --git a/drive/drive.go b/drive/drive.go
index a819dac02..ae43e7150 100644
--- a/drive/drive.go
+++ b/drive/drive.go
@@ -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.
diff --git a/fs/operations.go b/fs/operations.go
index db58769a3..b80f1ec79 100644
--- a/fs/operations.go
+++ b/fs/operations.go
@@ -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 {
diff --git a/googlecloudstorage/googlecloudstorage.go b/googlecloudstorage/googlecloudstorage.go
index a49aa3cdd..67c977143 100644
--- a/googlecloudstorage/googlecloudstorage.go
+++ b/googlecloudstorage/googlecloudstorage.go
@@ -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),
diff --git a/s3/s3.go b/s3/s3.go
index 13176991d..207c8b707 100644
--- a/s3/s3.go
+++ b/s3/s3.go
@@ -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
}