From 9f8e1a1dc554bc9a55cbc845307a67356a36d4c2 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 5 Mar 2019 17:18:04 +0000 Subject: [PATCH] drive: fix imports of text files Before this change text file imports were ignored. This was because the mime type wasn't matched. Fix this by adjusting the keys in the mime type maps as well as the values. See: https://forum.rclone.org/t/how-to-upload-text-files-to-google-drive-as-google-docs/9014 --- backend/drive/drive.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/backend/drive/drive.go b/backend/drive/drive.go index a268e8b07..f60e0ba2f 100644 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -676,28 +676,33 @@ func isPowerOfTwo(x int64) bool { } // add a charset parameter to all text/* MIME types -func fixMimeType(mimeType string) string { - mediaType, param, err := mime.ParseMediaType(mimeType) +func fixMimeType(mimeTypeIn string) string { + if mimeTypeIn == "" { + return "" + } + mediaType, param, err := mime.ParseMediaType(mimeTypeIn) if err != nil { - return mimeType + return mimeTypeIn } - if strings.HasPrefix(mimeType, "text/") && param["charset"] == "" { + mimeTypeOut := mimeTypeIn + if strings.HasPrefix(mediaType, "text/") && param["charset"] == "" { param["charset"] = "utf-8" - mimeType = mime.FormatMediaType(mediaType, param) + mimeTypeOut = mime.FormatMediaType(mediaType, param) } - return mimeType + if mimeTypeOut == "" { + panic(errors.Errorf("unable to fix MIME type %q", mimeTypeIn)) + } + return mimeTypeOut } -func fixMimeTypeMap(m map[string][]string) map[string][]string { - for _, v := range m { +func fixMimeTypeMap(in map[string][]string) (out map[string][]string) { + out = make(map[string][]string, len(in)) + for k, v := range in { for i, mt := range v { - fixed := fixMimeType(mt) - if fixed == "" { - panic(errors.Errorf("unable to fix MIME type %q", mt)) - } - v[i] = fixed + v[i] = fixMimeType(mt) } + out[fixMimeType(k)] = v } - return m + return out } func isInternalMimeType(mimeType string) bool { return strings.HasPrefix(mimeType, "application/vnd.google-apps.")