From 0a57c45d92d0821229dab061627cbff3dd58c22b Mon Sep 17 00:00:00 2001 From: grnd-alt Date: Tue, 7 Jan 2025 17:14:27 +0100 Subject: [PATCH] add file size check to dropped files Signed-off-by: grnd-alt --- lib/Controller/SettingsController.php | 5 +++++ lib/Listener/LoadViewerListener.php | 4 ++++ lib/Service/ConfigService.php | 8 ++++++++ lib/Settings/Admin.php | 1 + src/files/SideBarDownload.tsx | 3 +++ src/files/files.ts | 19 ++++++++++++------- src/settings/Settings.vue | 6 ++++++ 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index da3afcd..5fde044 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -35,6 +35,7 @@ public function update(): DataResponse { try { $serverUrl = $this->request->getParam('serverUrl'); $secret = $this->request->getParam('secret'); + $maxFileSize = $this->request->getParam('maxFileSize'); if ($serverUrl !== null) { $this->configService->setCollabBackendUrl($serverUrl); @@ -44,6 +45,10 @@ public function update(): DataResponse { $this->configService->setWhiteboardSharedSecret($secret); } + if ($maxFileSize !== null) { + $this->configService->setMaxFileSize(intval($maxFileSize)); + } + return new DataResponse([ 'jwt' => $this->jwtService->generateJWTFromPayload([ 'serverUrl' => $serverUrl ]) ]); diff --git a/lib/Listener/LoadViewerListener.php b/lib/Listener/LoadViewerListener.php index 888f892..9934ec4 100644 --- a/lib/Listener/LoadViewerListener.php +++ b/lib/Listener/LoadViewerListener.php @@ -37,5 +37,9 @@ public function handle(Event $event): void { 'collabBackendUrl', $this->configService->getCollabBackendUrl() ); + $this->initialState->provideInitialState( + 'maxFileSize', + $this->configService->getMaxFileSize() + ); } } diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 1e46d0c..41e7f7d 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -25,6 +25,14 @@ public function getJwtSecretKey(): string { return $this->appConfig->getAppValueString('jwt_secret_key'); } + public function getMaxFileSize(): int { + return $this->appConfig->getAppValueInt('max_file_size'); + } + + public function setMaxFileSize(int $maxFileSize): void { + $this->appConfig->setAppValueInt('max_file_size', $maxFileSize); + } + public function getCollabBackendUrl(): string { if (!method_exists($this->appConfig, 'getAppValueString')) { return $this->appConfig->getAppValue('collabBackendUrl'); diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 0029054..c947fad 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -25,6 +25,7 @@ public function getForm(): TemplateResponse { $this->initialState->provideInitialState('url', $this->configService->getCollabBackendUrl()); $this->initialState->provideInitialState('secret', $this->configService->getWhiteboardSharedSecret()); $this->initialState->provideInitialState('jwt', $this->jwtService->generateJWTFromPayload([])); + $this->initialState->provideInitialState('maxFileSize', $this->configService->getMaxFileSize()); $response = new TemplateResponse( 'whiteboard', 'admin', diff --git a/src/files/SideBarDownload.tsx b/src/files/SideBarDownload.tsx index 2a015a7..769013b 100644 --- a/src/files/SideBarDownload.tsx +++ b/src/files/SideBarDownload.tsx @@ -69,6 +69,9 @@ export function ResetDownloadButton() { panelColumn.style.display = '' } const downloadButton = document.getElementsByClassName('nc-download')[0] + if (downloadButton === undefined) { + return + } sideBar.removeChild(downloadButton) } diff --git a/src/files/files.ts b/src/files/files.ts index 6cd37ab..3be5071 100644 --- a/src/files/files.ts +++ b/src/files/files.ts @@ -15,6 +15,7 @@ import { Collab } from '../collaboration/collab' import type { FileId } from '@excalidraw/excalidraw/types/element/types' import axios from '@nextcloud/axios' import { InsertDownloadButton, ResetDownloadButton } from './SideBarDownload' +import { loadState } from '@nextcloud/initial-state' export type Meta = { name: string @@ -50,8 +51,8 @@ export class FileHandle { // eslint-disable-next-line @typescript-eslint/no-unused-vars this.excalidrawApi.onPointerDown(async (activeTool, state, event) => { const clickedElement = state.hit.element + ResetDownloadButton() if (!clickedElement || !clickedElement.customData) { - ResetDownloadButton() return } InsertDownloadButton(clickedElement.customData.meta, () => @@ -77,6 +78,13 @@ export class FileHandle { } private handleFileInsert(file: File, ev: Event) { + const maxFileSize = loadState('whiteboard', 'maxFileSize', 10) + if (file.size > maxFileSize * 1024 * 1024) { + ev.stopImmediatePropagation() + this.excalidrawApi.setToast({ message: `Max file size is: ${maxFileSize} MB`, closable: true, duration: 5000 }) + return + } + // if excalidraw can handle it, do nothing if (this.types.includes(file.type)) { return @@ -123,8 +131,9 @@ export class FileHandle { id: `filetype-icon-${mimeType}` as FileId, dataURL: reader.result as DataURL, } - this.collab.portal.sendImageFiles({ [file.id]: file }) - resolve(file.id) + this.collab.portal.sendImageFiles({ [file.id]: file }).then(() => { + resolve(file.id) + }) } } reader.readAsDataURL(blob) @@ -144,9 +153,6 @@ export class FileHandle { this.excalidrawApi.getAppState(), ) const iconId = await this.getMimeIcon(meta.type) - this.collab.portal.sendImageFiles({ - [constructedFile.id]: constructedFile, - }) const elements = this.excalidrawApi .getSceneElementsIncludingDeleted() .slice() @@ -193,7 +199,6 @@ export class FileHandle { }, { type: 'text', - customData: { meta }, isDeleted: false, fillStyle: 'solid', strokeWidth: 1, diff --git a/src/settings/Settings.vue b/src/settings/Settings.vue index d3497ac..0058368 100644 --- a/src/settings/Settings.vue +++ b/src/settings/Settings.vue @@ -33,6 +33,10 @@

+

+ +