From 9a3ff2d2446e9be269a3c4187bd5d255ce36393a Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 1 Jul 2017 06:20:27 -0400 Subject: [PATCH] Add .contentType and .charset options for importFromFile() And use them in new importTextAttachment() and importHTMLAttachment() test support functions. These can be used to avoid needing a hidden browser for determining the character set of the imported text documents. --- chrome/content/zotero/xpcom/attachments.js | 12 +++++-- test/content/support.js | 10 ++++++ test/tests/storageLocalTest.js | 40 ++++++++++++---------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 3250f446d5..fb3de655e1 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -39,6 +39,8 @@ Zotero.Attachments = new function(){ * @param {Integer} [options.libraryID] * @param {Integer[]|String[]} [options.parentItemID] - Parent item to add item to * @param {Integer[]} [options.collections] - Collection keys or ids to add new item to + * @param {String} [options.contentType] + * @param {String} [options.charset] * @param {Object} [options.saveOptions] - Options to pass to Zotero.Item::save() * @return {Promise} */ @@ -49,6 +51,8 @@ Zotero.Attachments = new function(){ var file = Zotero.File.pathToFile(options.file); var parentItemID = options.parentItemID; var collections = options.collections; + var contentType = options.contentType; + var charset = options.charset; var saveOptions = options.saveOptions; var newName = Zotero.File.getValidFileName(file.leafName); @@ -90,9 +94,13 @@ Zotero.Attachments = new function(){ // Copy file to unique filename, which automatically shortens long filenames newFile = Zotero.File.copyToUnique(file, newFile); - contentType = yield Zotero.MIME.getMIMETypeFromFile(newFile); - + if (!contentType) { + contentType = yield Zotero.MIME.getMIMETypeFromFile(newFile); + } attachmentItem.attachmentContentType = contentType; + if (charset) { + attachmentItem.attachmentCharset = charset; + } attachmentItem.attachmentPath = newFile.path; yield attachmentItem.save(saveOptions); }.bind(this)); diff --git a/test/content/support.js b/test/content/support.js index 0fad22b671..10a4e752b2 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -851,6 +851,16 @@ function importFileAttachment(filename, options = {}) { } +function importTextAttachment() { + return importFileAttachment('test.txt', { contentType: 'text/plain', charset: 'utf-8' }); +} + + +function importHTMLAttachment() { + return importFileAttachment('test.html', { contentType: 'text/html', charset: 'utf-8' }); +} + + /** * Sets the fake XHR server to response to a given response * diff --git a/test/tests/storageLocalTest.js b/test/tests/storageLocalTest.js index 2f4143156a..1afdc55f38 100644 --- a/test/tests/storageLocalTest.js +++ b/test/tests/storageLocalTest.js @@ -1,27 +1,18 @@ "use strict"; describe("Zotero.Sync.Storage.Local", function () { - var win; - - before(function* () { - win = yield loadBrowserWindow(); - }); beforeEach(function* () { yield resetDB({ thisArg: this, skipBundledFiles: true }) }) - after(function () { - if (win) { - win.close(); - } - }); + describe("#checkForUpdatedFiles()", function () { it("should flag modified file for upload and return it", function* () { // Create attachment - let item = yield importFileAttachment('test.txt') + let item = yield importTextAttachment(); var hash = yield item.attachmentHash; // Set file mtime to the past (without milliseconds, which aren't used on OS X) var mtime = (Math.floor(new Date().getTime() / 1000) * 1000) - 1000; @@ -50,7 +41,7 @@ describe("Zotero.Sync.Storage.Local", function () { it("should skip a file if mod time hasn't changed", function* () { // Create attachment - let item = yield importFileAttachment('test.txt') + let item = yield importTextAttachment(); var hash = yield item.attachmentHash; var mtime = yield item.attachmentModificationTime; @@ -72,7 +63,7 @@ describe("Zotero.Sync.Storage.Local", function () { it("should skip a file if mod time has changed but contents haven't", function* () { // Create attachment - let item = yield importFileAttachment('test.txt') + let item = yield importTextAttachment(); var hash = yield item.attachmentHash; // Set file mtime to the past (without milliseconds, which aren't used on OS X) var mtime = (Math.floor(new Date().getTime() / 1000) * 1000) - 1000; @@ -529,8 +520,8 @@ describe("Zotero.Sync.Storage.Local", function () { var item1 = yield importFileAttachment('test.png'); item1.version = 10; yield item1.saveTx(); - var item2 = yield importFileAttachment('test.txt'); - var item3 = yield importFileAttachment('test.html'); + var item2 = yield importTextAttachment(); + var item3 = yield importHTMLAttachment(); item3.version = 11; yield item3.saveTx(); @@ -574,14 +565,27 @@ describe("Zotero.Sync.Storage.Local", function () { }) describe("#resolveConflicts()", function () { + var win; + + before(function* () { + win = yield loadBrowserWindow(); + }); + + after(function () { + if (win) { + win.close(); + } + }); + + it("should show the conflict resolution window on attachment conflicts", function* () { var libraryID = Zotero.Libraries.userLibraryID; var item1 = yield importFileAttachment('test.png'); item1.version = 10; yield item1.saveTx(); - var item2 = yield importFileAttachment('test.txt'); - var item3 = yield importFileAttachment('test.html'); + var item2 = yield importTextAttachment(); + var item3 = yield importHTMLAttachment(); item3.version = 11; yield item3.saveTx(); @@ -647,6 +651,4 @@ describe("Zotero.Sync.Storage.Local", function () { assert.isNull(item3.attachmentSyncedHash); }) }) - - })