Get binary contents in Zotero.File.getSample()

And fix magic numbers for content-type sniffing, which wrongly used the
Unicode replacement character (which likely just meant we were falling
back to file-extension-based detection)
This commit is contained in:
Dan Stillman 2022-06-17 18:24:44 -04:00
parent 13adfd131c
commit 2b41b0127c
3 changed files with 27 additions and 7 deletions

View file

@ -118,9 +118,9 @@ Zotero.File = new function(){
* @param {nsIURI|nsIFile|string spec|nsIChannel|nsIInputStream} source - The source to read
* @return {Promise}
*/
this.getSample = function (file) {
this.getSample = async function (file) {
var bytes = 200;
return this.getContentsAsync(file, null, bytes);
return this.getBinaryContentsAsync(file, bytes);
}

View file

@ -35,21 +35,19 @@ Zotero.MIME = new function(){
["%PDF-", "application/pdf"],
["%!PS-Adobe-", 'application/postscript', 0],
["%! PS-Adobe-", 'application/postscript', 0],
["\uFFFD\uFFFD\x11\u0871\x1A\uFFFD\x00\x00", "application/msword", 0],
["From", 'text/plain', 0],
[">From", 'text/plain', 0],
["#!", 'text/plain', 0],
["<?xml", 'text/xml', 0],
["<!DOCTYPE html", 'text/html', 0],
["<html", 'text/html', 0],
["\uFFFD\uFFFD\uFFFD\uFFFD", 'image/jpeg', 0],
["GIF8", 'image/gif', 0],
["\uFFFDPNG", 'image/png', 0],
["\u0089PNG", 'image/png', 0],
["\u00FF\u00D8", 'image/jpeg', 0],
["JFIF", 'image/jpeg'],
["FLV", "video/x-flv", 0],
["\u0000\u0000\u0001\u0000", "image/vnd.microsoft.icon", 0],
["\u0053\u0051\u004C\u0069\u0074\u0065\u0020\u0066"
+ "\u006F\u0072\u006D\u0061\u0074\u0020\u0033\u0000", "application/x-sqlite3", 0]
["SQLite format 3\u0000", "application/x-sqlite3", 0],
];
var _extensions = {

22
test/tests/mimeTest.js Normal file
View file

@ -0,0 +1,22 @@
describe("Zotero.MIME", function () {
describe("#sniffForMIMEType()", function () {
async function test(filename, expectedType) {
var path = OS.Path.join(getTestDataDirectory().path, filename);
var sample = await Zotero.File.getSample(path);
var type = Zotero.MIME.sniffForMIMEType(sample);
assert.equal(type, expectedType);
}
it("should detect PNG", async function () {
await test('test.png', 'image/png');
});
it("should detect JPEG", async function () {
await test('test.jpg', 'image/jpeg');
});
it("should detect SQLite database", async function () {
await test('dev@zotero.org@www.mendeley.com.sqlite', 'application/x-sqlite3');
});
});
});