From cdee741a6dae27566c498de4ab059ec767dff57d Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 16 Jun 2018 01:46:51 -0400 Subject: [PATCH] Mendeley import: Fix duplicate PDF copying for PDFs in Downloaded For each PDF with an associated URL in the Downloaded directory, we were copying all files in the directory (!) to the attachment's storage directory. (Zotero imports always have files in separate directories, and this was a function used to save both single files and HTML snapshots.) We'll clean up the extra files in a separate step. --- .../zotero/import/mendeley/mendeleyImport.js | 1 + chrome/content/zotero/xpcom/attachments.js | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/import/mendeley/mendeleyImport.js b/chrome/content/zotero/import/mendeley/mendeleyImport.js index d869ad1e98..6cfa673526 100644 --- a/chrome/content/zotero/import/mendeley/mendeleyImport.js +++ b/chrome/content/zotero/import/mendeley/mendeleyImport.js @@ -983,6 +983,7 @@ Zotero_Import_Mendeley.prototype._saveFilesAndAnnotations = async function (file options.title = file.title; options.url = file.url; options.contentType = file.contentType; + options.singleFile = true; attachment = await Zotero.Attachments.importSnapshotFromFile(options); } else { diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 1a69ecaedf..137e354f43 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -176,14 +176,18 @@ Zotero.Attachments = new function(){ /** - * @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'parentItemID' + * @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'parentItemID', 'singleFile' * @return {Promise} */ this.importSnapshotFromFile = Zotero.Promise.coroutine(function* (options) { Zotero.debug('Importing snapshot from file'); var file = Zotero.File.pathToFile(options.file); - var fileName = file.leafName; + // TODO: Fix main filename when copying directory, though in that case it's probably + // from our own export and already clean + var fileName = options.singleFile + ? Zotero.File.getValidFileName(file.leafName) + : file.leafName; var url = options.url; var title = options.title; var contentType = options.contentType; @@ -194,7 +198,7 @@ Zotero.Attachments = new function(){ throw new Error("parentItemID not provided"); } - var attachmentItem, itemID, destDir, newFile; + var attachmentItem, itemID, destDir, newPath; try { yield Zotero.DB.executeTransaction(function* () { // Create a new attachment @@ -217,13 +221,23 @@ Zotero.Attachments = new function(){ var storageDir = Zotero.getStorageDirectory(); destDir = this.getStorageDirectory(attachmentItem); yield OS.File.removeDir(destDir.path); - file.parent.copyTo(storageDir, destDir.leafName); - - // Point to copied file - newFile = destDir.clone(); - newFile.append(file.leafName); + newPath = OS.Path.join(destDir.path, fileName); + // Copy single file to new directory + if (options.singleFile) { + yield this.createDirectoryForItem(attachmentItem); + yield OS.File.copy(file.path, newPath); + } + // Copy entire parent directory (for HTML snapshots) + else { + file.parent.copyTo(storageDir, destDir.leafName); + } }.bind(this)); - yield _postProcessFile(attachmentItem, newFile, contentType, charset); + yield _postProcessFile( + attachmentItem, + Zotero.File.pathToFile(newPath), + contentType, + charset + ); } catch (e) { Zotero.logError(e);