Notes/src/routes/api/export.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-12-02 21:48:22 -05:00
"use strict";
2020-03-20 16:55:35 +01:00
const zipExportService = require('../../services/export/zip');
2018-11-24 20:58:38 +01:00
const singleExportService = require('../../services/export/single');
const opmlExportService = require('../../services/export/opml');
const becca = require('../../becca/becca');
2019-10-18 22:27:38 +02:00
const TaskContext = require("../../services/task_context");
2019-02-10 22:30:55 +01:00
const log = require("../../services/log");
const NotFoundError = require("../../errors/not_found_error");
2017-12-02 21:48:22 -05:00
2020-06-20 12:31:38 +02:00
function exportBranch(req, res) {
2019-10-18 22:27:38 +02:00
const {branchId, type, format, version, taskId} = req.params;
2021-05-02 11:23:58 +02:00
const branch = becca.getBranch(branchId);
2019-02-10 22:30:55 +01:00
2020-05-17 21:07:54 +02:00
if (!branch) {
2023-01-03 21:30:49 +01:00
const message = `Cannot export branch '${branchId}' since it does not exist.`;
2020-05-17 21:07:54 +02:00
log.error(message);
res.setHeader("Content-Type", "text/plain")
.status(500)
.send(message);
2020-05-17 21:07:54 +02:00
return;
}
2019-10-18 22:27:38 +02:00
const taskContext = new TaskContext(taskId, 'export');
2019-02-10 22:30:55 +01:00
try {
if (type === 'subtree' && (format === 'html' || format === 'markdown')) {
2020-06-20 12:31:38 +02:00
zipExportService.exportToZip(taskContext, branch, format, res);
2019-02-10 22:30:55 +01:00
}
else if (type === 'single') {
2020-06-20 12:31:38 +02:00
singleExportService.exportSingleNote(taskContext, branch, format, res);
2019-02-10 22:30:55 +01:00
}
else if (format === 'opml') {
2020-06-20 12:31:38 +02:00
opmlExportService.exportToOpml(taskContext, branch, version, res);
2019-02-10 22:30:55 +01:00
}
else {
throw new NotFoundError(`Unrecognized export format '${format}'`);
2019-02-10 22:30:55 +01:00
}
2018-09-02 23:39:10 +02:00
}
2019-02-10 22:30:55 +01:00
catch (e) {
const message = `Export failed with following error: '${e.message}'. More details might be in the logs.`;
2019-10-18 22:27:38 +02:00
taskContext.reportError(message);
2019-02-10 22:30:55 +01:00
log.error(message + e.stack);
res.setHeader("Content-Type", "text/plain")
.status(500)
.send(message);
}
}
2018-03-30 15:34:07 -04:00
module.exports = {
2018-11-24 20:58:38 +01:00
exportBranch
2020-05-17 21:07:54 +02:00
};