trilium/src/routes/api/sender.js

85 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-02-11 13:18:59 +08:00
"use strict";
const image = require('../../services/image');
const utils = require('../../services/utils');
const date_notes = require('../../services/date_notes');
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const password_encryption = require('../../services/password_encryption');
const options = require('../../services/options');
const sync_table = require('../../services/sync_table');
async function login(req) {
2018-02-11 13:18:59 +08:00
const username = req.body.username;
const password = req.body.password;
const isUsernameValid = username === await options.getOption('username');
const isPasswordValid = await password_encryption.verifyPassword(password);
if (!isUsernameValid || !isPasswordValid) {
return [401, "Incorrect username/password"];
2018-02-11 13:18:59 +08:00
}
const token = utils.randomSecureToken();
2018-02-11 13:18:59 +08:00
const apiTokenId = utils.newApiTokenId();
2018-02-11 13:18:59 +08:00
await sql.insert("api_tokens", {
apiTokenId: apiTokenId,
token: token,
dateCreated: utils.nowDate(),
isDeleted: false
});
2018-02-11 13:18:59 +08:00
await sync_table.addApiTokenSync(apiTokenId);
2018-02-11 13:18:59 +08:00
return {
token: token
};
2018-02-11 13:18:59 +08:00
}
async function uploadImage(req) {
2018-02-11 13:18:59 +08:00
const file = req.file;
if (!["image/png", "image/jpeg", "image/gif"].includes(file.mimetype)) {
return [400, "Unknown image type: " + file.mimetype];
2018-02-11 13:18:59 +08:00
}
const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);
2018-02-11 13:18:59 +08:00
const {noteId} = await notes.createNewNote(parentNoteId, {
2018-02-11 13:18:59 +08:00
title: "Sender image",
content: "",
target: 'into',
isProtected: false,
type: 'text',
mime: 'text/html'
});
2018-02-11 13:18:59 +08:00
const {fileName, imageId} = await image.saveImage(file, null, noteId);
const url = `/api/images/${imageId}/${fileName}`;
const content = `<img src="${url}"/>`;
await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, noteId]);
}
2018-02-11 13:18:59 +08:00
async function saveNote(req) {
2018-02-11 23:54:56 +08:00
const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);
await notes.createNewNote(parentNoteId, {
title: req.body.title,
content: req.body.content,
target: 'into',
isProtected: false,
type: 'text',
mime: 'text/html'
});
}
2018-02-11 23:54:56 +08:00
module.exports = {
login,
uploadImage,
saveNote
};