Notes/src/routes/api/export.ts

61 lines
2.0 KiB
TypeScript
Raw Normal View History

2017-12-02 21:48:22 -05:00
"use strict";
2024-04-05 20:58:31 +03:00
import zipExportService = require('../../services/export/zip');
import singleExportService = require('../../services/export/single');
import opmlExportService = require('../../services/export/opml');
import becca = require('../../becca/becca');
import TaskContext = require('../../services/task_context');
import log = require('../../services/log');
import NotFoundError = require('../../errors/not_found_error');
import { Request, Response } from 'express';
import ValidationError = require('../../errors/validation_error');
function exportBranch(req: Request, res: Response) {
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') {
2024-04-05 20:58:31 +03:00
if (format !== "html" && format !== "markdown") {
throw new ValidationError("Invalid export type.");
}
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
}
2024-04-05 20:58:31 +03:00
catch (e: any) {
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
};