diff --git a/chrome/content/zotero/overlay.js b/chrome/content/zotero/overlay.js index 8a76eabca..d26901abe 100644 --- a/chrome/content/zotero/overlay.js +++ b/chrome/content/zotero/overlay.js @@ -1566,7 +1566,7 @@ var ZoteroPane = new function() Zotero.Attachments.linkFromDocument(window.content.document, itemID, false, parentCollectionID); } else { - Zotero.Attachments.importFromDocument(window.content.document, itemID, false, false, parentCollectionID); + Zotero.Attachments.importFromDocument(window.content.document, itemID, false, parentCollectionID); } } // Give progress window time to appear diff --git a/chrome/content/zotero/webpagedump/domsaver.js b/chrome/content/zotero/webpagedump/domsaver.js index 221f90771..2b8f60c3d 100644 --- a/chrome/content/zotero/webpagedump/domsaver.js +++ b/chrome/content/zotero/webpagedump/domsaver.js @@ -873,8 +873,11 @@ var wpdDOMSaver = { // wrapper HTML File which references "aDocument" // ("aFileName" is the filename without(!) extension) saveDocumentFile : function(aDocument,aFileName) - { - dump("[wpdDOMSaver.saveDocumentFile]: "+aFileName+"\n"); + { + dump("[wpdDOMSaver.saveDocumentFile]: "+aFileName+"\n"); + + return this.download(this.currentURL,true) + /* Wrapper file disabled by Dan S. for Zotero var aFileURL = aDocument.location.href; if ( !aFileName ) aFileName = "file" + Math.random().toString(); @@ -887,11 +890,13 @@ var wpdDOMSaver = { var HTMLText = ''; } - var HTMLFile = this.currentDir + aFileName + ".html"; + var HTMLFile = this.currentDir + aFileName + ".html"; + if (!wpdCommon.writeFile(HTMLText,HTMLFile)) wpdCommon.addError("[wpdDOMSaver.saveDocumentFile]: could not write HTML wrapper for "+aFileName+"\n"); - - return aFileName + ".html"; + + return aFileName + ".html"; + */ }, // save the CSS Stylesheets of "aDocument" as "aFileName" and diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index e1bbd1073..af20f64a5 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -33,6 +33,7 @@ Zotero.Attachments = new function(){ this.linkFromURL = linkFromURL; this.linkFromDocument = linkFromDocument; this.importFromDocument = importFromDocument; + this.getFileBaseNameFromItem = getFileBaseNameFromItem; this.createDirectoryForItem = createDirectoryForItem; this.getPath = getPath; @@ -168,7 +169,7 @@ Zotero.Attachments = new function(){ } - function importFromURL(url, sourceItemID, forceTitle, forceFileName, parentCollectionIDs){ + function importFromURL(url, sourceItemID, forceTitle, forceFileBaseName, parentCollectionIDs){ Zotero.debug('Importing attachment from URL'); Zotero.Utilities.HTTP.doHead(url, function(obj){ @@ -193,7 +194,7 @@ Zotero.Attachments = new function(){ browser.addEventListener("pageshow", function(){ try { Zotero.Attachments.importFromDocument(browser.contentDocument, - sourceItemID, forceTitle, forceFileName, parentCollectionIDs); + sourceItemID, forceTitle, parentCollectionIDs); } finally { browser.removeEventListener("pageshow", arguments.callee, true); @@ -216,7 +217,14 @@ Zotero.Attachments = new function(){ // Otherwise use a remote web page persist else { - var fileName = _getFileNameFromURL(url, mimeType); + if (forceFileBaseName) { + var ext = _getExtensionFromURL(url, mimeType); + var fileName = forceFileBaseName + (ext != '' ? '.' + ext : ''); + } + else { + var fileName = _getFileNameFromURL(url, mimeType); + } + var title = forceTitle ? forceTitle : fileName; const nsIWBP = Components.interfaces.nsIWebBrowserPersist; @@ -405,7 +413,7 @@ Zotero.Attachments = new function(){ * * Returns itemID of attachment */ - function importFromDocument(document, sourceItemID, forceTitle, forceFileName, parentCollectionIDs) { + function importFromDocument(document, sourceItemID, forceTitle, parentCollectionIDs) { Zotero.debug('Importing attachment from document'); var url = document.location.href; @@ -643,6 +651,75 @@ Zotero.Attachments = new function(){ */ + /* + * Returns a formatted string to use as the basename of an attachment + * based on the metadata of the specified item and a format string + * + * (Optional) |formatString| specifies the format string -- otherwise + * the 'attachmentRenameFormatString' pref is used + * + * Valid substitution markers: + * + * %c -- firstCreator + * %y -- year (extracted from Date field) + * %t -- title + * + * Fields can be truncated to a certain length by appending an integer + * within curly brackets -- e.g. %t{50} truncates the title to 50 characters + */ + function getFileBaseNameFromItem(itemID, formatString) { + if (!formatString) { + formatString = Zotero.Prefs.get('attachmentRenameFormatString'); + } + + var item = Zotero.Items.get(itemID); + if (!item) { + throw ('Invalid itemID ' + itemID + ' in Zotero.Attachments.getFileBaseNameFromItem()'); + } + + // Replaces the substitution marker with the field value, + // truncating based on the {[0-9]+} modifier if applicable + function rpl(field, str) { + switch (field) { + case 'creator': + field = 'firstCreator'; + var rpl = '%c'; + break; + + case 'title': + var rpl = '%t'; + break; + } + + var f = function(match) { + var value = item.getField(field, false, true); + var chars = match.match(/{([0-9]+)}/); + return (chars) ? value.substr(0, chars[1]) : value; + } + + return str.replace(new RegExp(rpl + "(\{[0-9]+\})?"), f); + } + + // Creator + formatString = rpl('creator', formatString); + + // Year + var year = item.getField('date', true); + if (year) { + year = Zotero.Date.multipartToSQL(year).substr(0, 4); + if (year == '0000') { + year = ''; + } + } + formatString = formatString.replace('%y', year); + + // Title + formatString = rpl('title', formatString); + + return formatString; + } + + /* * Create directory for attachment files within storage directory */ diff --git a/chrome/content/zotero/xpcom/translate.js b/chrome/content/zotero/xpcom/translate.js index 5f60b1a22..80a59c479 100644 --- a/chrome/content/zotero/xpcom/translate.js +++ b/chrome/content/zotero/xpcom/translate.js @@ -1223,7 +1223,8 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) { attachment.url = Zotero.Ingester.ProxyMonitor.properToProxy(attachment.url); } - Zotero.Attachments.importFromURL(attachment.url, myID, title); + var fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(myID); + Zotero.Attachments.importFromURL(attachment.url, myID, title, fileBaseName); } } // links no longer exist, so just don't save them diff --git a/defaults/preferences/zotero.js b/defaults/preferences/zotero.js index 1f0c7a982..de2c7583f 100644 --- a/defaults/preferences/zotero.js +++ b/defaults/preferences/zotero.js @@ -18,6 +18,7 @@ pref("extensions.zotero.reportTranslationFailure",true); pref("extensions.zotero.automaticTags",true); pref("extensions.zotero.fontSize", "1.0"); pref("extensions.zotero.recursiveCollections", false); +pref("extensions.zotero.attachmentRenameFormatString", '%c - %y - %t{50}'); pref("extensions.zotero.lastCreatorFieldMode",0); pref("extensions.zotero.lastAbstractExpand",0);