117 lines
3.6 KiB
TypeScript
Raw Normal View History

import toastService, { type ToastOptions } from "./toast.js";
import server from "./server.js";
import ws from "./ws.js";
import utils from "./utils.js";
2022-12-01 13:07:23 +01:00
import appContext from "../components/app_context.js";
import { t } from "./i18n.js";
2025-01-28 15:44:15 +02:00
interface UploadFilesOptions {
safeImport: boolean;
shrinkImages: boolean;
textImportedAsText: boolean;
codeImportedAsCode: boolean;
explodeArchives: boolean;
replaceUnderscoresWithSpaces: boolean;
}
export async function uploadFiles(entityType: string, parentNoteId: string, files: string[], options: UploadFilesOptions) {
2025-01-09 18:07:02 +02:00
if (!["notes", "attachments"].includes(entityType)) {
throw new Error(`Unrecognized import entity type '${entityType}'.`);
}
if (files.length === 0) {
return;
}
const taskId = utils.randomString(10);
let counter = 0;
for (const file of files) {
counter++;
const formData = new FormData();
2025-01-09 18:07:02 +02:00
formData.append("upload", file);
formData.append("taskId", taskId);
formData.append("last", counter === files.length ? "true" : "false");
for (const key in options) {
2025-01-28 15:44:15 +02:00
formData.append(key, (options as any)[key]);
}
2023-04-08 21:13:37 +08:00
await $.ajax({
url: `${window.glob.baseApiUrl}notes/${parentNoteId}/${entityType}-import`,
2020-12-03 21:50:41 +01:00
headers: await server.getHeaders(),
data: formData,
2025-01-09 18:07:02 +02:00
dataType: "json",
type: "POST",
timeout: 60 * 60 * 1000,
2023-04-08 21:13:37 +08:00
error: function (xhr) {
2024-10-20 02:19:47 +03:00
toastService.showError(t("import.failed", { message: xhr.responseText }));
},
contentType: false, // NEEDED, DON'T REMOVE THIS
2025-01-09 18:07:02 +02:00
processData: false // NEEDED, DON'T REMOVE THIS
2023-04-08 21:13:37 +08:00
});
}
}
2024-12-21 17:50:18 +02:00
function makeToast(id: string, message: string): ToastOptions {
2019-10-17 21:15:27 +02:00
return {
id: id,
title: t("import.import-status"),
2019-10-17 21:15:27 +02:00
message: message,
2019-10-17 20:44:51 +02:00
icon: "plus"
};
2019-10-17 21:15:27 +02:00
}
2019-10-17 20:44:51 +02:00
2025-01-09 18:07:02 +02:00
ws.subscribeToMessages(async (message) => {
if (message.taskType !== "importNotes") {
2019-10-18 23:19:16 +02:00
return;
}
2025-01-09 18:07:02 +02:00
if (message.type === "taskError") {
2019-10-20 10:00:18 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2025-01-09 18:07:02 +02:00
} else if (message.type === "taskProgressCount") {
toastService.showPersistent(makeToast(message.taskId, t("import.in-progress", { progress: message.progressCount })));
2025-01-09 18:07:02 +02:00
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("import.successful"));
2019-10-17 20:44:51 +02:00
toast.closeAfter = 5000;
2019-10-20 10:00:18 +02:00
toastService.showPersistent(toast);
2019-10-19 00:11:07 +02:00
if (message.result.importedNoteId) {
2021-05-22 12:35:41 +02:00
await appContext.tabManager.getActiveContext().setNote(message.result.importedNoteId);
}
}
});
2025-01-09 18:07:02 +02:00
ws.subscribeToMessages(async (message) => {
if (message.taskType !== "importAttachments") {
2023-05-09 00:05:27 +02:00
return;
}
2025-01-09 18:07:02 +02:00
if (message.type === "taskError") {
2023-05-09 00:05:27 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2025-01-09 18:07:02 +02:00
} else if (message.type === "taskProgressCount") {
toastService.showPersistent(makeToast(message.taskId, t("import.in-progress", { progress: message.progressCount })));
2025-01-09 18:07:02 +02:00
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("import.successful"));
2023-05-09 00:05:27 +02:00
toast.closeAfter = 5000;
toastService.showPersistent(toast);
if (message.result.parentNoteId) {
await appContext.tabManager.getActiveContext().setNote(message.result.importedNoteId, {
viewScope: {
2025-01-09 18:07:02 +02:00
viewMode: "attachments"
2023-05-09 00:05:27 +02:00
}
});
}
}
});
export default {
uploadFiles
2023-04-08 21:13:37 +08:00
};