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');
|
2018-11-16 12:12:04 +01:00
|
|
|
const opmlExportService = require('../../services/export/opml');
|
2021-06-26 23:10:03 +02:00
|
|
|
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");
|
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) {
|
|
|
|
const message = `Cannot export branch ${branchId} since it does not exist.`;
|
|
|
|
log.error(message);
|
|
|
|
|
2022-07-01 00:01:29 +02:00
|
|
|
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 {
|
|
|
|
return [404, "Unrecognized export format " + format];
|
|
|
|
}
|
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);
|
|
|
|
|
2022-07-01 00:01:29 +02:00
|
|
|
res.setHeader("Content-Type", "text/plain")
|
|
|
|
.status(500)
|
|
|
|
.send(message);
|
2018-05-27 12:26:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|