1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-12-11 23:17:19 +02:00

Clipper: Support clipping screenshots

This commit is contained in:
Laurent Cozic
2018-05-25 08:51:54 +01:00
parent 4640d6d6e3
commit 264ee4f319
8 changed files with 132 additions and 44 deletions

View File

@@ -10,6 +10,7 @@ const { fileExtension, safeFileExtension, safeFilename, filename } = require('li
const HtmlToMd = require('lib/HtmlToMd');
const { Logger } = require('lib/logger.js');
const markdownUtils = require('lib/markdownUtils');
const mimeUtils = require('lib/mime-utils.js').mime;
class ClipperServer {
@@ -59,6 +60,19 @@ class ClipperServer {
return output;
}
// Note must have been saved first
async attachImageFromDataUrl_(note, imageDataUrl, cropRect) {
const tempDir = Setting.value('tempDir');
const mime = mimeUtils.fromDataUrl(imageDataUrl);
let ext = mimeUtils.toFileExtension(mime) || '';
if (ext) ext = '.' + ext;
const tempFilePath = tempDir + '/' + md5(Math.random() + '_' + Date.now()) + ext;
const imageConvOptions = {};
if (cropRect) imageConvOptions.cropRect = cropRect;
await shim.imageFromDataUrl(imageDataUrl, tempFilePath, imageConvOptions);
return await shim.attachFileToNote(note, tempFilePath);
}
async downloadImage_(url) {
const tempDir = Setting.value('tempDir');
const name = filename(url);
@@ -189,6 +203,11 @@ class ClipperServer {
note.body = this.replaceImageUrlsByResources_(note.body, result);
note = await Note.save(note);
if (requestNote.imageDataUrl) {
await this.attachImageFromDataUrl_(note, requestNote.imageDataUrl, requestNote.cropRect);
}
this.logger().info('Request (' + requestId + '): Created note ' + note.id);
return writeResponseJson(200, note);
} catch (error) {