2019-10-14 16:31:58 +08:00
|
|
|
import infoService from "./info.js";
|
|
|
|
import treeService from "./tree.js";
|
|
|
|
import server from "./server.js";
|
|
|
|
import ws from "./ws.js";
|
|
|
|
import utils from "./utils.js";
|
|
|
|
|
|
|
|
export async function uploadFiles(parentNoteId, files, options) {
|
|
|
|
if (files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-18 03:11:35 +08:00
|
|
|
const taskId = utils.randomString(10);
|
2019-10-14 16:31:58 +08:00
|
|
|
let noteId;
|
|
|
|
let counter = 0;
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
counter++;
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('upload', file);
|
2019-10-18 03:11:35 +08:00
|
|
|
formData.append('taskId', taskId);
|
2019-10-14 16:31:58 +08:00
|
|
|
formData.append('last', counter === files.length ? "true" : "false");
|
|
|
|
|
|
|
|
for (const key in options) {
|
|
|
|
formData.append(key, options[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
({noteId} = await $.ajax({
|
|
|
|
url: baseApiUrl + 'notes/' + parentNoteId + '/import',
|
|
|
|
headers: server.getHeaders(),
|
|
|
|
data: formData,
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
timeout: 60 * 60 * 1000,
|
|
|
|
contentType: false, // NEEDED, DON'T REMOVE THIS
|
|
|
|
processData: false, // NEEDED, DON'T REMOVE THIS
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 03:15:27 +08:00
|
|
|
function makeToast(id, message) {
|
|
|
|
return {
|
|
|
|
id: id,
|
2019-10-18 03:11:35 +08:00
|
|
|
title: "Import status",
|
2019-10-18 03:15:27 +08:00
|
|
|
message: message,
|
2019-10-18 02:44:51 +08:00
|
|
|
icon: "plus"
|
|
|
|
};
|
2019-10-18 03:15:27 +08:00
|
|
|
}
|
2019-10-18 02:44:51 +08:00
|
|
|
|
2019-10-18 03:15:27 +08:00
|
|
|
ws.subscribeToMessages(async message => {
|
2019-10-18 03:11:35 +08:00
|
|
|
if (message.type === 'task-error' && message.taskType === 'import') {
|
2019-10-18 03:15:27 +08:00
|
|
|
infoService.closePersistent(message.taskId);
|
2019-10-14 16:31:58 +08:00
|
|
|
infoService.showError(message.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-18 03:11:35 +08:00
|
|
|
if (message.type === 'task-progress-count' && message.taskType === 'import') {
|
2019-10-18 03:15:27 +08:00
|
|
|
infoService.showPersistent(makeToast(message.taskId, "Import in progress: " + message.progressCount));
|
2019-10-14 16:31:58 +08:00
|
|
|
}
|
|
|
|
|
2019-10-18 03:11:35 +08:00
|
|
|
if (message.type === 'task-succeeded' && message.taskType === 'import') {
|
2019-10-18 03:15:27 +08:00
|
|
|
const toast = makeToast(message.taskId, "Import finished successfully.");
|
2019-10-18 02:44:51 +08:00
|
|
|
toast.closeAfter = 5000;
|
|
|
|
|
|
|
|
infoService.showPersistent(toast);
|
2019-10-14 16:31:58 +08:00
|
|
|
|
|
|
|
await treeService.reloadNote(message.parentNoteId);
|
|
|
|
|
|
|
|
if (message.importedNoteId) {
|
|
|
|
const node = await treeService.activateNote(message.importedNoteId);
|
|
|
|
|
|
|
|
node.setExpanded(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default {
|
|
|
|
uploadFiles
|
|
|
|
}
|