mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 10:40:41 +01:00
converted of web (non-api) routes, basic conversion completed
This commit is contained in:
@@ -1,12 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const sql = require('../../services/sql');
|
||||
const auth = require('../../services/auth');
|
||||
const image = require('../../services/image');
|
||||
const multer = require('multer')();
|
||||
const wrap = require('express-promise-wrap').wrap;
|
||||
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
|
||||
const fs = require('fs');
|
||||
|
||||
@@ -14,7 +9,7 @@ async function returnImage(req, res) {
|
||||
const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);
|
||||
|
||||
if (!image) {
|
||||
return res.status(404).send({});
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
else if (image.data === null) {
|
||||
res.set('Content-Type', 'image/png');
|
||||
@@ -26,7 +21,7 @@ async function returnImage(req, res) {
|
||||
res.send(image.data);
|
||||
}
|
||||
|
||||
async function uploadImage(req, res) {
|
||||
async function uploadImage(req) {
|
||||
const sourceId = req.headers.source_id;
|
||||
const noteId = req.query.noteId;
|
||||
const file = req.file;
|
||||
@@ -34,19 +29,19 @@ async function uploadImage(req, res) {
|
||||
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
||||
|
||||
if (!note) {
|
||||
return res.status(404).send(`Note ${noteId} doesn't exist.`);
|
||||
return [404, `Note ${noteId} doesn't exist.`];
|
||||
}
|
||||
|
||||
if (!["image/png", "image/jpeg", "image/gif"].includes(file.mimetype)) {
|
||||
return res.status(400).send("Unknown image type: " + file.mimetype);
|
||||
return [400, "Unknown image type: " + file.mimetype];
|
||||
}
|
||||
|
||||
const {fileName, imageId} = await image.saveImage(file, sourceId, noteId);
|
||||
|
||||
res.send({
|
||||
return {
|
||||
uploaded: true,
|
||||
url: `/api/images/${imageId}/${fileName}`
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -90,7 +90,7 @@ async function parseImportFile(file) {
|
||||
});
|
||||
}
|
||||
|
||||
async function importTar(req, res) {
|
||||
async function importTar(req) {
|
||||
const sourceId = req.headers.source_id;
|
||||
const parentNoteId = req.params.parentNoteId;
|
||||
const file = req.file;
|
||||
@@ -98,14 +98,12 @@ async function importTar(req, res) {
|
||||
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
|
||||
|
||||
if (!note) {
|
||||
return res.status(404).send(`Note ${parentNoteId} doesn't exist.`);
|
||||
return [404, `Note ${parentNoteId} doesn't exist.`];
|
||||
}
|
||||
|
||||
const files = await parseImportFile(file);
|
||||
|
||||
await importNotes(files, parentNoteId, sourceId);
|
||||
|
||||
res.send({});
|
||||
}
|
||||
|
||||
async function importNotes(files, parentNoteId, sourceId) {
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const options = require('../../services/options');
|
||||
const utils = require('../../services/utils');
|
||||
const source_id = require('../../services/source_id');
|
||||
const auth = require('../../services/auth');
|
||||
const password_encryption = require('../../services/password_encryption');
|
||||
const protected_session = require('../../services/protected_session');
|
||||
const app_info = require('../../services/app_info');
|
||||
const wrap = require('express-promise-wrap').wrap;
|
||||
|
||||
router.post('/sync', wrap(async (req, res, next) => {
|
||||
async function loginSync(req) {
|
||||
const timestampStr = req.body.timestamp;
|
||||
|
||||
const timestamp = utils.parseDateTime(timestampStr);
|
||||
@@ -19,15 +15,13 @@ router.post('/sync', wrap(async (req, res, next) => {
|
||||
const now = new Date();
|
||||
|
||||
if (Math.abs(timestamp.getTime() - now.getTime()) > 5000) {
|
||||
res.status(400);
|
||||
res.send({ message: 'Auth request time is out of sync' });
|
||||
return [400, { message: 'Auth request time is out of sync' }];
|
||||
}
|
||||
|
||||
const dbVersion = req.body.dbVersion;
|
||||
|
||||
if (dbVersion !== app_info.db_version) {
|
||||
res.status(400);
|
||||
res.send({ message: 'Non-matching db versions, local is version ' + app_info.db_version });
|
||||
return [400, { message: 'Non-matching db versions, local is version ' + app_info.db_version }];
|
||||
}
|
||||
|
||||
const documentSecret = await options.getOption('document_secret');
|
||||
@@ -36,38 +30,37 @@ router.post('/sync', wrap(async (req, res, next) => {
|
||||
const givenHash = req.body.hash;
|
||||
|
||||
if (expectedHash !== givenHash) {
|
||||
res.status(400);
|
||||
res.send({ message: "Sync login hash doesn't match" });
|
||||
return [400, { message: "Sync login hash doesn't match" }];
|
||||
}
|
||||
|
||||
req.session.loggedIn = true;
|
||||
|
||||
res.send({
|
||||
return {
|
||||
sourceId: source_id.getCurrentSourceId()
|
||||
});
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
// this is for entering protected mode so user has to be already logged-in (that's the reason we don't require username)
|
||||
router.post('/protected', auth.checkApiAuth, wrap(async (req, res, next) => {
|
||||
async function loginToProtectedSession(req) {
|
||||
const password = req.body.password;
|
||||
|
||||
if (!await password_encryption.verifyPassword(password)) {
|
||||
res.send({
|
||||
return {
|
||||
success: false,
|
||||
message: "Given current password doesn't match hash"
|
||||
});
|
||||
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
const decryptedDataKey = await password_encryption.getDataKey(password);
|
||||
|
||||
const protectedSessionId = protected_session.setDataKey(req, decryptedDataKey);
|
||||
|
||||
res.send({
|
||||
return {
|
||||
success: true,
|
||||
protectedSessionId: protectedSessionId
|
||||
});
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
module.exports = {
|
||||
loginSync,
|
||||
loginToProtectedSession
|
||||
};
|
||||
@@ -1,26 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const auth = require('../../services/auth');
|
||||
const options = require('../../services/options');
|
||||
const migration = require('../../services/migration');
|
||||
const app_info = require('../../services/app_info');
|
||||
const wrap = require('express-promise-wrap').wrap;
|
||||
|
||||
router.get('', auth.checkApiAuthForMigrationPage, wrap(async (req, res, next) => {
|
||||
res.send({
|
||||
async function getMigrationInfo() {
|
||||
return {
|
||||
db_version: parseInt(await options.getOption('db_version')),
|
||||
app_db_version: app_info.db_version
|
||||
});
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
router.post('', auth.checkApiAuthForMigrationPage, wrap(async (req, res, next) => {
|
||||
async function executeMigration() {
|
||||
const migrations = await migration.migrate();
|
||||
|
||||
res.send({
|
||||
return {
|
||||
migrations: migrations
|
||||
});
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
module.exports = {
|
||||
getMigrationInfo,
|
||||
executeMigration
|
||||
};
|
||||
Reference in New Issue
Block a user