Return a Zotero.Item from all Zotero.Attachments methods
These previously returned an itemID, but now that new saved items can be edited without a refetch, they should just return the new item. (Possible I missed a few spots where these are called.)
This commit is contained in:
parent
5a2ec43de1
commit
2154673dd3
7 changed files with 43 additions and 54 deletions
|
@ -39,7 +39,7 @@ Zotero.Attachments = new function(){
|
||||||
* @param {Integer} [options.libraryID]
|
* @param {Integer} [options.libraryID]
|
||||||
* @param {Integer[]|String[]} [options.parentItemID] - Parent item to add item to
|
* @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 {Integer[]} [options.collections] - Collection keys or ids to add new item to
|
||||||
* @return {Promise}
|
* @return {Promise<Zotero.Item>}
|
||||||
*/
|
*/
|
||||||
this.importFromFile = Zotero.Promise.coroutine(function* (options) {
|
this.importFromFile = Zotero.Promise.coroutine(function* (options) {
|
||||||
Zotero.debug('Importing attachment from file');
|
Zotero.debug('Importing attachment from file');
|
||||||
|
@ -59,10 +59,10 @@ Zotero.Attachments = new function(){
|
||||||
throw new Error("parentItemID and collections cannot both be provided");
|
throw new Error("parentItemID and collections cannot both be provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
var itemID, newFile, contentType;
|
var attachmentItem, itemID, newFile, contentType;
|
||||||
yield Zotero.DB.executeTransaction(function* () {
|
return Zotero.DB.executeTransaction(function* () {
|
||||||
// Create a new attachment
|
// Create a new attachment
|
||||||
var attachmentItem = new Zotero.Item('attachment');
|
attachmentItem = new Zotero.Item('attachment');
|
||||||
if (parentItemID) {
|
if (parentItemID) {
|
||||||
let {libraryID: parentLibraryID, key: parentKey} =
|
let {libraryID: parentLibraryID, key: parentKey} =
|
||||||
Zotero.Items.getLibraryAndKeyFromID(parentItemID);
|
Zotero.Items.getLibraryAndKeyFromID(parentItemID);
|
||||||
|
@ -77,8 +77,7 @@ Zotero.Attachments = new function(){
|
||||||
if (collections) {
|
if (collections) {
|
||||||
attachmentItem.setCollections(collections);
|
attachmentItem.setCollections(collections);
|
||||||
}
|
}
|
||||||
itemID = yield attachmentItem.save();
|
yield attachmentItem.save();
|
||||||
attachmentItem = yield Zotero.Items.getAsync(itemID);
|
|
||||||
|
|
||||||
// Create directory for attachment files within storage directory
|
// Create directory for attachment files within storage directory
|
||||||
destDir = yield this.createDirectoryForItem(attachmentItem);
|
destDir = yield this.createDirectoryForItem(attachmentItem);
|
||||||
|
@ -97,13 +96,12 @@ Zotero.Attachments = new function(){
|
||||||
yield attachmentItem.save();
|
yield attachmentItem.save();
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
.then(function () {
|
.then(function () {
|
||||||
return _postProcessFile(itemID, newFile, contentType);
|
return _postProcessFile(attachmentItem.id, newFile, contentType);
|
||||||
})
|
})
|
||||||
.catch(function (e) {
|
.catch(function (e) {
|
||||||
Zotero.debug(e, 1);
|
Zotero.logError(e);
|
||||||
var msg = "Failed importing file " + file.path;
|
var msg = "Failed importing file " + file.path;
|
||||||
Components.utils.reportError(msg);
|
Zotero.logError(msg);
|
||||||
Zotero.debug(msg, 1);
|
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
try {
|
try {
|
||||||
|
@ -112,12 +110,12 @@ Zotero.Attachments = new function(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
Zotero.debug(e, 1);
|
Zotero.logError(e);
|
||||||
}
|
}
|
||||||
|
}.bind(this))
|
||||||
throw e;
|
.then(function () {
|
||||||
}.bind(this));
|
return attachmentItem;
|
||||||
return itemID;
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -155,7 +153,7 @@ Zotero.Attachments = new function(){
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'parentItemID'
|
* @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'parentItemID'
|
||||||
* @return {Promise}
|
* @return {Promise<Zotero.Item>}
|
||||||
*/
|
*/
|
||||||
this.importSnapshotFromFile = Zotero.Promise.coroutine(function* (options) {
|
this.importSnapshotFromFile = Zotero.Promise.coroutine(function* (options) {
|
||||||
Zotero.debug('Importing snapshot from file');
|
Zotero.debug('Importing snapshot from file');
|
||||||
|
@ -171,10 +169,10 @@ Zotero.Attachments = new function(){
|
||||||
throw new Error("parentItemID not provided");
|
throw new Error("parentItemID not provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
var itemID, destDir, newFile;
|
var attachmentItem, itemID, destDir, newFile;
|
||||||
yield Zotero.DB.executeTransaction(function* () {
|
return Zotero.DB.executeTransaction(function* () {
|
||||||
// Create a new attachment
|
// Create a new attachment
|
||||||
var attachmentItem = new Zotero.Item('attachment');
|
attachmentItem = new Zotero.Item('attachment');
|
||||||
let {libraryID, key: parentKey} = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
|
let {libraryID, key: parentKey} = Zotero.Items.getLibraryAndKeyFromID(parentItemID);
|
||||||
attachmentItem.libraryID = libraryID;
|
attachmentItem.libraryID = libraryID;
|
||||||
attachmentItem.setField('title', title);
|
attachmentItem.setField('title', title);
|
||||||
|
@ -188,7 +186,6 @@ Zotero.Attachments = new function(){
|
||||||
// create a proper item, but at the moment this is only called by
|
// create a proper item, but at the moment this is only called by
|
||||||
// translate.js, which sets the metadata fields itself
|
// translate.js, which sets the metadata fields itself
|
||||||
itemID = yield attachmentItem.save();
|
itemID = yield attachmentItem.save();
|
||||||
attachmentItem = yield Zotero.Items.getAsync(itemID)
|
|
||||||
|
|
||||||
destDir = this.getStorageDirectory(attachmentItem);
|
destDir = this.getStorageDirectory(attachmentItem);
|
||||||
yield _moveOrphanedDirectory(destDir);
|
yield _moveOrphanedDirectory(destDir);
|
||||||
|
@ -205,7 +202,7 @@ Zotero.Attachments = new function(){
|
||||||
return _postProcessFile(itemID, newFile, contentType);
|
return _postProcessFile(itemID, newFile, contentType);
|
||||||
})
|
})
|
||||||
.catch(function (e) {
|
.catch(function (e) {
|
||||||
Zotero.debug(e, 1);
|
Zotero.logError(e);
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
try {
|
try {
|
||||||
|
@ -214,13 +211,12 @@ Zotero.Attachments = new function(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
Zotero.debug(e, 1);
|
Zotero.logError(e, 1);
|
||||||
}
|
}
|
||||||
|
}.bind(this))
|
||||||
throw e;
|
.then(function () {
|
||||||
}.bind(this));
|
return attachmentItem;
|
||||||
|
});
|
||||||
return itemID;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -361,7 +357,6 @@ Zotero.Attachments = new function(){
|
||||||
destFile.append(fileName);
|
destFile.append(fileName);
|
||||||
|
|
||||||
// Refetch item to update path
|
// Refetch item to update path
|
||||||
attachmentItem = yield Zotero.Items.getAsync(itemID);
|
|
||||||
attachmentItem.attachmentPath = Zotero.Attachments.getPath(
|
attachmentItem.attachmentPath = Zotero.Attachments.getPath(
|
||||||
destFile, Zotero.Attachments.LINK_MODE_IMPORTED_URL
|
destFile, Zotero.Attachments.LINK_MODE_IMPORTED_URL
|
||||||
);
|
);
|
||||||
|
@ -496,7 +491,7 @@ Zotero.Attachments = new function(){
|
||||||
* TODO: what if called on file:// document?
|
* TODO: what if called on file:// document?
|
||||||
*
|
*
|
||||||
* @param {Object} options - 'document', 'parentItemID', 'collections'
|
* @param {Object} options - 'document', 'parentItemID', 'collections'
|
||||||
* @return {Promise}
|
* @return {Promise<Zotero.Item>}
|
||||||
*/
|
*/
|
||||||
this.linkFromDocument = Zotero.Promise.coroutine(function* (options) {
|
this.linkFromDocument = Zotero.Promise.coroutine(function* (options) {
|
||||||
Zotero.debug('Linking attachment from document');
|
Zotero.debug('Linking attachment from document');
|
||||||
|
@ -653,7 +648,6 @@ Zotero.Attachments = new function(){
|
||||||
var destFile = destDir.clone();
|
var destFile = destDir.clone();
|
||||||
destFile.append(fileName);
|
destFile.append(fileName);
|
||||||
|
|
||||||
attachmentItem = yield Zotero.Items.getAsync(itemID);
|
|
||||||
attachmentItem.attachmentPath = this.getPath(
|
attachmentItem.attachmentPath = this.getPath(
|
||||||
destFile, Zotero.Attachments.LINK_MODE_IMPORTED_URL
|
destFile, Zotero.Attachments.LINK_MODE_IMPORTED_URL
|
||||||
);
|
);
|
||||||
|
|
|
@ -2008,13 +2008,13 @@ Zotero.CollectionTreeView.prototype.drop = Zotero.Promise.coroutine(function* (r
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dropEffect == 'link') {
|
if (dropEffect == 'link') {
|
||||||
var itemID = yield Zotero.Attachments.linkFromFile({
|
yield Zotero.Attachments.linkFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
collections: [parentCollectionID]
|
collections: [parentCollectionID]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var itemID = yield Zotero.Attachments.importFromFile({
|
yield Zotero.Attachments.importFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
libraryID: targetLibraryID,
|
libraryID: targetLibraryID,
|
||||||
collections: [parentCollectionID]
|
collections: [parentCollectionID]
|
||||||
|
|
|
@ -3080,14 +3080,14 @@ Zotero.ItemTreeView.prototype.drop = Zotero.Promise.coroutine(function* (row, or
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dropEffect == 'link') {
|
if (dropEffect == 'link') {
|
||||||
var itemID = yield Zotero.Attachments.linkFromFile({
|
yield Zotero.Attachments.linkFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
parentItemID: parentItemID,
|
parentItemID: parentItemID,
|
||||||
collections: parentCollectionID ? [parentCollectionID] : undefined
|
collections: parentCollectionID ? [parentCollectionID] : undefined
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var itemID = yield Zotero.Attachments.importFromFile({
|
yield Zotero.Attachments.importFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
libraryID: targetLibraryID,
|
libraryID: targetLibraryID,
|
||||||
parentItemID: parentItemID,
|
parentItemID: parentItemID,
|
||||||
|
|
|
@ -3282,16 +3282,15 @@ var ZoteroPane = new function()
|
||||||
while (files.hasMoreElements()){
|
while (files.hasMoreElements()){
|
||||||
var file = files.getNext();
|
var file = files.getNext();
|
||||||
file.QueryInterface(Components.interfaces.nsILocalFile);
|
file.QueryInterface(Components.interfaces.nsILocalFile);
|
||||||
let attachmentID;
|
|
||||||
if (link) {
|
if (link) {
|
||||||
attachmentID = yield Zotero.Attachments.linkFromFile({
|
yield Zotero.Attachments.linkFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
parentItemID: parentItemID,
|
parentItemID: parentItemID,
|
||||||
collections: collection ? [collection] : undefined
|
collections: collection ? [collection] : undefined
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
attachmentID = yield Zotero.Attachments.importFromFile({
|
yield Zotero.Attachments.importFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
libraryID: libraryID,
|
libraryID: libraryID,
|
||||||
parentItemID: parentItemID,
|
parentItemID: parentItemID,
|
||||||
|
|
|
@ -27,16 +27,15 @@ describe("Zotero.Attachments", function() {
|
||||||
var parentItemID = yield item.saveTx();
|
var parentItemID = yield item.saveTx();
|
||||||
|
|
||||||
// Create attachment and compare content
|
// Create attachment and compare content
|
||||||
var itemID = yield Zotero.Attachments.importFromFile({
|
var item = yield Zotero.Attachments.importFromFile({
|
||||||
file: tmpFile,
|
file: tmpFile,
|
||||||
parentItemID: parentItemID
|
parentItemID: parentItemID
|
||||||
});
|
});
|
||||||
var item = yield Zotero.Items.getAsync(itemID);
|
|
||||||
var storedFile = item.getFile();
|
var storedFile = item.getFile();
|
||||||
assert.equal((yield Zotero.File.getContentsAsync(storedFile)), contents);
|
assert.equal((yield Zotero.File.getContentsAsync(storedFile)), contents);
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
yield Zotero.Items.erase(itemID);
|
yield Zotero.Items.erase(item.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create a top-level attachment from a PNG file", function* () {
|
it("should create a top-level attachment from a PNG file", function* () {
|
||||||
|
@ -45,15 +44,14 @@ describe("Zotero.Attachments", function() {
|
||||||
var contents = yield Zotero.File.getBinaryContentsAsync(file);
|
var contents = yield Zotero.File.getBinaryContentsAsync(file);
|
||||||
|
|
||||||
// Create attachment and compare content
|
// Create attachment and compare content
|
||||||
var itemID = yield Zotero.Attachments.importFromFile({
|
var item = yield Zotero.Attachments.importFromFile({
|
||||||
file: file
|
file: file
|
||||||
});
|
});
|
||||||
var item = yield Zotero.Items.getAsync(itemID);
|
|
||||||
var storedFile = item.getFile();
|
var storedFile = item.getFile();
|
||||||
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
yield Zotero.Items.erase(itemID);
|
yield Zotero.Items.erase(item.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create a top-level attachment from a PNG file in a collection", function* () {
|
it("should create a top-level attachment from a PNG file in a collection", function* () {
|
||||||
|
@ -64,16 +62,15 @@ describe("Zotero.Attachments", function() {
|
||||||
var collection = yield createDataObject('collection');
|
var collection = yield createDataObject('collection');
|
||||||
|
|
||||||
// Create attachment and compare content
|
// Create attachment and compare content
|
||||||
var itemID = yield Zotero.Attachments.importFromFile({
|
var item = yield Zotero.Attachments.importFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
collections: [collection.id]
|
collections: [collection.id]
|
||||||
});
|
});
|
||||||
var item = yield Zotero.Items.getAsync(itemID);
|
|
||||||
var storedFile = item.getFile();
|
var storedFile = item.getFile();
|
||||||
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
yield Zotero.Items.erase(itemID);
|
yield Zotero.Items.erase(item.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create a child attachment from a PNG file", function* () {
|
it("should create a child attachment from a PNG file", function* () {
|
||||||
|
@ -86,16 +83,15 @@ describe("Zotero.Attachments", function() {
|
||||||
var parentItemID = yield item.saveTx();
|
var parentItemID = yield item.saveTx();
|
||||||
|
|
||||||
// Create attachment and compare content
|
// Create attachment and compare content
|
||||||
var itemID = yield Zotero.Attachments.importFromFile({
|
var item = yield Zotero.Attachments.importFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
parentItemID: parentItemID
|
parentItemID: parentItemID
|
||||||
});
|
});
|
||||||
var item = yield Zotero.Items.getAsync(itemID);
|
|
||||||
var storedFile = item.getFile();
|
var storedFile = item.getFile();
|
||||||
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
yield Zotero.Items.erase(itemID);
|
yield Zotero.Items.erase(item.id);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -236,7 +236,7 @@ describe("Zotero.CollectionTreeView", function() {
|
||||||
});
|
});
|
||||||
var file = getTestDataDirectory();
|
var file = getTestDataDirectory();
|
||||||
file.append('test.png');
|
file.append('test.png');
|
||||||
var attachmentID = yield Zotero.Attachments.importFromFile({
|
yield Zotero.Attachments.importFromFile({
|
||||||
file: file,
|
file: file,
|
||||||
parentItemID: item.id
|
parentItemID: item.id
|
||||||
});
|
});
|
||||||
|
|
|
@ -24,12 +24,12 @@ describe.skip("PDF Recognition", function() {
|
||||||
// Import the PDF
|
// Import the PDF
|
||||||
var testdir = getTestDataDirectory();
|
var testdir = getTestDataDirectory();
|
||||||
testdir.append("recognizePDF_test_DOI.pdf");
|
testdir.append("recognizePDF_test_DOI.pdf");
|
||||||
var id = yield Zotero.Attachments.importFromFile({
|
var item = yield Zotero.Attachments.importFromFile({
|
||||||
file: testdir
|
file: testdir
|
||||||
});
|
});
|
||||||
|
|
||||||
// Recognize the PDF
|
// Recognize the PDF
|
||||||
win.ZoteroPane.selectItem(id);
|
win.ZoteroPane.selectItem(item.id);
|
||||||
win.Zotero_RecognizePDF.recognizeSelected();
|
win.Zotero_RecognizePDF.recognizeSelected();
|
||||||
|
|
||||||
return waitForItemEvent("add").then(function(ids) {
|
return waitForItemEvent("add").then(function(ids) {
|
||||||
|
@ -46,12 +46,12 @@ describe.skip("PDF Recognition", function() {
|
||||||
// Import the PDF
|
// Import the PDF
|
||||||
var testdir = getTestDataDirectory();
|
var testdir = getTestDataDirectory();
|
||||||
testdir.append("recognizePDF_test_GS.pdf");
|
testdir.append("recognizePDF_test_GS.pdf");
|
||||||
var id = yield Zotero.Attachments.importFromFile({
|
var item = yield Zotero.Attachments.importFromFile({
|
||||||
file: testdir
|
file: testdir
|
||||||
});
|
});
|
||||||
|
|
||||||
// Recognize the PDF
|
// Recognize the PDF
|
||||||
win.ZoteroPane.selectItem(id);
|
win.ZoteroPane.selectItem(item.id);
|
||||||
win.Zotero_RecognizePDF.recognizeSelected();
|
win.Zotero_RecognizePDF.recognizeSelected();
|
||||||
|
|
||||||
return waitForItemEvent("add").then(function(ids) {
|
return waitForItemEvent("add").then(function(ids) {
|
||||||
|
|
Loading…
Reference in a new issue