saving image

This commit is contained in:
azivner
2018-11-08 11:08:16 +01:00
parent 21e952a7f0
commit d7afbe4059
9 changed files with 215 additions and 261 deletions

View File

@@ -480,13 +480,6 @@ class Note extends Entity {
return await repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
}
/**
* @returns {Promise<NoteImage[]>}
*/
async getNoteImages() {
return await repository.getEntities("SELECT * FROM note_images WHERE noteId = ? AND isDeleted = 0", [this.noteId]);
}
/**
* @returns {Promise<Link[]>}
*/
@@ -494,6 +487,15 @@ class Note extends Entity {
return await repository.getEntities("SELECT * FROM links WHERE noteId = ? AND isDeleted = 0", [this.noteId]);
}
/**
* Return all links from this note, including deleted ones.
*
* @returns {Promise<Link[]>}
*/
async getLinksWithDeleted() {
return await repository.getEntities("SELECT * FROM links WHERE noteId = ?", [this.noteId]);
}
/**
* @returns {Promise<Branch[]>}
*/

View File

@@ -37,6 +37,7 @@ window.glob.getHeaders = server.getHeaders;
window.glob.showAddLinkDialog = addLinkDialog.showDialog;
// this is required by CKEditor when uploading images
window.glob.noteChanged = noteDetailService.noteChanged;
window.glob.refreshTree = treeService.reload;
// required for ESLint plugin
window.glob.getCurrentNote = noteDetailService.getCurrentNote;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -47,9 +47,7 @@ async function uploadImage(req) {
mime: 'text/html'
});
const {fileName, imageId} = await imageService.saveImage(file, null, note.noteId);
const url = `/api/images/${imageId}/${fileName}`;
const {url} = await imageService.saveImage(file, null, note.noteId);
const content = `<img src="${url}"/>`;

View File

@@ -1,8 +1,7 @@
"use strict";
const utils = require('./utils');
const Image = require('../entities/image');
const NoteImage = require('../entities/note_image');
const noteService = require('./notes');
const imagemin = require('imagemin');
const imageminMozJpeg = require('imagemin-mozjpeg');
const imageminPngQuant = require('imagemin-pngquant');
@@ -20,22 +19,18 @@ async function saveImage(buffer, originalName, noteId) {
const fileNameWithoutExtension = originalName.replace(/\.[^/.]+$/, "");
const fileName = sanitizeFilename(fileNameWithoutExtension + "." + imageFormat.ext);
const image = await new Image({
format: imageFormat.ext,
name: fileName,
checksum: utils.hash(optimizedImage),
data: optimizedImage
}).save();
await new NoteImage({
noteId: noteId,
imageId: image.imageId
}).save();
const {note} = await noteService.createNewNote(noteId, {
target: 'into',
title: fileName,
content: optimizedImage,
type: 'image',
mime: 'image/' + imageFormat.ext
});
return {
fileName,
imageId: image.imageId,
url: `/api/images/${image.imageId}/${fileName}`
noteId: note.noteId,
url: `/api/images/${note.noteId}/${fileName}`
};
}

View File

@@ -7,7 +7,7 @@ const eventService = require('./events');
const repository = require('./repository');
const cls = require('../services/cls');
const Note = require('../entities/note');
const NoteImage = require('../entities/note_image');
const Link = require('../entities/link');
const NoteRevision = require('../entities/note_revision');
const Branch = require('../entities/branch');
const Attribute = require('../entities/attribute');
@@ -45,6 +45,9 @@ async function triggerNoteTitleChanged(note) {
await eventService.emit(eventService.NOTE_TITLE_CHANGED, note);
}
/**
* FIXME: noteData has mandatory property "target", it might be better to add it as parameter to reflect this
*/
async function createNewNote(parentNoteId, noteData) {
const newNotePos = await getNewNotePosition(parentNoteId, noteData);
@@ -67,7 +70,7 @@ async function createNewNote(parentNoteId, noteData) {
const note = await new Note({
title: noteData.title,
content: noteData.content || '',
content: noteData.content,
isProtected: noteData.isProtected,
type: noteData.type || 'text',
mime: noteData.mime || 'text/html'
@@ -135,8 +138,6 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
});
}
await triggerNoteTitleChanged(note);
return {note, branch};
}
@@ -168,38 +169,42 @@ async function protectNoteRevisions(note) {
}
}
async function saveNoteImages(note) {
async function saveLinks(note) {
if (note.type !== 'text') {
return;
}
const existingNoteImages = await note.getNoteImages();
const foundImageIds = [];
const existingLinks = await note.getLinks();
const foundNoteIds = [];
const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g;
let match;
while (match = re.exec(note.content)) {
const imageId = match[1];
const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId);
const targetNoteId = match[1];
const existingLink = existingLinks.find(link => link.targetNoteId === targetNoteId && link.type === 'image');
if (!existingNoteImage) {
await new NoteImage({
if (!existingLink) {
await new Link({
noteId: note.noteId,
imageId: imageId
targetNoteId,
type: 'image'
}).save();
}
// else we don't need to do anything
else if (existingLink.isDeleted) {
existingLink.isDeleted = false;
await existingLink.save();
}
// else the link exists so we don't need to do anything
foundImageIds.push(imageId);
foundNoteIds.push(targetNoteId);
}
// marking note images as deleted if they are not present on the page anymore
const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.imageId));
// marking links as deleted if they are not present on the page anymore
const unusedLinks = existingLinks.filter(link => !foundNoteIds.includes(link.noteId));
for (const unusedNoteImage of unusedNoteImages) {
unusedNoteImage.isDeleted = true;
await unusedNoteImage.save();
for (const unusedLink of unusedLinks) {
unusedLink.isDeleted = true;
await unusedLink.save();
}
}
@@ -258,7 +263,7 @@ async function updateNote(noteId, noteUpdates) {
await triggerNoteTitleChanged(note);
}
await saveNoteImages(note);
await saveLinks(note);
await protectNoteRevisions(note);
}