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:
parent
b1fad5a310
commit
9a3ff2d244
3 changed files with 41 additions and 21 deletions
|
@ -39,6 +39,8 @@ Zotero.Attachments = new function(){
|
||||||
* @param {Integer} [options.libraryID]
|
* @param {Integer} [options.libraryID]
|
||||||
* @param {Integer[]|String[]} [options.parentItemID] - Parent item to add item to
|
* @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 {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()
|
* @param {Object} [options.saveOptions] - Options to pass to Zotero.Item::save()
|
||||||
* @return {Promise<Zotero.Item>}
|
* @return {Promise<Zotero.Item>}
|
||||||
*/
|
*/
|
||||||
|
@ -49,6 +51,8 @@ Zotero.Attachments = new function(){
|
||||||
var file = Zotero.File.pathToFile(options.file);
|
var file = Zotero.File.pathToFile(options.file);
|
||||||
var parentItemID = options.parentItemID;
|
var parentItemID = options.parentItemID;
|
||||||
var collections = options.collections;
|
var collections = options.collections;
|
||||||
|
var contentType = options.contentType;
|
||||||
|
var charset = options.charset;
|
||||||
var saveOptions = options.saveOptions;
|
var saveOptions = options.saveOptions;
|
||||||
|
|
||||||
var newName = Zotero.File.getValidFileName(file.leafName);
|
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
|
// Copy file to unique filename, which automatically shortens long filenames
|
||||||
newFile = Zotero.File.copyToUnique(file, newFile);
|
newFile = Zotero.File.copyToUnique(file, newFile);
|
||||||
|
|
||||||
contentType = yield Zotero.MIME.getMIMETypeFromFile(newFile);
|
if (!contentType) {
|
||||||
|
contentType = yield Zotero.MIME.getMIMETypeFromFile(newFile);
|
||||||
|
}
|
||||||
attachmentItem.attachmentContentType = contentType;
|
attachmentItem.attachmentContentType = contentType;
|
||||||
|
if (charset) {
|
||||||
|
attachmentItem.attachmentCharset = charset;
|
||||||
|
}
|
||||||
attachmentItem.attachmentPath = newFile.path;
|
attachmentItem.attachmentPath = newFile.path;
|
||||||
yield attachmentItem.save(saveOptions);
|
yield attachmentItem.save(saveOptions);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
|
@ -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
|
* Sets the fake XHR server to response to a given response
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,27 +1,18 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe("Zotero.Sync.Storage.Local", function () {
|
describe("Zotero.Sync.Storage.Local", function () {
|
||||||
var win;
|
|
||||||
|
|
||||||
before(function* () {
|
|
||||||
win = yield loadBrowserWindow();
|
|
||||||
});
|
|
||||||
beforeEach(function* () {
|
beforeEach(function* () {
|
||||||
yield resetDB({
|
yield resetDB({
|
||||||
thisArg: this,
|
thisArg: this,
|
||||||
skipBundledFiles: true
|
skipBundledFiles: true
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
after(function () {
|
|
||||||
if (win) {
|
|
||||||
win.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("#checkForUpdatedFiles()", function () {
|
describe("#checkForUpdatedFiles()", function () {
|
||||||
it("should flag modified file for upload and return it", function* () {
|
it("should flag modified file for upload and return it", function* () {
|
||||||
// Create attachment
|
// Create attachment
|
||||||
let item = yield importFileAttachment('test.txt')
|
let item = yield importTextAttachment();
|
||||||
var hash = yield item.attachmentHash;
|
var hash = yield item.attachmentHash;
|
||||||
// Set file mtime to the past (without milliseconds, which aren't used on OS X)
|
// 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;
|
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* () {
|
it("should skip a file if mod time hasn't changed", function* () {
|
||||||
// Create attachment
|
// Create attachment
|
||||||
let item = yield importFileAttachment('test.txt')
|
let item = yield importTextAttachment();
|
||||||
var hash = yield item.attachmentHash;
|
var hash = yield item.attachmentHash;
|
||||||
var mtime = yield item.attachmentModificationTime;
|
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* () {
|
it("should skip a file if mod time has changed but contents haven't", function* () {
|
||||||
// Create attachment
|
// Create attachment
|
||||||
let item = yield importFileAttachment('test.txt')
|
let item = yield importTextAttachment();
|
||||||
var hash = yield item.attachmentHash;
|
var hash = yield item.attachmentHash;
|
||||||
// Set file mtime to the past (without milliseconds, which aren't used on OS X)
|
// 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;
|
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');
|
var item1 = yield importFileAttachment('test.png');
|
||||||
item1.version = 10;
|
item1.version = 10;
|
||||||
yield item1.saveTx();
|
yield item1.saveTx();
|
||||||
var item2 = yield importFileAttachment('test.txt');
|
var item2 = yield importTextAttachment();
|
||||||
var item3 = yield importFileAttachment('test.html');
|
var item3 = yield importHTMLAttachment();
|
||||||
item3.version = 11;
|
item3.version = 11;
|
||||||
yield item3.saveTx();
|
yield item3.saveTx();
|
||||||
|
|
||||||
|
@ -574,14 +565,27 @@ describe("Zotero.Sync.Storage.Local", function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("#resolveConflicts()", 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* () {
|
it("should show the conflict resolution window on attachment conflicts", function* () {
|
||||||
var libraryID = Zotero.Libraries.userLibraryID;
|
var libraryID = Zotero.Libraries.userLibraryID;
|
||||||
|
|
||||||
var item1 = yield importFileAttachment('test.png');
|
var item1 = yield importFileAttachment('test.png');
|
||||||
item1.version = 10;
|
item1.version = 10;
|
||||||
yield item1.saveTx();
|
yield item1.saveTx();
|
||||||
var item2 = yield importFileAttachment('test.txt');
|
var item2 = yield importTextAttachment();
|
||||||
var item3 = yield importFileAttachment('test.html');
|
var item3 = yield importHTMLAttachment();
|
||||||
item3.version = 11;
|
item3.version = 11;
|
||||||
yield item3.saveTx();
|
yield item3.saveTx();
|
||||||
|
|
||||||
|
@ -647,6 +651,4 @@ describe("Zotero.Sync.Storage.Local", function () {
|
||||||
assert.isNull(item3.attachmentSyncedHash);
|
assert.isNull(item3.attachmentSyncedHash);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue