file upload WIP

This commit is contained in:
zadam 2023-06-06 12:31:38 +02:00
parent 0234ff5fca
commit 38839532d5
7 changed files with 24 additions and 29 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -15,7 +15,7 @@ function setupGlobs() {
window.glob.getHeaders = server.getHeaders;
// required for ESLint plugin and CKEditor
window.glob.getActiveTabNote = () => appContext.tabManager.getActiveContextNote();
window.glob.getActiveContextNote = () => appContext.tabManager.getActiveContextNote();
window.glob.requireLibrary = libraryLoader.requireLibrary;
window.glob.ESLINT = libraryLoader.ESLINT;
window.glob.appContext = appContext; // for debugging

View file

@ -98,31 +98,6 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
doRender() {
this.$widget = $(TPL);
this.contentSized();
this.$widget.on("dragover", e => e.preventDefault());
this.$widget.on("dragleave", e => e.preventDefault());
this.$widget.on("drop", async e => {
const activeNote = appContext.tabManager.getActiveContextNote();
if (!activeNote) {
return;
}
const files = [...e.originalEvent.dataTransfer.files]; // chrome has issue that dataTransfer.files empties after async operation
const importService = await import('../services/import.js');
importService.uploadFiles('notes', activeNote.noteId, files, {
safeImport: true,
shrinkImages: true,
textImportedAsText: true,
codeImportedAsCode: true,
explodeArchives: true,
replaceUnderscoresWithSpaces: true
});
});
}
async refresh() {

View file

@ -35,6 +35,24 @@ function saveAttachment(req) {
note.saveAttachment({attachmentId, role, mime, title, content});
}
function uploadAttachment(req) {
const {noteId} = req.params;
const {file} = req;
const note = becca.getNoteOrThrow(noteId);
const attachment = note.saveAttachment({
role: 'file',
mime: file.mimetype,
title: file.originalname,
content: file.buffer
});
return {
uploaded: true
};
}
function deleteAttachment(req) {
const {attachmentId} = req.params;
@ -58,6 +76,7 @@ module.exports = {
getAttachment,
getAllAttachments,
saveAttachment,
uploadAttachment,
deleteAttachment,
convertAttachmentToNote
};

View file

@ -63,7 +63,7 @@ function uploadImage(req) {
const {noteId} = req.query;
const {file} = req;
const note = becca.getNoteOrThrow(noteId);
becca.getNoteOrThrow(noteId);
if (!["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
throw new ValidationError(`Unknown image type '${file.mimetype}'`);

View file

@ -153,6 +153,7 @@ function register(app) {
apiRoute(GET, '/api/notes/:noteId/attachments', attachmentsApiRoute.getAttachments);
apiRoute(PST, '/api/notes/:noteId/attachments', attachmentsApiRoute.saveAttachment);
route(PST, '/api/notes/:noteId/attachments/upload', [auth.checkApiAuthOrElectron, uploadMiddlewareWithErrorHandling, csrfMiddleware], attachmentsApiRoute.uploadAttachment, apiResultHandler);
apiRoute(GET, '/api/attachments/:attachmentId', attachmentsApiRoute.getAttachment);
apiRoute(GET, '/api/attachments/:attachmentId/all', attachmentsApiRoute.getAllAttachments);
apiRoute(PST, '/api/attachments/:attachmentId/convert-to-note', attachmentsApiRoute.convertAttachmentToNote);