2017-12-03 12:41:18 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const fs = require('fs');
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const data_dir = require('../../services/data_dir');
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sync_table = require('../../services/sync_table');
|
2017-12-23 22:57:20 +08:00
|
|
|
const auth = require('../../services/auth');
|
2018-01-07 22:35:44 +08:00
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2017-12-03 12:41:18 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.get('/:directory/to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-12-03 12:41:18 +08:00
|
|
|
const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, '');
|
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
|
|
|
|
const dir = data_dir.EXPORT_DIR + '/' + directory;
|
|
|
|
|
|
|
|
await sql.doInTransaction(async () => await importNotes(dir, parentNoteId));
|
|
|
|
|
|
|
|
res.send({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-12-03 12:41:18 +08:00
|
|
|
|
|
|
|
async function importNotes(dir, parentNoteId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const parent = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
|
2017-12-03 12:41:18 +08:00
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileList = fs.readdirSync(dir);
|
|
|
|
|
|
|
|
for (const file of fileList) {
|
|
|
|
const path = dir + '/' + file;
|
|
|
|
|
|
|
|
if (fs.lstatSync(path).isDirectory()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.endsWith('.html')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-03 13:10:43 +08:00
|
|
|
const fileNameWithoutExt = file.substr(0, file.length - 5);
|
|
|
|
|
|
|
|
let noteTitle;
|
|
|
|
let notePos;
|
2017-12-03 12:41:18 +08:00
|
|
|
|
2017-12-03 13:10:43 +08:00
|
|
|
const match = fileNameWithoutExt.match(/^([0-9]{4})-(.*)$/);
|
|
|
|
if (match) {
|
|
|
|
notePos = parseInt(match[1]);
|
|
|
|
noteTitle = match[2];
|
|
|
|
}
|
|
|
|
else {
|
2018-01-29 08:38:05 +08:00
|
|
|
let maxPos = await sql.getFirstValue("SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
|
2017-12-03 13:10:43 +08:00
|
|
|
if (maxPos) {
|
|
|
|
notePos = maxPos + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
notePos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
noteTitle = fileNameWithoutExt;
|
2017-12-03 12:41:18 +08:00
|
|
|
}
|
|
|
|
|
2017-12-03 13:10:43 +08:00
|
|
|
const noteText = fs.readFileSync(path, "utf8");
|
|
|
|
|
2017-12-03 12:41:18 +08:00
|
|
|
const noteId = utils.newNoteId();
|
2018-01-29 08:38:05 +08:00
|
|
|
const noteTreeId = utils.newnoteRevisionId();
|
2017-12-03 12:41:18 +08:00
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
const now = utils.nowDate();
|
|
|
|
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.insert('note_tree', {
|
2018-01-29 08:30:14 +08:00
|
|
|
noteTreeId: noteTreeId,
|
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
notePosition: notePos,
|
|
|
|
isExpanded: 0,
|
|
|
|
isDeleted: 0,
|
|
|
|
dateModified: now
|
2017-12-03 12:41:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await sync_table.addNoteTreeSync(noteTreeId);
|
|
|
|
|
|
|
|
await sql.insert('notes', {
|
2018-01-29 08:30:14 +08:00
|
|
|
noteId: noteId,
|
|
|
|
title: noteTitle,
|
|
|
|
content: noteText,
|
|
|
|
isDeleted: 0,
|
|
|
|
isProtected: 0,
|
2018-01-22 12:36:09 +08:00
|
|
|
type: 'text',
|
|
|
|
mime: 'text/html',
|
2018-01-29 08:30:14 +08:00
|
|
|
dateCreated: now,
|
|
|
|
dateModified: now
|
2017-12-03 12:41:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await sync_table.addNoteSync(noteId);
|
|
|
|
|
2017-12-03 13:10:43 +08:00
|
|
|
const noteDir = dir + '/' + fileNameWithoutExt;
|
2017-12-03 12:41:18 +08:00
|
|
|
|
|
|
|
if (fs.existsSync(noteDir) && fs.lstatSync(noteDir).isDirectory()) {
|
|
|
|
await importNotes(noteDir, noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = router;
|