commit da30f9e25c2e2b5047e21602bc559b5f323d7682
parent f5a2534169944c54659bc3e9f996f4fce0478c69
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 9 Mar 2018 01:07:57 -0500
Restore attachment title and filename when undoing Retrieve Metadata
Diffstat:
1 file changed, 32 insertions(+), 8 deletions(-)
diff --git a/chrome/content/zotero/xpcom/recognizePDF.js b/chrome/content/zotero/xpcom/recognizePDF.js
@@ -135,13 +135,12 @@ Zotero.RecognizePDF = new function () {
this.canUnrecognize = function (item) {
- var threshold = UNRECOGNIZE_TIMEOUT;
- var added = _newItems.get(item);
+ var { dateModified } = _newItems.get(item) || {};
// Item must have been recognized recently, must not have been modified since it was
// created, and must have only one attachment and no other children
- if (!added
- || Zotero.Date.sqlToDate(added, true) < new Date() - threshold
- || item.dateModified != added
+ if (!dateModified
+ || Zotero.Date.sqlToDate(dateModified, true) < new Date() - UNRECOGNIZE_TIMEOUT
+ || item.dateModified != dateModified
|| item.numAttachments(true) != 1
|| item.numChildren(true) != 1) {
_newItems.delete(item);
@@ -160,7 +159,22 @@ Zotero.RecognizePDF = new function () {
this.unrecognize = async function (item) {
+ var { originalTitle, originalFilename } = _newItems.get(item);
var attachment = Zotero.Items.get(item.getAttachments()[0]);
+
+ try {
+ let currentFilename = attachment.attachmentFilename;
+ if (currentFilename != originalFilename) {
+ let renamed = await attachment.renameAttachmentFile(originalFilename);
+ if (renamed) {
+ attachment.setField('title', originalTitle);
+ }
+ }
+ }
+ catch (e) {
+ Zotero.logError(e);
+ }
+
return Zotero.DB.executeTransaction(async function () {
let collections = item.getCollections();
attachment.parentItemID = null
@@ -174,7 +188,7 @@ Zotero.RecognizePDF = new function () {
this.report = async function (item) {
var attachment = Zotero.Items.get(item.getAttachments()[0]);
- var filePath = await attachment.getFilePath();
+ var filePath = attachment.getFilePath();
if (!filePath || !await OS.File.exists(filePath)) {
throw new Error("File not found when reporting metadata");
}
@@ -355,9 +369,12 @@ Zotero.RecognizePDF = new function () {
await attachment.save();
});
+ var originalTitle = attachment.getField('title');
+ var path = attachment.getFilePath();
+ var originalFilename = OS.Path.basename(path);
+
// Rename attachment file to match new metadata
if (Zotero.Prefs.get('autoRenameFiles')) {
- let path = attachment.getFilePath();
let ext = Zotero.File.getExtension(path);
let fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(parentItem);
let newName = fileBaseName + (ext ? '.' + ext : '');
@@ -370,7 +387,14 @@ Zotero.RecognizePDF = new function () {
await attachment.saveTx();
}
- _newItems.set(parentItem, parentItem.dateModified);
+ _newItems.set(
+ parentItem,
+ {
+ originalTitle,
+ originalFilename,
+ dateModified: parentItem.dateModified
+ }
+ );
return parentItem;
}