Restore attachment title and filename when undoing Retrieve Metadata
This commit is contained in:
parent
f5a2534169
commit
da30f9e25c
1 changed files with 32 additions and 8 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue