1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-06 09:19:22 +02:00

Allow attaching files in Electron ap

This commit is contained in:
Laurent Cozic
2017-11-10 22:18:00 +00:00
parent f543015714
commit 49c11fb22e
15 changed files with 437 additions and 86 deletions

View File

@@ -2,15 +2,8 @@ const { BaseCommand } = require('./base-command.js');
const { app } = require('./app.js');
const { _ } = require('lib/locale.js');
const { BaseModel } = require('lib/base-model.js');
const { Folder } = require('lib/models/folder.js');
const { Note } = require('lib/models/note.js');
const { Resource } = require('lib/models/resource.js');
const { uuid } = require('lib/uuid.js');
const { filename } = require('lib/path-utils.js');
const { shim } = require('lib/shim.js');
const fs = require('fs-extra');
const mime = require('mime/lite');
const sharp = require('sharp');
class Command extends BaseCommand {
@@ -22,22 +15,6 @@ class Command extends BaseCommand {
return _('Attaches the given file to the note.');
}
resizeImage_(filePath, targetPath) {
return new Promise((resolve, reject) => {
sharp(filePath)
.resize(Resource.IMAGE_MAX_DIMENSION, Resource.IMAGE_MAX_DIMENSION)
.max()
.withoutEnlargement()
.toFile(targetPath, (err, info) => {
if (err) {
reject(err);
} else {
resolve(info);
}
});
});
}
async action(args) {
let title = args['note'];
@@ -45,26 +22,8 @@ class Command extends BaseCommand {
if (!note) throw new Error(_('Cannot find "%s".', title));
const localFilePath = args['file'];
if (!(await fs.pathExists(localFilePath))) throw new Error(_('Cannot access %s', localFilePath));
let resource = Resource.new();
resource.id = uuid.create();
resource.mime = mime.getType(localFilePath);
resource.title = filename(localFilePath);
let targetPath = Resource.fullPath(resource);
if (resource.mime == 'image/jpeg' || resource.mime == 'image/jpg' || resource.mime == 'image/png') {
const result = await this.resizeImage_(localFilePath, targetPath);
this.logger().info(result);
} else {
await fs.copy(localFilePath, targetPath, { overwrite: true });
}
await Resource.save(resource, { isNew: true });
note.body += "\n\n" + Resource.markdownTag(resource);
await Note.save(note);
await shim.attachFileToNote(note, localFilePath);
}
}