Fix snapshots of pages without extensions on Linux -- nsIMIMEService::getPrimaryExtension() doesn't work on Linux

Better error recovery in attachment methods
This commit is contained in:
Dan Stillman 2006-12-05 16:49:31 +00:00
parent a16552cd72
commit 02f205e919

View File

@ -77,6 +77,7 @@ Zotero.Attachments = new function(){
// hmph // hmph
Zotero.DB.rollbackTransaction(); Zotero.DB.rollbackTransaction();
try {
// Clean up // Clean up
if (itemID){ if (itemID){
var itemDir = Zotero.getStorageDirectory(); var itemDir = Zotero.getStorageDirectory();
@ -85,6 +86,9 @@ Zotero.Attachments = new function(){
itemDir.remove(true); itemDir.remove(true);
} }
} }
}
catch (e) {}
throw (e); throw (e);
} }
return itemID; return itemID;
@ -145,6 +149,7 @@ Zotero.Attachments = new function(){
catch (e){ catch (e){
Zotero.DB.rollbackTransaction(); Zotero.DB.rollbackTransaction();
try {
// Clean up // Clean up
if (itemID){ if (itemID){
var itemDir = Zotero.getStorageDirectory(); var itemDir = Zotero.getStorageDirectory();
@ -153,6 +158,9 @@ Zotero.Attachments = new function(){
itemDir.remove(true); itemDir.remove(true);
} }
} }
}
catch (e) {}
throw (e); throw (e);
} }
return itemID; return itemID;
@ -226,6 +234,19 @@ Zotero.Attachments = new function(){
} }
catch (e){ catch (e){
Zotero.DB.rollbackTransaction(); Zotero.DB.rollbackTransaction();
try {
// Clean up
if (itemID) {
var destDir = Zotero.getStorageDirectory();
destDir.append(itemID);
if (destDir.exists()) {
destDir.remove(true);
}
}
}
catch (e) {}
throw (e); throw (e);
} }
} }
@ -305,6 +326,7 @@ Zotero.Attachments = new function(){
Zotero.DB.beginTransaction(); Zotero.DB.beginTransaction();
try {
// Create a new attachment // Create a new attachment
var attachmentItem = Zotero.Items.getNewItemByType(Zotero.ItemTypes.getID('attachment')); var attachmentItem = Zotero.Items.getNewItemByType(Zotero.ItemTypes.getID('attachment'));
attachmentItem.setField('title', title); attachmentItem.setField('title', title);
@ -337,6 +359,7 @@ Zotero.Attachments = new function(){
file.append(fileName); file.append(fileName);
wbp.progressListener = new Zotero.WebProgressFinishListener(function(){ wbp.progressListener = new Zotero.WebProgressFinishListener(function(){
try {
_addToDB(file, url, title, Zotero.Attachments.LINK_MODE_IMPORTED_URL, mimeType, _addToDB(file, url, title, Zotero.Attachments.LINK_MODE_IMPORTED_URL, mimeType,
charsetID, sourceItemID, itemID); charsetID, sourceItemID, itemID);
@ -350,12 +373,34 @@ Zotero.Attachments = new function(){
} }
Zotero.DB.commitTransaction(); Zotero.DB.commitTransaction();
}
catch (e) {
Zotero.DB.rollbackTransaction();
}
Zotero.Fulltext.indexDocument(document, itemID); Zotero.Fulltext.indexDocument(document, itemID);
}); });
wbp.saveDocument(document, file, destDir, mimeType, encodingFlags, false); wbp.saveDocument(document, file, destDir, mimeType, encodingFlags, false);
} }
catch (e) {
Zotero.DB.rollbackTransaction();
try {
// Clean up
if (itemID) {
var destDir = Zotero.getStorageDirectory();
destDir.append(itemID);
if (destDir.exists()) {
destDir.remove(true);
}
}
}
catch (e) {}
throw (e);
}
}
function _getFileNameFromURL(url, mimeType){ function _getFileNameFromURL(url, mimeType){
@ -368,10 +413,14 @@ Zotero.Attachments = new function(){
} }
if (mimeType){ if (mimeType){
try {
var ext = Components.classes["@mozilla.org/mime;1"] var ext = Components.classes["@mozilla.org/mime;1"]
.getService(Components.interfaces.nsIMIMEService) .getService(Components.interfaces.nsIMIMEService)
.getPrimaryExtension(mimeType, nsIURL.fileExt ? nsIURL.fileExt : null); .getPrimaryExtension(mimeType, nsIURL.fileExt ? nsIURL.fileExt : null);
} }
// getPrimaryExtension doesn't work on Linux
catch (e) {}
}
return nsIURL.host + (ext ? '.' + ext : ''); return nsIURL.host + (ext ? '.' + ext : '');
} }