diff --git a/chrome/chromeFiles/content/scholar/overlay.js b/chrome/chromeFiles/content/scholar/overlay.js index f1b9fb607..c7544d6bd 100644 --- a/chrome/chromeFiles/content/scholar/overlay.js +++ b/chrome/chromeFiles/content/scholar/overlay.js @@ -530,8 +530,7 @@ var ScholarPane = new function() if(attachment.getAttachmentLinkMode() != Scholar.Attachments.LINK_MODE_LINKED_URL) { var file = attachment.getFile(); - if (attachment.getAttachmentLinkMode() == Scholar.Attachments.LINK_MODE_IMPORTED_URL - || Scholar.File.hasInternalHandler(file)) + if (Scholar.MIME.fileHasInternalHandler(file)) { window.loadURI(attachment.getLocalFileURL()); } diff --git a/chrome/chromeFiles/content/scholar/xpcom/data_access.js b/chrome/chromeFiles/content/scholar/xpcom/data_access.js index 11587c7ef..6c5735190 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/data_access.js +++ b/chrome/chromeFiles/content/scholar/xpcom/data_access.js @@ -1978,13 +1978,70 @@ Scholar.Attachments = new function(){ function importFromURL(url, sourceItemID){ - var browser = Scholar.Browser.createHiddenBrowser(); - browser.addEventListener("pageshow", function(){ - Scholar.Attachments.importFromDocument(browser.contentDocument, sourceItemID); - browser.removeEventListener("pageshow", arguments.callee, true); - Scholar.Browser.deleteHiddenBrowser(browser); - }, true); - browser.loadURI(url, null, null, null, null); + Scholar.Utilities.HTTP.doHead(url, function(obj){ + var mimeType = obj.channel.contentType; + + var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"] + .createInstance(Components.interfaces.nsIURL); + nsIURL.spec = url; + var ext = nsIURL.fileExtension; + + // If we can load this internally, use a hidden browser (so we can + // get the charset and title) + if (Scholar.MIME.hasInternalHandler(mimeType, ext)){ + var browser = Scholar.Browser.createHiddenBrowser(); + browser.addEventListener("pageshow", function(){ + Scholar.Attachments.importFromDocument(browser.contentDocument, sourceItemID); + browser.removeEventListener("pageshow", arguments.callee, true); + Scholar.Browser.deleteHiddenBrowser(browser); + }, true); + browser.loadURI(url, null, null, null, null); + } + + // Otherwise use a remote web page persist + else { + var fileName = _getFileNameFromURL(url, mimeType); + var title = fileName; + + const nsIWBP = Components.interfaces.nsIWebBrowserPersist; + var wbp = Components + .classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] + .createInstance(nsIWBP); + //wbp.persistFlags = nsIWBP.PERSIST_FLAGS...; + var encodingFlags = false; + + Scholar.DB.beginTransaction(); + + try { + // Create a new attachment + var attachmentItem = Scholar.Items.getNewItemByType(Scholar.ItemTypes.getID('attachment')); + attachmentItem.setField('title', title); + attachmentItem.save(); + var itemID = attachmentItem.getID(); + + // Create a new folder for this item in the storage directory + var destDir = Scholar.getStorageDirectory(); + destDir.append(itemID); + destDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0644); + + var file = Components.classes["@mozilla.org/file/local;1"]. + createInstance(Components.interfaces.nsILocalFile); + file.initWithFile(destDir); + file.append(fileName); + + wbp.saveURI(nsIURL, null, null, null, null, file); + + _addToDB(file, url, title, Scholar.Attachments.LINK_MODE_IMPORTED_URL, + mimeType, null, sourceItemID, itemID); + + Scholar.DB.commitTransaction(); + } + catch (e){ + Scholar.DB.rollbackTransaction(); + throw (e); + } + } + }); } diff --git a/chrome/chromeFiles/content/scholar/xpcom/file.js b/chrome/chromeFiles/content/scholar/xpcom/file.js index 628fcfa4c..2e2fda79d 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/file.js +++ b/chrome/chromeFiles/content/scholar/xpcom/file.js @@ -1,38 +1,6 @@ Scholar.File = new function(){ this.getExtension = getExtension; - this.isExternalTextExtension = isExternalTextExtension; this.getSample = getSample; - this.sniffForMIMEType = sniffForMIMEType; - this.sniffForBinary = sniffForBinary; - this.getMIMETypeFromFile = getMIMETypeFromFile; - this.hasInternalHandler = hasInternalHandler; - - // Magic numbers - var _snifferEntries = [ - ["%PDF-", "application/pdf"], - ["%!PS-Adobe-", 'application/postscript'], - ["%! PS-Adobe-", 'application/postscript'], - ["From", 'text/plain'], - [">From", 'text/plain'], - ["#!", 'text/plain'], - [" 31 || (9 <= chr && chr <=13 ) || chr == 27; + browser.loadURI(url, Components.interfaces.nsIWebNavigation.LOAD_FLAGS_BYPASS_HISTORY); } } diff --git a/chrome/chromeFiles/content/scholar/xpcom/mime.js b/chrome/chromeFiles/content/scholar/xpcom/mime.js new file mode 100644 index 000000000..618e92f1f --- /dev/null +++ b/chrome/chromeFiles/content/scholar/xpcom/mime.js @@ -0,0 +1,178 @@ +Scholar.MIME = new function(){ + this.isExternalTextExtension = isExternalTextExtension; + this.sniffForMIMEType = sniffForMIMEType; + this.sniffForBinary = sniffForBinary; + this.getMIMETypeFromData = getMIMETypeFromData; + this.getMIMETypeFromFile = getMIMETypeFromFile; + this.hasInternalHandler = hasInternalHandler; + this.fileHasInternalHandler = fileHasInternalHandler; + + // Magic numbers + var _snifferEntries = [ + ["%PDF-", "application/pdf"], + ["%!PS-Adobe-", 'application/postscript'], + ["%! PS-Adobe-", 'application/postscript'], + ["From", 'text/plain'], + [">From", 'text/plain'], + ["#!", 'text/plain'], + [" 31 || (9 <= chr && chr <=13 ) || chr == 27; + } +} diff --git a/components/chnmIScholarService.js b/components/chnmIScholarService.js index aacaeda87..19081d35e 100644 --- a/components/chnmIScholarService.js +++ b/components/chnmIScholarService.js @@ -30,10 +30,6 @@ Cc["@mozilla.org/moz/jssubscript-loader;1"] .getService(Ci.mozIJSSubScriptLoader) .loadSubScript("chrome://scholar/content/xpcom/data_access.js"); -Cc["@mozilla.org/moz/jssubscript-loader;1"] - .getService(Ci.mozIJSSubScriptLoader) - .loadSubScript("chrome://scholar/content/xpcom/file.js"); - Cc["@mozilla.org/moz/jssubscript-loader;1"] .getService(Ci.mozIJSSubScriptLoader) .loadSubScript("chrome://scholar/content/xpcom/notifier.js"); @@ -62,6 +58,14 @@ Cc["@mozilla.org/moz/jssubscript-loader;1"] .getService(Ci.mozIJSSubScriptLoader) .loadSubScript("chrome://scholar/content/xpcom/utilities.js"); +Cc["@mozilla.org/moz/jssubscript-loader;1"] + .getService(Ci.mozIJSSubScriptLoader) + .loadSubScript("chrome://scholar/content/xpcom/file.js"); + +Cc["@mozilla.org/moz/jssubscript-loader;1"] + .getService(Ci.mozIJSSubScriptLoader) + .loadSubScript("chrome://scholar/content/xpcom/mime.js"); + /********************************************************************/