1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

Merge branch 'clipper'

This commit is contained in:
Laurent Cozic
2018-05-25 13:31:15 +01:00
114 changed files with 25697 additions and 1180 deletions

View File

@ -5,6 +5,7 @@ const { FileApiDriverLocal } = require('lib/file-api-driver-local.js');
const { time } = require('lib/time-utils.js');
const { setLocale, defaultLocale, closestSupportedLocale } = require('lib/locale.js');
const { FsDriverNode } = require('lib/fs-driver-node.js');
const mimeUtils = require('lib/mime-utils.js').mime;
const urlValidator = require('valid-url');
function shimInit() {
@ -35,21 +36,24 @@ function shimInit() {
return locale;
}
// For Electron only
shim.writeImageToFile = async function(nativeImage, mime, targetPath) {
let buffer = null;
if (shim.isElectron()) { // For Electron
let buffer = null;
mime = mime.toLowerCase();
mime = mime.toLowerCase();
if (mime === 'image/png') {
buffer = nativeImage.toPNG();
} else if (mime === 'image/jpg' || mime === 'image/jpeg') {
buffer = nativeImage.toJPEG(90);
if (mime === 'image/png') {
buffer = nativeImage.toPNG();
} else if (mime === 'image/jpg' || mime === 'image/jpeg') {
buffer = nativeImage.toJPEG(90);
}
if (!buffer) throw new Error('Cannot resize image because mime type "' + mime + '" is not supported: ' + targetPath);
await shim.fsDriver().writeFile(targetPath, buffer, 'buffer');
} else {
throw new Error('Node support not implemented');
}
if (!buffer) throw new Error('Cannot reisze image because mime type "' + mime + '" is not supported: ' + targetPath);
await shim.fsDriver().writeFile(targetPath, buffer, 'buffer');
}
const resizeImage_ = async function(filePath, targetPath, mime) {
@ -96,7 +100,10 @@ function shimInit() {
}
}
shim.attachFileToNote = async function(note, filePath, position = null) {
shim.createResourceFromPath = async function(filePath) {
const readChunk = require('read-chunk');
const imageType = require('image-type');
const Resource = require('lib/models/Resource.js');
const { uuid } = require('lib/uuid.js');
const { basename, fileExtension, safeFileExtension } = require('lib/path-utils.js');
@ -109,9 +116,22 @@ function shimInit() {
resource.id = uuid.create();
resource.mime = mime.getType(filePath);
resource.title = basename(filePath);
resource.file_extension = safeFileExtension(fileExtension(filePath));
if (!resource.mime) resource.mime = 'application/octet-stream';
let fileExt = safeFileExtension(fileExtension(filePath));
if (!resource.mime) {
const buffer = await readChunk(filePath, 0, 64);
const detectedType = imageType(buffer);
if (detectedType) {
fileExt = detectedType.ext;
resource.mime = detectedType.mime;
} else {
resource.mime = 'application/octet-stream';
}
}
resource.file_extension = fileExt;
let targetPath = Resource.fullPath(resource);
@ -126,6 +146,12 @@ function shimInit() {
await Resource.save(resource, { isNew: true });
return resource;
}
shim.attachFileToNote = async function(note, filePath, position = null) {
const resource = await shim.createResourceFromPath(filePath);
const newBody = [];
if (position === null) {
@ -142,6 +168,20 @@ function shimInit() {
return await Note.save(newNote);
}
shim.imageFromDataUrl = async function(imageDataUrl, filePath, options = null) {
if (options === null) options = {};
if (shim.isElectron()) {
const nativeImage = require('electron').nativeImage;
let image = nativeImage.createFromDataURL(imageDataUrl);
if (options.cropRect) image = image.crop(options.cropRect);
const mime = mimeUtils.fromDataUrl(imageDataUrl);
await shim.writeImageToFile(image, mime, filePath);
} else {
throw new Error('Node support not implemented');
}
}
const nodeFetch = require('node-fetch');
shim.readLocalFileBase64 = (path) => {