From 80b25daac72e512267379a7881f18c42f4f25126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=B6ller?= Date: Tue, 21 Aug 2018 12:54:12 +0200 Subject: [PATCH] drive: add support for multipart document extensions --- backend/drive/drive.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/backend/drive/drive.go b/backend/drive/drive.go index 250b20b0b..70ec279b7 100644 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -477,7 +477,7 @@ func (f *Fs) list(dirIDs []string, title string, directoriesOnly bool, filesOnly _ = parentsQuery.WriteByte(')') query = append(query, parentsQuery.String()) } - stem := "" + var stems []string if title != "" { // Escaping the backslash isn't documented but seems to work searchTitle := strings.Replace(title, `\`, `\\`, -1) @@ -485,16 +485,21 @@ func (f *Fs) list(dirIDs []string, title string, directoriesOnly bool, filesOnly // Convert / to / for search searchTitle = strings.Replace(searchTitle, "/", "/", -1) - handleGdocs := !directoriesOnly && !f.opt.SkipGdocs - // if the search title contains an extension and the extension is in the export extensions add a search - // for the filename without the extension. - // assume that export extensions don't contain escape sequences and only have one part (not .tar.gz) - if ext := path.Ext(searchTitle); handleGdocs && len(ext) > 0 && containsString(f.exportExtensions, ext) { - stem = title[:len(title)-len(ext)] - query = append(query, fmt.Sprintf("(name='%s' or name='%s')", searchTitle, searchTitle[:len(searchTitle)-len(ext)])) - } else { - query = append(query, fmt.Sprintf("name='%s'", searchTitle)) + var titleQuery bytes.Buffer + _, _ = fmt.Fprintf(&titleQuery, "(name='%s'", searchTitle) + if !directoriesOnly && !f.opt.SkipGdocs { + // If the search title has an extension that is in the export extensions add a search + // for the filename without the extension. + // Assume that export extensions don't contain escape sequences. + for _, ext := range f.exportExtensions { + if strings.HasSuffix(searchTitle, ext) { + stems = append(stems, title[:len(title)-len(ext)]) + _, _ = fmt.Fprintf(&titleQuery, " or name='%s'", searchTitle[:len(searchTitle)-len(ext)]) + } + } } + _ = titleQuery.WriteByte(')') + query = append(query, titleQuery.String()) } if directoriesOnly { query = append(query, fmt.Sprintf("mimeType='%s'", driveFolderType)) @@ -546,7 +551,14 @@ OUTER: // the `=` operator is case insensitive. if title != "" && title != item.Name { - if stem == "" || stem != item.Name { + found := false + for _, stem := range stems { + if stem == item.Name { + found = true + break + } + } + if !found { continue } _, exportName, _, _ := f.findExportFormat(item)