Restore attachment title and filename when undoing Retrieve Metadata

This commit is contained in:
Dan Stillman 2018-03-09 01:07:57 -05:00
parent f5a2534169
commit da30f9e25c

View File

@ -135,13 +135,12 @@ Zotero.RecognizePDF = new function () {
this.canUnrecognize = function (item) { this.canUnrecognize = function (item) {
var threshold = UNRECOGNIZE_TIMEOUT; var { dateModified } = _newItems.get(item) || {};
var added = _newItems.get(item);
// Item must have been recognized recently, must not have been modified since it was // 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 // created, and must have only one attachment and no other children
if (!added if (!dateModified
|| Zotero.Date.sqlToDate(added, true) < new Date() - threshold || Zotero.Date.sqlToDate(dateModified, true) < new Date() - UNRECOGNIZE_TIMEOUT
|| item.dateModified != added || item.dateModified != dateModified
|| item.numAttachments(true) != 1 || item.numAttachments(true) != 1
|| item.numChildren(true) != 1) { || item.numChildren(true) != 1) {
_newItems.delete(item); _newItems.delete(item);
@ -160,7 +159,22 @@ Zotero.RecognizePDF = new function () {
this.unrecognize = async function (item) { this.unrecognize = async function (item) {
var { originalTitle, originalFilename } = _newItems.get(item);
var attachment = Zotero.Items.get(item.getAttachments()[0]); 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 () { return Zotero.DB.executeTransaction(async function () {
let collections = item.getCollections(); let collections = item.getCollections();
attachment.parentItemID = null attachment.parentItemID = null
@ -174,7 +188,7 @@ Zotero.RecognizePDF = new function () {
this.report = async function (item) { this.report = async function (item) {
var attachment = Zotero.Items.get(item.getAttachments()[0]); var attachment = Zotero.Items.get(item.getAttachments()[0]);
var filePath = await attachment.getFilePath(); var filePath = attachment.getFilePath();
if (!filePath || !await OS.File.exists(filePath)) { if (!filePath || !await OS.File.exists(filePath)) {
throw new Error("File not found when reporting metadata"); throw new Error("File not found when reporting metadata");
} }
@ -355,9 +369,12 @@ Zotero.RecognizePDF = new function () {
await attachment.save(); 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 // Rename attachment file to match new metadata
if (Zotero.Prefs.get('autoRenameFiles')) { if (Zotero.Prefs.get('autoRenameFiles')) {
let path = attachment.getFilePath();
let ext = Zotero.File.getExtension(path); let ext = Zotero.File.getExtension(path);
let fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(parentItem); let fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(parentItem);
let newName = fileBaseName + (ext ? '.' + ext : ''); let newName = fileBaseName + (ext ? '.' + ext : '');
@ -370,7 +387,14 @@ Zotero.RecognizePDF = new function () {
await attachment.saveTx(); await attachment.saveTx();
} }
_newItems.set(parentItem, parentItem.dateModified); _newItems.set(
parentItem,
{
originalTitle,
originalFilename,
dateModified: parentItem.dateModified
}
);
return parentItem; return parentItem;
} }