From 7cb95f41297477788ddbf014915dc9bdba9c1ac8 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 10 Jan 2018 00:39:16 -0500 Subject: [PATCH] Automatically rename dragged file attachments from parent metadata Rename happens if only one file is dragged and the parent item has no existing file attachments. Closes #1405 --- chrome/content/zotero/xpcom/attachments.js | 14 ++- chrome/content/zotero/xpcom/itemTreeView.js | 20 ++++ test/tests/itemTreeViewTest.js | 117 ++++++++++++++++++++ 3 files changed, 149 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index e79fc7968..3555029c9 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -40,6 +40,7 @@ Zotero.Attachments = new function(){ * @param {Integer} [options.libraryID] * @param {Integer[]|String[]} [options.parentItemID] - Parent item to add item to * @param {Integer[]} [options.collections] - Collection keys or ids to add new item to + * @param {String} [options.fileBaseName] * @param {String} [options.contentType] * @param {String} [options.charset] * @param {Object} [options.saveOptions] - Options to pass to Zotero.Item::save() @@ -50,15 +51,24 @@ Zotero.Attachments = new function(){ var libraryID = options.libraryID; var file = Zotero.File.pathToFile(options.file); + var path = file.path; + var leafName = file.leafName; var parentItemID = options.parentItemID; var collections = options.collections; + var fileBaseName = options.fileBaseName; var contentType = options.contentType; var charset = options.charset; var saveOptions = options.saveOptions; - var newName = Zotero.File.getValidFileName(file.leafName); + if (fileBaseName) { + let ext = Zotero.File.getExtension(path); + var newName = fileBaseName + (ext != '' ? '.' + ext : ''); + } + else { + var newName = Zotero.File.getValidFileName(OS.Path.basename(leafName)); + } - if (file.leafName.endsWith(".lnk")) { + if (leafName.endsWith(".lnk")) { throw new Error("Cannot add Windows shortcut"); } if (parentItemID && collections) { diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index 7e138a09f..032e408cb 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -3233,9 +3233,26 @@ Zotero.ItemTreeView.prototype.drop = Zotero.Promise.coroutine(function* (row, or var notifierQueue = new Zotero.Notifier.Queue; try { + let parentItem; + let numExistingFileAttachments; + if (parentItemID) { + parentItem = Zotero.Items.get(parentItemID); + numExistingFileAttachments = parentItem.getAttachments() + .map(itemID => Zotero.Items.get(itemID)) + .filter(item => item.isFileAttachment()) + .length; + } + for (var i=0; i