Retrieve Metadata & Create Parent: Use autoRenameFiles.fileTypes
(#4488)
This commit is contained in:
parent
8209ee6279
commit
30cb2e1721
6 changed files with 68 additions and 13 deletions
|
@ -2503,6 +2503,12 @@ Zotero.Attachments = new function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
this.shouldAutoRenameAttachment = function (attachment) {
|
||||||
|
return Zotero.Attachments.shouldAutoRenameFile(attachment.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE)
|
||||||
|
&& Zotero.Attachments.isRenameAllowedForType(attachment.attachmentContentType);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
this.getRenamedFileBaseNameIfAllowedType = async function (parentItem, file) {
|
this.getRenamedFileBaseNameIfAllowedType = async function (parentItem, file) {
|
||||||
var contentType = file.endsWith('.pdf')
|
var contentType = file.endsWith('.pdf')
|
||||||
// Don't bother reading file if there's a .pdf extension
|
// Don't bother reading file if there's a .pdf extension
|
||||||
|
|
|
@ -3907,9 +3907,7 @@ Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs
|
||||||
// file is being renamed, give it a default title ("PDF", "Webpage", etc.)
|
// file is being renamed, give it a default title ("PDF", "Webpage", etc.)
|
||||||
let isFirstOfType = this.parentItemID
|
let isFirstOfType = this.parentItemID
|
||||||
&& this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1;
|
&& this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1;
|
||||||
let isBeingRenamed = ignoreAutoRenamePrefs
|
let isBeingRenamed = ignoreAutoRenamePrefs || Zotero.Attachments.shouldAutoRenameAttachment(this);
|
||||||
|| (Zotero.Attachments.shouldAutoRenameFile(this.isLinkedFileAttachment())
|
|
||||||
&& Zotero.Attachments.isRenameAllowedForType(this.attachmentContentType));
|
|
||||||
if (isFirstOfType && isBeingRenamed) {
|
if (isFirstOfType && isBeingRenamed) {
|
||||||
let defaultTitle = this._getDefaultTitleForAttachmentContentType();
|
let defaultTitle = this._getDefaultTitleForAttachmentContentType();
|
||||||
if (defaultTitle !== null) {
|
if (defaultTitle !== null) {
|
||||||
|
|
|
@ -292,7 +292,7 @@ Zotero.RecognizeDocument = new function () {
|
||||||
var originalFilename = PathUtils.filename(path);
|
var originalFilename = PathUtils.filename(path);
|
||||||
|
|
||||||
// Rename attachment file to match new metadata
|
// Rename attachment file to match new metadata
|
||||||
if (Zotero.Attachments.shouldAutoRenameFile(attachment.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE)) {
|
if (Zotero.Attachments.shouldAutoRenameAttachment(attachment)) {
|
||||||
let ext = Zotero.File.getExtension(path);
|
let ext = Zotero.File.getExtension(path);
|
||||||
let fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(parentItem, { attachmentTitle: originalTitle });
|
let fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(parentItem, { attachmentTitle: originalTitle });
|
||||||
let newName = fileBaseName + (ext ? '.' + ext : '');
|
let newName = fileBaseName + (ext ? '.' + ext : '');
|
||||||
|
@ -300,11 +300,9 @@ Zotero.RecognizeDocument = new function () {
|
||||||
if (result !== true) {
|
if (result !== true) {
|
||||||
throw new Error("Error renaming " + path);
|
throw new Error("Error renaming " + path);
|
||||||
}
|
}
|
||||||
}
|
attachment.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true });
|
||||||
|
|
||||||
// Rename attachment title
|
|
||||||
attachment.setAutoAttachmentTitle();
|
|
||||||
await attachment.saveTx();
|
await attachment.saveTx();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let win = Zotero.getMainWindow();
|
let win = Zotero.getMainWindow();
|
||||||
|
|
|
@ -5343,7 +5343,7 @@ var ZoteroPane = new function()
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let item of items) {
|
for (let item of items) {
|
||||||
if (Zotero.Attachments.shouldAutoRenameFile(item.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE)) {
|
if (Zotero.Attachments.shouldAutoRenameAttachment(item)) {
|
||||||
let path = item.getFilePath();
|
let path = item.getFilePath();
|
||||||
if (!path) {
|
if (!path) {
|
||||||
Zotero.debug('No path for attachment ' + item.key);
|
Zotero.debug('No path for attachment ' + item.key);
|
||||||
|
|
|
@ -238,7 +238,7 @@ describe("Document Recognition", function() {
|
||||||
|
|
||||||
it("should rename a linked file attachment using parent metadata if no existing file attachments and pref enabled", async function () {
|
it("should rename a linked file attachment using parent metadata if no existing file attachments and pref enabled", async function () {
|
||||||
Zotero.Prefs.set('autoRenameFiles.linked', true);
|
Zotero.Prefs.set('autoRenameFiles.linked', true);
|
||||||
var itemTitle = Zotero.Utilities.randomString();;
|
var itemTitle = Zotero.Utilities.randomString();
|
||||||
Zotero.RecognizeDocument.recognizeStub = async function () {
|
Zotero.RecognizeDocument.recognizeStub = async function () {
|
||||||
return createDataObject('item', { title: itemTitle });
|
return createDataObject('item', { title: itemTitle });
|
||||||
};
|
};
|
||||||
|
@ -280,9 +280,43 @@ describe("Document Recognition", function() {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("shouldn't rename or change the title of a file attachment with a disabled type", async function () {
|
||||||
|
Zotero.Prefs.set('autoRenameFiles.fileTypes', 'x-nonexistent/type');
|
||||||
|
|
||||||
|
var itemTitle = Zotero.Utilities.randomString();
|
||||||
|
Zotero.RecognizeDocument.recognizeStub = async function () {
|
||||||
|
return createDataObject('item', { title: itemTitle });
|
||||||
|
};
|
||||||
|
|
||||||
|
var attachment = await importPDFAttachment();
|
||||||
|
assert.equal(attachment.getField('title'), 'test');
|
||||||
|
|
||||||
|
win.ZoteroPane.recognizeSelected();
|
||||||
|
|
||||||
|
var addedIDs = await waitForItemEvent("add");
|
||||||
|
var modifiedIDs = await waitForItemEvent("modify");
|
||||||
|
assert.lengthOf(addedIDs, 1);
|
||||||
|
var item = Zotero.Items.get(addedIDs[0]);
|
||||||
|
assert.equal(item.getField("title"), itemTitle);
|
||||||
|
assert.lengthOf(modifiedIDs, 2);
|
||||||
|
|
||||||
|
// Wait for status to show as complete
|
||||||
|
var progressWindow = getWindows("chrome://zotero/content/progressQueueDialog.xhtml")[0];
|
||||||
|
var completeStr = Zotero.getString("general.finished");
|
||||||
|
while (progressWindow.document.getElementById("label").value != completeStr) {
|
||||||
|
await Zotero.Promise.delay(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The file should not have been renamed
|
||||||
|
assert.equal(attachment.attachmentFilename, 'test.pdf');
|
||||||
|
|
||||||
|
// The title should not have changed
|
||||||
|
assert.equal(attachment.getField('title'), 'test');
|
||||||
|
});
|
||||||
|
|
||||||
it("shouldn't rename a linked file attachment using parent metadata if pref disabled", async function () {
|
it("shouldn't rename a linked file attachment using parent metadata if pref disabled", async function () {
|
||||||
Zotero.Prefs.set('autoRenameFiles.linked', false);
|
Zotero.Prefs.set('autoRenameFiles.linked', false);
|
||||||
var itemTitle = Zotero.Utilities.randomString();;
|
var itemTitle = Zotero.Utilities.randomString();
|
||||||
Zotero.RecognizeDocument.recognizeStub = async function () {
|
Zotero.RecognizeDocument.recognizeStub = async function () {
|
||||||
return createDataObject('item', { title: itemTitle });
|
return createDataObject('item', { title: itemTitle });
|
||||||
};
|
};
|
||||||
|
|
|
@ -1698,7 +1698,26 @@ describe("ZoteroPane", function() {
|
||||||
assert.equal(attachment.getField('title'), Zotero.getString('file-type-pdf'));
|
assert.equal(attachment.getField('title'), Zotero.getString('file-type-pdf'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not rename a linked attachment or set an automatic title when linked file renaming disabled", async function () {
|
it("shouldn't rename or change the title of an attachment with a disabled type", async function () {
|
||||||
|
Zotero.Prefs.set('autoRenameFiles.fileTypes', 'x-nonexistent/type');
|
||||||
|
|
||||||
|
let file = getTestDataDirectory();
|
||||||
|
file.append('test.pdf');
|
||||||
|
let attachment = await Zotero.Attachments.linkFromFile({
|
||||||
|
file,
|
||||||
|
title: 'Attachment title'
|
||||||
|
});
|
||||||
|
assert.equal(attachment.attachmentFilename, 'test.pdf');
|
||||||
|
|
||||||
|
let parent = await createParent();
|
||||||
|
assert.equal(attachment.parentItem, parent);
|
||||||
|
assert.equal(attachment.attachmentFilename, 'test.pdf');
|
||||||
|
assert.equal(attachment.getField('title'), 'Attachment title');
|
||||||
|
|
||||||
|
Zotero.Prefs.clear('autoRenameFiles.fileTypes');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shouldn't rename a linked attachment or set an automatic title when linked file renaming disabled", async function () {
|
||||||
Zotero.Prefs.set('autoRenameFiles.linked', false);
|
Zotero.Prefs.set('autoRenameFiles.linked', false);
|
||||||
|
|
||||||
let file = getTestDataDirectory();
|
let file = getTestDataDirectory();
|
||||||
|
|
Loading…
Reference in a new issue