2025-01-09 18:36:24 +02:00
|
|
|
import toastService, { type ToastOptions } from "./toast.js";
|
2019-10-14 10:31:58 +02:00
|
|
|
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";
|
2024-11-28 23:31:06 +02:00
|
|
|
import { t } from "./i18n.js";
|
2019-10-14 10:31:58 +02:00
|
|
|
|
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)) {
|
2023-05-08 00:02:08 +02:00
|
|
|
throw new Error(`Unrecognized import entity type '${entityType}'.`);
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:31:58 +02:00
|
|
|
if (files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
const taskId = utils.randomString(10);
|
2019-10-14 10:31:58 +02:00
|
|
|
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");
|
2019-10-14 10:31:58 +02:00
|
|
|
|
|
|
|
for (const key in options) {
|
2025-01-28 15:44:15 +02:00
|
|
|
formData.append(key, (options as any)[key]);
|
2019-10-14 10:31:58 +02:00
|
|
|
}
|
|
|
|
|
2023-04-08 21:13:37 +08:00
|
|
|
await $.ajax({
|
2023-05-08 00:02:08 +02:00
|
|
|
url: `${window.glob.baseApiUrl}notes/${parentNoteId}/${entityType}-import`,
|
2020-12-03 21:50:41 +01:00
|
|
|
headers: await server.getHeaders(),
|
2019-10-14 10:31:58 +02:00
|
|
|
data: formData,
|
2025-01-09 18:07:02 +02:00
|
|
|
dataType: "json",
|
|
|
|
type: "POST",
|
2019-10-14 10:31:58 +02:00
|
|
|
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 }));
|
2022-09-03 15:11:03 +02:00
|
|
|
},
|
2019-10-14 10:31:58 +02:00
|
|
|
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
|
|
|
});
|
2019-10-14 10:31:58 +02: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,
|
2024-11-28 23:31:06 +02:00
|
|
|
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") {
|
2024-11-28 23:31:06 +02:00
|
|
|
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") {
|
2024-11-28 23:31:06 +02:00
|
|
|
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-14 10:31:58 +02:00
|
|
|
|
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);
|
2019-10-14 10:31:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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") {
|
2024-11-28 23:31:06 +02:00
|
|
|
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") {
|
2024-11-28 23:31:06 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-14 10:31:58 +02:00
|
|
|
export default {
|
|
|
|
uploadFiles
|
2023-04-08 21:13:37 +08:00
|
|
|
};
|