smaller refactorings continued

This commit is contained in:
azivner
2018-04-01 11:42:12 -04:00
parent fad0ec757b
commit acc82f39c4
13 changed files with 96 additions and 100 deletions

View File

@@ -1,9 +1,9 @@
"use strict";
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const labels = require('../../services/labels');
const protected_session = require('../../services/protected_session');
const repository = require('../../services/repository');
async function uploadFile(req) {
const parentNoteId = req.params.parentNoteId;
@@ -11,50 +11,46 @@ async function uploadFile(req) {
const originalName = file.originalname;
const size = file.size;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
const parentNote = await repository.getNote(parentNoteId);
if (!note) {
if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
}
const {noteId} = await notes.createNewNote(parentNoteId, {
const {note} = await notes.createNewNote(parentNoteId, {
title: originalName,
content: file.buffer,
target: 'into',
isProtected: false,
type: 'file',
mime: file.mimetype
}, req);
});
await labels.createLabel(noteId, "original_file_name", originalName);
await labels.createLabel(noteId, "file_size", size);
await labels.createLabel(note.noteId, "original_file_name", originalName);
await labels.createLabel(note.noteId, "file_size", size);
return {
noteId: noteId
noteId: note.noteId
};
}
async function downloadFile(req, res) {
const noteId = req.params.noteId;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await repository.getNote(noteId);
if (!note) {
return res.status(404).send(`Note ${noteId} doesn't exist.`);
}
if (note.isProtected) {
if (!protected_session.isProtectedSessionAvailable()) {
res.status(401).send("Protected session not available");
return;
}
protected_session.decryptNote(note);
if (note.isProtected && !protected_session.isProtectedSessionAvailable()) {
res.status(401).send("Protected session not available");
return;
}
const labelMap = await labels.getNoteLabelMap(noteId);
const labelMap = await note.getLabelMap();
const fileName = labelMap.original_file_name ? labelMap.original_file_name : note.title;
res.setHeader('Content-Disposition', 'file; filename=' + fileName);
res.setHeader('Content-Disposition', 'file; filename="' + fileName + '"');
res.setHeader('Content-Type', note.mime);
res.send(note.content);

View File

@@ -1,12 +1,12 @@
"use strict";
const sql = require('../../services/sql');
const image = require('../../services/image');
const repository = require('../../services/repository');
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
const fs = require('fs');
async function returnImage(req, res) {
const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);
const image = await repository.getImage(req.params.imageId);
if (!image) {
return res.sendStatus(404);
@@ -25,7 +25,7 @@ async function uploadImage(req) {
const noteId = req.query.noteId;
const file = req.file;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await repository.getNote(noteId);
if (!note) {
return [404, `Note ${noteId} doesn't exist.`];

View File

@@ -1,6 +1,6 @@
"use strict";
const sql = require('../../services/sql');
const repository = require('../../services/repository');
const labels = require('../../services/labels');
const notes = require('../../services/notes');
const tar = require('tar-stream');
@@ -89,9 +89,9 @@ async function importTar(req) {
const parentNoteId = req.params.parentNoteId;
const file = req.file;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
const parentNote = await repository.getNote(parentNoteId);
if (!note) {
if (!parentNote) {
return [404, `Note ${parentNoteId} doesn't exist.`];
}

View File

@@ -1,25 +1,26 @@
"use strict";
const sql = require('../../services/sql');
const sync_table = require('../../services/sync_table');
const utils = require('../../services/utils');
const labels = require('../../services/labels');
const repository = require('../../services/repository');
const Label = require('../../entities/label');
async function getNoteLabels(req) {
const noteId = req.params.noteId;
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
async function updateNoteLabels(req, res, next) {
async function updateNoteLabels(req) {
const noteId = req.params.noteId;
const labels = req.body;
const now = utils.nowDate();
for (const label of labels) {
let labelEntity;
if (label.labelId) {
await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
[label.name, label.value, now, label.isDeleted, label.position, label.labelId]);
labelEntity = await repository.getLabel(label.labelId);
}
else {
// if it was "created" and then immediatelly deleted, we just don't create it at all
@@ -27,27 +28,23 @@ async function updateNoteLabels(req, res, next) {
continue;
}
label.labelId = utils.newLabelId();
await sql.insert("labels", {
labelId: label.labelId,
noteId: noteId,
name: label.name,
value: label.value,
position: label.position,
dateCreated: now,
dateModified: now,
isDeleted: false
});
labelEntity = new Label();
labelEntity.labelId = utils.newLabelId();
labelEntity.noteId = noteId;
}
await sync_table.addLabelSync(label.labelId);
labelEntity.name = label.name;
labelEntity.value = label.value;
labelEntity.position = label.position;
labelEntity.isDeleted = label.isDeleted;
await labelEntity.save();
}
return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
}
async function getAllLabelNames(req) {
async function getAllLabelNames() {
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
for (const label of labels.BUILTIN_LABELS) {

View File

@@ -3,22 +3,18 @@
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const utils = require('../../services/utils');
const protected_session = require('../../services/protected_session');
const tree = require('../../services/tree');
const sync_table = require('../../services/sync_table');
const repository = require('../../services/repository');
async function getNote(req) {
const noteId = req.params.noteId;
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
const note = await repository.getNote(noteId);
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
protected_session.decryptNote(note);
if (note.type === 'file') {
// no need to transfer (potentially large) file payload for this request
note.content = null;
@@ -31,12 +27,11 @@ async function createNote(req) {
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req);
const { note, branch } = await notes.createNewNote(parentNoteId, newNote, req);
return {
'noteId': noteId,
'branchId': branchId,
'note': note
note,
branch
};
}

View File

@@ -47,7 +47,7 @@ async function uploadImage(req) {
const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);
const {noteId} = await notes.createNewNote(parentNoteId, {
const {note} = await notes.createNewNote(parentNoteId, {
title: "Sender image",
content: "",
target: 'into',
@@ -56,13 +56,13 @@ async function uploadImage(req) {
mime: 'text/html'
});
const {fileName, imageId} = await image.saveImage(file, null, noteId);
const {fileName, imageId} = await image.saveImage(file, null, note.noteId);
const url = `/api/images/${imageId}/${fileName}`;
const content = `<img src="${url}"/>`;
await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, noteId]);
await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, note.noteId]);
}
async function saveNote(req) {