trilium/src/routes/api/export.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-12-03 10:48:22 +08:00
"use strict";
2020-03-20 23:55:35 +08:00
const zipExportService = require('../../services/export/zip');
2018-11-25 03:58:38 +08:00
const singleExportService = require('../../services/export/single');
const opmlExportService = require('../../services/export/opml');
2018-03-31 21:07:58 +08:00
const repository = require("../../services/repository");
2019-10-19 04:27:38 +08:00
const TaskContext = require("../../services/task_context");
2019-02-11 05:30:55 +08:00
const log = require("../../services/log");
2017-12-03 10:48:22 +08:00
2019-02-11 05:30:55 +08:00
async function exportBranch(req, res) {
2019-10-19 04:27:38 +08:00
const {branchId, type, format, version, taskId} = req.params;
2019-02-11 05:30:55 +08:00
const branch = await repository.getBranch(branchId);
2020-05-18 03:07:54 +08:00
if (!branch) {
const message = `Cannot export branch ${branchId} since it does not exist.`;
log.error(message);
res.status(500).send(message);
return;
}
2019-10-19 04:27:38 +08:00
const taskContext = new TaskContext(taskId, 'export');
2019-02-11 05:30:55 +08:00
try {
if (type === 'subtree' && (format === 'html' || format === 'markdown')) {
2020-06-18 05:03:46 +08:00
const start = Date.now();
await zipExportService.exportToZip(taskContext, branch, format, res);
2020-06-18 05:03:46 +08:00
console.log("Export took", Date.now() - start, "ms");
2019-02-11 05:30:55 +08:00
}
else if (type === 'single') {
2019-10-19 04:27:38 +08:00
await singleExportService.exportSingleNote(taskContext, branch, format, res);
2019-02-11 05:30:55 +08:00
}
else if (format === 'opml') {
2019-10-19 04:27:38 +08:00
await opmlExportService.exportToOpml(taskContext, branch, version, res);
2019-02-11 05:30:55 +08:00
}
else {
return [404, "Unrecognized export format " + format];
}
2018-09-03 05:39:10 +08:00
}
2019-02-11 05:30:55 +08:00
catch (e) {
const message = "Export failed with following error: '" + e.message + "'. More details might be in the logs.";
2019-10-19 04:27:38 +08:00
taskContext.reportError(message);
2019-02-11 05:30:55 +08:00
log.error(message + e.stack);
res.status(500).send(message);
}
}
2018-03-31 03:34:07 +08:00
module.exports = {
2018-11-25 03:58:38 +08:00
exportBranch
2020-05-18 03:07:54 +08:00
};