uploaded attachments with image mime are saved as image notes, closes #211

This commit is contained in:
azivner 2018-11-15 12:13:32 +01:00
parent 8ee8639faa
commit 346b8c21dd
6 changed files with 73 additions and 34 deletions

View File

@ -5,11 +5,11 @@ import noteDetailService from "./note_detail.js";
const $component = $('#note-detail-file'); const $component = $('#note-detail-file');
const $fileFileName = $("#file-filename"); const $fileName = $("#file-filename");
const $fileFileType = $("#file-filetype"); const $fileType = $("#file-filetype");
const $fileFileSize = $("#file-filesize"); const $fileSize = $("#file-filesize");
const $fileDownload = $("#file-download"); const $downloadButton = $("#file-download");
const $fileOpen = $("#file-open"); const $openButton = $("#file-open");
async function show() { async function show() {
const currentNote = noteDetailService.getCurrentNote(); const currentNote = noteDetailService.getCurrentNote();
@ -19,14 +19,14 @@ async function show() {
$component.show(); $component.show();
$fileFileName.text(attributeMap.originalFileName); $fileName.text(attributeMap.originalFileName);
$fileFileSize.text(attributeMap.fileSize + " bytes"); $fileSize.text(attributeMap.fileSize + " bytes");
$fileFileType.text(currentNote.mime); $fileType.text(currentNote.mime);
} }
$fileDownload.click(() => utils.download(getFileUrl())); $downloadButton.click(() => utils.download(getFileUrl()));
$fileOpen.click(() => { $openButton.click(() => {
if (utils.isElectron()) { if (utils.isElectron()) {
const open = require("open"); const open = require("open");

View File

@ -2,22 +2,33 @@ import utils from "./utils.js";
import protectedSessionHolder from "./protected_session_holder.js"; import protectedSessionHolder from "./protected_session_holder.js";
import noteDetailService from "./note_detail.js"; import noteDetailService from "./note_detail.js";
import infoService from "./info.js"; import infoService from "./info.js";
import server from "./server.js";
const $component = $('#note-detail-image'); const $component = $('#note-detail-image');
const $imageView = $('#note-detail-image-view'); const $imageView = $('#note-detail-image-view');
const $imageDownload = $("#image-download"); const $imageDownloadButton = $("#image-download");
const $copyToClipboardDownload = $("#image-copy-to-clipboard"); const $copyToClipboardButton = $("#image-copy-to-clipboard");
const $fileName = $("#image-filename");
const $fileType = $("#image-filetype");
const $fileSize = $("#image-filesize");
async function show() { async function show() {
const currentNote = noteDetailService.getCurrentNote(); const currentNote = noteDetailService.getCurrentNote();
const attributes = await server.get('notes/' + currentNote.noteId + '/attributes');
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);
$component.show(); $component.show();
$fileName.text(attributeMap.originalFileName);
$fileSize.text(attributeMap.fileSize + " bytes");
$fileType.text(currentNote.mime);
$imageView.prop("src", `/api/images/${currentNote.noteId}/${currentNote.title}`); $imageView.prop("src", `/api/images/${currentNote.noteId}/${currentNote.title}`);
} }
$imageDownload.click(() => utils.download(getFileUrl())); $imageDownloadButton.click(() => utils.download(getFileUrl()));
function selectImage(element) { function selectImage(element) {
const selection = window.getSelection(); const selection = window.getSelection();
@ -27,7 +38,7 @@ function selectImage(element) {
selection.addRange(range); selection.addRange(range);
} }
$copyToClipboardDownload.click(() => { $copyToClipboardButton.click(() => {
$component.attr('contenteditable','true'); $component.attr('contenteditable','true');
try { try {

View File

@ -455,6 +455,14 @@ html.theme-dark body {
padding: 5px; padding: 5px;
} }
#note-detail-image {
text-align: center;
}
#note-detail-image-view {
max-width: 100%;
}
.pointer { .pointer {
cursor: pointer; cursor: pointer;
} }

View File

@ -1,7 +1,6 @@
"use strict"; "use strict";
const noteService = require('../../services/notes'); const noteService = require('../../services/notes');
const attributeService = require('../../services/attributes');
const protectedSessionService = require('../../services/protected_session'); const protectedSessionService = require('../../services/protected_session');
const repository = require('../../services/repository'); const repository = require('../../services/repository');
@ -10,6 +9,7 @@ async function uploadFile(req) {
const file = req.file; const file = req.file;
const originalName = file.originalname; const originalName = file.originalname;
const size = file.size; const size = file.size;
const mime = file.mimetype.toLowerCase();
const parentNote = await repository.getNote(parentNoteId); const parentNote = await repository.getNote(parentNoteId);
@ -17,18 +17,17 @@ async function uploadFile(req) {
return [404, `Note ${parentNoteId} doesn't exist.`]; return [404, `Note ${parentNoteId} doesn't exist.`];
} }
const {note} = await noteService.createNewNote(parentNoteId, { const {note} = await noteService.createNote(parentNoteId, originalName, file.buffer, {
title: originalName,
content: file.buffer,
target: 'into', target: 'into',
isProtected: false, isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
type: 'file', type: mime.startsWith("image/") ? 'image' : 'file',
mime: file.mimetype mime: file.mimetype,
attributes: [
{ type: "label", name: "originalFileName", value: originalName },
{ type: "label", name: "fileSize", value: size }
]
}); });
await attributeService.createLabel(note.noteId, "originalFileName", originalName);
await attributeService.createLabel(note.noteId, "fileSize", size);
return { return {
noteId: note.noteId noteId: note.noteId
}; };

View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
const utils = require('./utils'); const repository = require('./repository');
const protectedSessionService = require('./protected_session');
const noteService = require('./notes'); const noteService = require('./notes');
const imagemin = require('imagemin'); const imagemin = require('imagemin');
const imageminMozJpeg = require('imagemin-mozjpeg'); const imageminMozJpeg = require('imagemin-mozjpeg');
@ -10,21 +11,26 @@ const jimp = require('jimp');
const imageType = require('image-type'); const imageType = require('image-type');
const sanitizeFilename = require('sanitize-filename'); const sanitizeFilename = require('sanitize-filename');
async function saveImage(buffer, originalName, noteId) { async function saveImage(buffer, originalName, parentNoteId) {
const resizedImage = await resize(buffer); const resizedImage = await resize(buffer);
const optimizedImage = await optimize(resizedImage); const optimizedImage = await optimize(resizedImage);
const imageFormat = imageType(optimizedImage); const imageFormat = imageType(optimizedImage);
const parentNote = await repository.getNote(parentNoteId);
const fileNameWithoutExtension = originalName.replace(/\.[^/.]+$/, ""); const fileNameWithoutExtension = originalName.replace(/\.[^/.]+$/, "");
const fileName = sanitizeFilename(fileNameWithoutExtension + "." + imageFormat.ext); const fileName = sanitizeFilename(fileNameWithoutExtension + "." + imageFormat.ext);
const {note} = await noteService.createNewNote(noteId, { const {note} = await noteService.createNote(parentNoteId, fileName, optimizedImage, {
target: 'into', target: 'into',
title: fileName,
content: optimizedImage,
type: 'image', type: 'image',
mime: 'image/' + imageFormat.ext isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
mime: 'image/' + imageFormat.ext.toLowerCase(),
attributes: [
{ type: 'label', name: 'originalFileName', value: originalName },
{ type: 'label', name: 'fileSize', value: optimizedImage.byteLength }
]
}); });
return { return {

View File

@ -1,11 +1,26 @@
<div id="note-detail-image" class="note-detail-component"> <div id="note-detail-image" class="note-detail-component">
<img id="note-detail-image-view" /> <strong>File name:</strong>
<span id="image-filename"></span>
&nbsp; &nbsp;
<strong>File type:</strong>
<span id="image-filetype"></span>
&nbsp; &nbsp;
<strong>File size:</strong>
<span id="image-filesize"></span>
<br/><br/>
<br/>
<br/>
<button id="image-download" class="btn btn-primary" type="button">Download</button> <button id="image-download" class="btn btn-primary" type="button">Download</button>
&nbsp; &nbsp; &nbsp;
<button id="image-copy-to-clipboard" class="btn btn-primary" type="button">Copy to clipboard</button> <button id="image-copy-to-clipboard" class="btn btn-primary" type="button">Copy to clipboard</button>
<br/><br/>
<img id="note-detail-image-view" />
</div> </div>