2018-02-15 12:31:20 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const notes = require('../../services/notes');
|
2018-03-25 10:02:26 +08:00
|
|
|
const labels = require('../../services/labels');
|
2018-02-24 11:58:24 +08:00
|
|
|
const protected_session = require('../../services/protected_session');
|
2018-02-15 12:31:20 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function uploadFile(req) {
|
2018-02-15 12:31:20 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const file = req.file;
|
2018-02-19 10:28:24 +08:00
|
|
|
const originalName = file.originalname;
|
|
|
|
const size = file.size;
|
2018-02-15 12:31:20 +08:00
|
|
|
|
|
|
|
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
|
|
|
|
|
|
|
|
if (!note) {
|
2018-03-31 05:29:13 +08:00
|
|
|
return [404, `Note ${parentNoteId} doesn't exist.`];
|
2018-02-15 12:31:20 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
const {noteId} = await notes.createNewNote(parentNoteId, {
|
|
|
|
title: originalName,
|
|
|
|
content: file.buffer,
|
|
|
|
target: 'into',
|
|
|
|
isProtected: false,
|
|
|
|
type: 'file',
|
|
|
|
mime: file.mimetype
|
2018-03-31 07:41:54 +08:00
|
|
|
}, req);
|
2018-03-31 05:29:13 +08:00
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
await labels.createLabel(noteId, "original_file_name", originalName);
|
|
|
|
await labels.createLabel(noteId, "file_size", size);
|
2018-03-31 05:29:13 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
noteId: noteId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function downloadFile(req, res) {
|
2018-02-19 10:28:24 +08:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
2018-02-24 11:58:24 +08:00
|
|
|
const protectedSessionId = req.query.protectedSessionId;
|
2018-02-19 10:28:24 +08:00
|
|
|
|
|
|
|
if (!note) {
|
2018-02-24 11:58:24 +08:00
|
|
|
return res.status(404).send(`Note ${noteId} doesn't exist.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.isProtected) {
|
|
|
|
const dataKey = protected_session.getDataKeyForProtectedSessionId(protectedSessionId);
|
|
|
|
|
|
|
|
if (!dataKey) {
|
|
|
|
res.status(401).send("Protected session not available");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected_session.decryptNote(dataKey, note);
|
2018-02-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2018-03-25 10:02:26 +08:00
|
|
|
const labelMap = await labels.getNoteLabelMap(noteId);
|
|
|
|
const fileName = labelMap.original_file_name ? labelMap.original_file_name : note.title;
|
2018-02-19 10:28:24 +08:00
|
|
|
|
2018-03-28 10:11:06 +08:00
|
|
|
res.setHeader('Content-Disposition', 'file; filename=' + fileName);
|
2018-02-19 10:28:24 +08:00
|
|
|
res.setHeader('Content-Type', note.mime);
|
|
|
|
|
|
|
|
res.send(note.content);
|
2018-03-31 05:29:13 +08:00
|
|
|
}
|
2018-02-19 10:28:24 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
module.exports = {
|
|
|
|
uploadFile,
|
|
|
|
downloadFile
|
|
|
|
};
|