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.
This commit is contained in:
Dan Stillman 2017-07-01 06:20:27 -04:00
parent b1fad5a310
commit 9a3ff2d244
3 changed files with 41 additions and 21 deletions

View file

@ -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<Zotero.Item>}
*/
@ -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));

View file

@ -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
*

View file

@ -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);
})
})
})