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:
parent
a16552cd72
commit
02f205e919
|
@ -77,14 +77,18 @@ Zotero.Attachments = new function(){
|
||||||
// hmph
|
// hmph
|
||||||
Zotero.DB.rollbackTransaction();
|
Zotero.DB.rollbackTransaction();
|
||||||
|
|
||||||
// Clean up
|
try {
|
||||||
if (itemID){
|
// Clean up
|
||||||
var itemDir = Zotero.getStorageDirectory();
|
if (itemID){
|
||||||
itemDir.append(itemID);
|
var itemDir = Zotero.getStorageDirectory();
|
||||||
if (itemDir.exists()){
|
itemDir.append(itemID);
|
||||||
itemDir.remove(true);
|
if (itemDir.exists()){
|
||||||
|
itemDir.remove(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (e) {}
|
||||||
|
|
||||||
throw (e);
|
throw (e);
|
||||||
}
|
}
|
||||||
return itemID;
|
return itemID;
|
||||||
|
@ -145,14 +149,18 @@ Zotero.Attachments = new function(){
|
||||||
catch (e){
|
catch (e){
|
||||||
Zotero.DB.rollbackTransaction();
|
Zotero.DB.rollbackTransaction();
|
||||||
|
|
||||||
// Clean up
|
try {
|
||||||
if (itemID){
|
// Clean up
|
||||||
var itemDir = Zotero.getStorageDirectory();
|
if (itemID){
|
||||||
itemDir.append(itemID);
|
var itemDir = Zotero.getStorageDirectory();
|
||||||
if (itemDir.exists()){
|
itemDir.append(itemID);
|
||||||
itemDir.remove(true);
|
if (itemDir.exists()){
|
||||||
|
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,56 +326,80 @@ Zotero.Attachments = new function(){
|
||||||
|
|
||||||
Zotero.DB.beginTransaction();
|
Zotero.DB.beginTransaction();
|
||||||
|
|
||||||
// Create a new attachment
|
try {
|
||||||
var attachmentItem = Zotero.Items.getNewItemByType(Zotero.ItemTypes.getID('attachment'));
|
// Create a new attachment
|
||||||
attachmentItem.setField('title', title);
|
var attachmentItem = Zotero.Items.getNewItemByType(Zotero.ItemTypes.getID('attachment'));
|
||||||
attachmentItem.setField('url', url);
|
attachmentItem.setField('title', title);
|
||||||
attachmentItem.setField('accessDate', "CURRENT_TIMESTAMP");
|
attachmentItem.setField('url', url);
|
||||||
attachmentItem.save();
|
attachmentItem.setField('accessDate', "CURRENT_TIMESTAMP");
|
||||||
var itemID = attachmentItem.getID();
|
attachmentItem.save();
|
||||||
|
var itemID = attachmentItem.getID();
|
||||||
|
|
||||||
// Create a new folder for this item in the storage directory
|
// Create a new folder for this item in the storage directory
|
||||||
var destDir = Zotero.getStorageDirectory();
|
var destDir = Zotero.getStorageDirectory();
|
||||||
destDir.append(itemID);
|
destDir.append(itemID);
|
||||||
destDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
|
destDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
|
||||||
|
|
||||||
var file = Components.classes["@mozilla.org/file/local;1"].
|
var file = Components.classes["@mozilla.org/file/local;1"].
|
||||||
createInstance(Components.interfaces.nsILocalFile);
|
createInstance(Components.interfaces.nsILocalFile);
|
||||||
file.initWithFile(destDir);
|
file.initWithFile(destDir);
|
||||||
|
|
||||||
var fileName = _getFileNameFromURL(url, mimeType);
|
var fileName = _getFileNameFromURL(url, mimeType);
|
||||||
|
|
||||||
// This is a hack to make sure the file is opened in the browser when
|
// This is a hack to make sure the file is opened in the browser when
|
||||||
// we use loadURI(), since Firefox's internal detection mechanisms seem
|
// we use loadURI(), since Firefox's internal detection mechanisms seem
|
||||||
// to sometimes get confused
|
// to sometimes get confused
|
||||||
// (see #192, https://chnm.gmu.edu/trac/zotero/ticket/192)
|
// (see #192, https://chnm.gmu.edu/trac/zotero/ticket/192)
|
||||||
if (mimeType=='text/html' &&
|
if (mimeType=='text/html' &&
|
||||||
(fileName.substr(fileName.length-5)!='.html'
|
(fileName.substr(fileName.length-5)!='.html'
|
||||||
&& fileName.substr(fileName.length-4)!='.htm')){
|
&& fileName.substr(fileName.length-4)!='.htm')){
|
||||||
fileName += '.html';
|
fileName += '.html';
|
||||||
}
|
|
||||||
|
|
||||||
file.append(fileName);
|
|
||||||
|
|
||||||
wbp.progressListener = new Zotero.WebProgressFinishListener(function(){
|
|
||||||
_addToDB(file, url, title, Zotero.Attachments.LINK_MODE_IMPORTED_URL, mimeType,
|
|
||||||
charsetID, sourceItemID, itemID);
|
|
||||||
|
|
||||||
// Add to collections
|
|
||||||
if (parentCollectionIDs){
|
|
||||||
var ids = Zotero.flattenArguments(parentCollectionIDs);
|
|
||||||
for each(var id in ids){
|
|
||||||
var col = Zotero.Collections.get(id);
|
|
||||||
col.addItem(itemID);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Zotero.DB.commitTransaction();
|
file.append(fileName);
|
||||||
|
|
||||||
Zotero.Fulltext.indexDocument(document, itemID);
|
wbp.progressListener = new Zotero.WebProgressFinishListener(function(){
|
||||||
});
|
try {
|
||||||
|
_addToDB(file, url, title, Zotero.Attachments.LINK_MODE_IMPORTED_URL, mimeType,
|
||||||
|
charsetID, sourceItemID, itemID);
|
||||||
|
|
||||||
wbp.saveDocument(document, file, destDir, mimeType, encodingFlags, false);
|
// Add to collections
|
||||||
|
if (parentCollectionIDs){
|
||||||
|
var ids = Zotero.flattenArguments(parentCollectionIDs);
|
||||||
|
for each(var id in ids){
|
||||||
|
var col = Zotero.Collections.get(id);
|
||||||
|
col.addItem(itemID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Zotero.DB.commitTransaction();
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
Zotero.DB.rollbackTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
Zotero.Fulltext.indexDocument(document, itemID);
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -368,9 +413,13 @@ Zotero.Attachments = new function(){
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mimeType){
|
if (mimeType){
|
||||||
var ext = Components.classes["@mozilla.org/mime;1"]
|
try {
|
||||||
.getService(Components.interfaces.nsIMIMEService)
|
var ext = Components.classes["@mozilla.org/mime;1"]
|
||||||
.getPrimaryExtension(mimeType, nsIURL.fileExt ? nsIURL.fileExt : null);
|
.getService(Components.interfaces.nsIMIMEService)
|
||||||
|
.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 : '');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user