2017-12-02 21:48:22 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import zipExportService from "../../services/export/zip.js";
|
|
|
|
|
import singleExportService from "../../services/export/single.js";
|
|
|
|
|
import opmlExportService from "../../services/export/opml.js";
|
|
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
|
import TaskContext from "../../services/task_context.js";
|
|
|
|
|
import log from "../../services/log.js";
|
|
|
|
|
import NotFoundError from "../../errors/not_found_error.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { Request, Response } from "express";
|
2024-07-18 21:35:17 +03:00
|
|
|
import ValidationError from "../../errors/validation_error.js";
|
2025-03-08 17:07:25 +01:00
|
|
|
import { safeExtractMessageAndStackFromError } from "../../services/utils.js";
|
2024-04-05 20:58:31 +03:00
|
|
|
|
|
|
|
|
function exportBranch(req: Request, res: Response) {
|
2025-01-09 18:07:02 +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);
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
res.setHeader("Content-Type", "text/plain").status(500).send(message);
|
2020-05-17 21:07:54 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const taskContext = new TaskContext(taskId, "export");
|
2019-02-10 22:30:55 +01:00
|
|
|
|
|
|
|
|
try {
|
2025-06-23 18:13:47 +03:00
|
|
|
if (type === "subtree" && (format === "html" || format === "markdown" || format === "share")) {
|
2020-06-20 12:31:38 +02:00
|
|
|
zipExportService.exportToZip(taskContext, branch, format, res);
|
2025-01-09 18:07:02 +02: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);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (format === "opml") {
|
2020-06-20 12:31:38 +02:00
|
|
|
opmlExportService.exportToOpml(taskContext, branch, version, res);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2022-12-09 16:04:13 +01:00
|
|
|
throw new NotFoundError(`Unrecognized export format '${format}'`);
|
2019-02-10 22:30:55 +01:00
|
|
|
}
|
2025-03-06 23:32:05 +01:00
|
|
|
} catch (e: unknown) {
|
2025-03-08 17:07:25 +01:00
|
|
|
const [errMessage, errStack] = safeExtractMessageAndStackFromError(e);
|
|
|
|
|
const message = `Export failed with following error: '${errMessage}'. 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
|
|
|
|
2025-03-08 17:07:25 +01:00
|
|
|
log.error(errMessage + errStack);
|
2019-02-10 22:30:55 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
res.setHeader("Content-Type", "text/plain").status(500).send(message);
|
2018-05-27 12:26:34 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2018-11-24 20:58:38 +01:00
|
|
|
exportBranch
|
2020-05-17 21:07:54 +02:00
|
|
|
};
|