1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Clipper: Fixes #1600: Handle SVG images and fix issue with invalid file extensions

This commit is contained in:
Laurent Cozic 2019-06-12 09:45:31 +01:00
parent d6218f35fe
commit ad211b4b4e
3 changed files with 29 additions and 3 deletions

View File

@ -30,4 +30,11 @@ netUtils.findAvailablePort = async (possiblePorts, extraRandomPortsToTry = 20) =
return port;
}
netUtils.mimeTypeFromHeaders = headers => {
if (!headers || !headers['content-type']) return null;
const splitted = headers['content-type'].split(';');
return splitted[0].trim().toLowerCase();
}
module.exports = { netUtils };

View File

@ -37,9 +37,10 @@ function isHidden(path) {
return b[0] === '.';
}
function safeFileExtension(e) {
function safeFileExtension(e, maxLength = null) {
if (maxLength === null) maxLength = 8;
if (!e || !e.replace) return '';
return e.replace(/[^a-zA-Z0-9]/g, '')
return e.replace(/[^a-zA-Z0-9]/g, '').substr(0, maxLength);
}
function safeFilename(e, maxLength = null, allowSpaces = false) {

View File

@ -12,6 +12,7 @@ const md5 = require('md5');
const { shim } = require('lib/shim');
const HtmlToMd = require('lib/HtmlToMd');
const urlUtils = require('lib/urlUtils.js');
const { netUtils } = require('lib/net-utils');
const { fileExtension, safeFileExtension, safeFilename, filename } = require('lib/path-utils');
const ApiResponse = require('lib/services/rest/ApiResponse');
const SearchEngineUtils = require('lib/services/SearchEngineUtils');
@ -459,6 +460,18 @@ class Api {
return await shim.attachFileToNote(note, tempFilePath);
}
async tryToGuessImageExtFromMimeType_(response, imagePath) {
const mimeType = netUtils.mimeTypeFromHeaders(response.headers);
if (!mimeType) return imagePath;
const newExt = mimeUtils.toFileExtension(mimeType);
if (!newExt) return imagePath;
const newImagePath = imagePath + '.' + newExt;
await shim.fsDriver().move(imagePath, newImagePath);
return newImagePath;
}
async downloadImage_(url, allowFileProtocolImages) {
const tempDir = Setting.value('tempDir');
@ -466,6 +479,7 @@ class Api {
const name = isDataUrl ? md5(Math.random() + '_' + Date.now()) : filename(url);
let fileExt = isDataUrl ? mimeUtils.toFileExtension(mimeUtils.fromDataUrl(url)) : safeFileExtension(fileExtension(url).toLowerCase());
if (!mimeUtils.fromFileExtension(fileExt)) fileExt = ''; // If the file extension is unknown - clear it.
if (fileExt) fileExt = '.' + fileExt;
let imagePath = tempDir + '/' + safeFilename(name) + fileExt;
if (await shim.fsDriver().exists(imagePath)) imagePath = tempDir + '/' + safeFilename(name) + '_' + md5(Math.random() + '_' + Date.now()).substr(0,10) + fileExt;
@ -479,7 +493,11 @@ class Api {
const localPath = uri2path(url);
await shim.fsDriver().copy(localPath, imagePath);
} else {
await shim.fetchBlob(url, { path: imagePath });
const response = await shim.fetchBlob(url, { path: imagePath, maxRetry: 1 });
// If we could not find the file extension from the URL, try to get it
// now based on the Content-Type header.
if (!fileExt) imagePath = this.tryToGuessImageExtFromMimeType_(response, imagePath);
}
return imagePath;
} catch (error) {