Support additional data from OA PDF lookup service

Handle an array of objects with 'url' and 'version' rather than just an
array of URLs.

Also:

- Don't throw an error from addOpenAccessPDF() if there's an error from
  getOpenAccessPDFURLs()
- Make addPDFFromURLs() a separate function so URL lookup can be done
  separately from download
This commit is contained in:
Dan Stillman 2018-07-27 00:52:44 -04:00
parent 687b1b300c
commit cdda572728
2 changed files with 27 additions and 3 deletions

View file

@ -889,6 +889,7 @@ Zotero.Attachments = new function(){
*/
this.downloadFirstAvailableFile = async function (urls, path, options) {
var url;
urls = [...urls];
while (url = urls.shift()) {
try {
await this.downloadFile(url, path, options);
@ -918,15 +919,34 @@ Zotero.Attachments = new function(){
return false;
}
var urls = await Zotero.Utilities.Internal.getOpenAccessPDFURLs(doi);
if (!urls.length) {
try {
var urlObjects = await Zotero.Utilities.Internal.getOpenAccessPDFURLs(doi);
}
catch (e) {
Zotero.logError(e);
return false;
}
if (!urlObjects.length) {
return false;
}
return this.addPDFFromURLs(item, urlObjects);
};
/**
* Try to add a PDF to an item from a set of possible URLs
*
* @param {Zotero.Item} item
* @param {Object[]} urlObjects - Array of objects with 'url' and 'version'
* @return {Zotero.Item|false} - New attachment item, or false if unsuccessful
*/
this.addPDFFromURLs = async function (item, urlObjects) {
var fileBaseName = this.getFileBaseNameFromItem(item);
var tmpDir;
var tmpFile;
var attachmentItem = false;
var urls = urlObjects.map(o => o.url);
try {
tmpDir = (await this.createTemporaryStorageDirectory()).path;
tmpFile = OS.Path.join(tmpDir, fileBaseName + '.pdf');
@ -934,13 +954,15 @@ Zotero.Attachments = new function(){
urls, tmpFile, { isPDF: true }
);
if (url) {
let version = urlObjects[urls.indexOf(url)].version;
attachmentItem = await this.createURLAttachmentFromTemporaryStorageDirectory({
directory: tmpDir,
libraryID: item.libraryID,
filename: OS.Path.basename(tmpFile),
url,
contentType: 'application/pdf',
parentItemID: item.id
parentItemID: item.id,
articleVersion: version
});
}
else {

View file

@ -955,6 +955,8 @@ Zotero.Utilities.Internal = {
});
var urls = req.response;
Zotero.debug(`Found ${urls.length} ${Zotero.Utilities.pluralize(urls.length, ['URL', 'URLs'])}`);
// Handle older URL-only format
urls = urls.map(o => typeof o == 'string' ? { url: o } : o);
return urls;
},