mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Desktop, Cli: Fixed importing ENEX files that contain resources with invalid mime type
This commit is contained in:
parent
38b0702314
commit
33c5037816
@ -113,4 +113,18 @@ describe('EnexToMd', function() {
|
||||
expect(all[0].body).toBe('');
|
||||
});
|
||||
|
||||
it('should handle invalid mime types', async () => {
|
||||
// This is to handle the case where a resource has an invalid mime type,
|
||||
// but that type can be determined from the filename. For example, in
|
||||
// this thread, the ENEX file contains a "file.zip" file with a mime
|
||||
// type "application/octet-stream", which can later cause problems to
|
||||
// open the file.
|
||||
// https://discourse.joplinapp.org/t/importing-a-note-with-a-zip-file/12123?u=laurent
|
||||
const filePath = `${enexSampleBaseDir}/WithInvalidMime.enex`;
|
||||
await importEnex('', filePath);
|
||||
const all = await Resource.all();
|
||||
expect(all.length).toBe(1);
|
||||
expect(all[0].mime).toBe('application/zip');
|
||||
});
|
||||
|
||||
});
|
||||
|
53
packages/app-cli/tests/enex_to_md/WithInvalidMime.enex
Normal file
53
packages/app-cli/tests/enex_to_md/WithInvalidMime.enex
Normal file
@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
|
||||
<en-export export-date="20201218T233722Z" application="Evernote" version="Evernote Mac 7.13 (458080)">
|
||||
<note><title>WithInvalidMime</title><content><![CDATA[<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div><en-media hash="d502aa19556b5b4b4dcceaf0514ad206" type="application/zip" style="cursor:pointer;" /></div><div><br /></div></en-note>]]></content><created>20201218T233549Z</created><updated>20201218T233706Z</updated><note-attributes><latitude>51.57516479492188</latitude><longitude>0.2281720315013734</longitude><altitude>29.71427536010742</altitude><author>laurent@cozic.net</author><source>desktop.mac</source><reminder-order>0</reminder-order></note-attributes><resource><data encoding="base64">UEsDBBQACAAIAJlyelEAAAAAAAAAAKAKAAAJ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rvlenN
|
||||
6zRFv4mRV4PbYR2N7YdNO16Kb1/C84rYZ/sMiUKcmWPf1GUX/KkzmWM0scFzHWAweTOWC+DyamvzGyNV
|
||||
iy2NrJHhekMEe4rQ9XfFz18oIdIdTqwWW3ZZWnG2vm4VUBO7VjCBtBDNSea1CFVfSRU1xFmnri/T6VVT
|
||||
CxgC3VhsoG4tl0yiBORZ/BNuIKdbAIOblt5Ri/2BvW5s1qEK7lXKrTpve6bDDy1taM18I03sRZGDMzji
|
||||
QTjDl7tSsaxgQmTNfZVojMyocunznEVm01uabymZsv1AO0rXN6iG/UpkkqMJ4akLuQj1nGUpC60i7tek
|
||||
pEifM6hvDrh8kITke7xm5JuzciFhG/FNNehw4q4MA31N1jk/6upk/zJScztSp7Vn6adTd9qf9MWfch36
|
||||
fgWRa/CW+bWEQpHEvezbY64DNEjpBzseZjXwvjxjpJOCxWVaIfCHUtwhxJsmOQHZeDWFOIeLAaFG62+r
|
||||
+q2eJsv8ioccqJFegH3lcyQOEaFuyvQPdav/GL/3ZO3e8RQjhv7M+WXW5W17bql70fjWF7tR1o3IxajS
|
||||
m/fOpZEG04L7PwcbwTt+2T1YIYu5krZL3M7iFH4MsW8KZthygWljiUt3TQH5TlQnv4o8d+5fUEsHCAlM
|
||||
JEl7CgAAoAoAAFBLAQIUAxQACAAIAJlyelEJTCRJewoAAKAKAAAJACAAAAAAAAAAAACkgQAAAABwaG90
|
||||
by5qcGdVVA0AB8O5v1/Pub9fw7m/X3V4CwABBPUBAAAEFAAAAFBLBQYAAAAAAQABAFcAAADSCgAAAAA=
|
||||
</data><mime>application/octet-stream</mime><width>0</width><height>0</height><duration>0</duration><resource-attributes><timestamp>19700101T000000Z</timestamp><file-name>photo.zip</file-name></resource-attributes></resource></note>
|
||||
</en-export>
|
@ -14,6 +14,7 @@ const md5 = require('md5');
|
||||
const { Base64Decode } = require('base64-stream');
|
||||
const md5File = require('md5-file');
|
||||
const shim = require('./shim').default;
|
||||
const { mime } = require('./mime-utils');
|
||||
|
||||
// const Promise = require('promise');
|
||||
const fs = require('fs-extra');
|
||||
@ -514,11 +515,27 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
|
||||
|
||||
noteAttributes = null;
|
||||
} else if (n == 'resource') {
|
||||
let mimeType = noteResource.mime ? noteResource.mime.trim() : '';
|
||||
|
||||
// Evernote sometimes gives an invalid or generic
|
||||
// "application/octet-stream" mime type for files that could
|
||||
// have a valid mime type, based on the extension. So in
|
||||
// general, we trust the filename more than the provided mime
|
||||
// type.
|
||||
// https://discourse.joplinapp.org/t/importing-a-note-with-a-zip-file/12123
|
||||
if (noteResource.filename) {
|
||||
const mimeTypeFromFile = mime.fromFilename(noteResource.filename);
|
||||
if (mimeTypeFromFile && mimeTypeFromFile !== mimeType) {
|
||||
importOptions.onError(new Error(`Invalid mime type "${mimeType}" for resource "${noteResource.filename}". Using "${mimeTypeFromFile}" instead.`));
|
||||
mimeType = mimeTypeFromFile;
|
||||
}
|
||||
}
|
||||
|
||||
note.resources.push({
|
||||
id: noteResource.id,
|
||||
dataFilePath: noteResource.dataFilePath,
|
||||
dataEncoding: noteResource.dataEncoding,
|
||||
mime: noteResource.mime ? noteResource.mime.trim() : '',
|
||||
mime: mimeType,
|
||||
title: noteResource.filename ? noteResource.filename.trim() : '',
|
||||
filename: noteResource.filename ? noteResource.filename.trim() : '',
|
||||
hasData: noteResource.hasData,
|
||||
|
Loading…
Reference in New Issue
Block a user