Skip to content

Instantly share code, notes, and snippets.

@sss
Created April 23, 2011 20:08
Show Gist options
  • Save sss/938927 to your computer and use it in GitHub Desktop.
Save sss/938927 to your computer and use it in GitHub Desktop.
Try an alternate file name when failed to decode the final component of URI in Zotero 2.1.6
Zotero 2.1.6 is failing to capture a web page,
whose URI has the final component which can't be
decoded with decodeURIComponent().
(e.g. URI has the final component in non UTF-8 encoded string)
This patch is a workaround for this against /extension/branches/2.1 .
When failed to decode the final component with decodeURIComponent(),
try base64 of SHA-1 of the final component as an alternate file name.
---
attachments.js | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
Index: chrome/content/zotero/xpcom/attachments.js
===================================================================
--- chrome/content/zotero/xpcom/attachments.js (revision 9080)
+++ chrome/content/zotero/xpcom/attachments.js (working copy)
@@ -1165,8 +1165,26 @@
nsIURL.fileBaseName = nsIURL.fileBaseName + '.' + tld;
}
- // Pass unencoded name to getValidFileName() so that '%20' isn't stripped to '20'
- nsIURL.fileBaseName = Zotero.File.getValidFileName(decodeURIComponent(nsIURL.fileBaseName));
+ try {
+ // Pass unencoded name to getValidFileName() so that '%20' isn't stripped to '20'
+ nsIURL.fileBaseName = Zotero.File.getValidFileName(decodeURIComponent(nsIURL.fileBaseName));
+ }
+ catch (e) {
+ // If we got a error with decoding fileBaseName, try SHA-1 of fileBaseName.
+ if (e.name == 'URIError') {
+ var cryptoHash = Cc["@mozilla.org/security/hash;1"].
+ createInstance(Ci.nsICryptoHash);
+ cryptoHash.init(Ci.nsICryptoHash.SHA1);
+ var inputStream = Cc["@mozilla.org/io/string-input-stream;1"].
+ createInstance(Ci.nsIStringInputStream);
+ inputStream.setData(nsIURL.fileBaseName, nsIURL.fileBaseName.length);
+ cryptoHash.updateFromStream(inputStream, -1);
+ nsIURL.fileBaseName = Zotero.File.getValidFileName(cryptoHash.finish(true));
+ }
+ else {
+ throw e;
+ }
+ }
return decodeURIComponent(nsIURL.fileName);
}
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment