Skip to content

Commit

Permalink
add file size check to dropped files
Browse files Browse the repository at this point in the history
Signed-off-by: grnd-alt <[email protected]>
  • Loading branch information
grnd-alt committed Jan 8, 2025
1 parent 98e6d68 commit 0a57c45
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
5 changes: 5 additions & 0 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 ])
]);
Expand Down
4 changes: 4 additions & 0 deletions lib/Listener/LoadViewerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ public function handle(Event $event): void {
'collabBackendUrl',
$this->configService->getCollabBackendUrl()
);
$this->initialState->provideInitialState(
'maxFileSize',
$this->configService->getMaxFileSize()
);
}
}
8 changes: 8 additions & 0 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions src/files/SideBarDownload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export function ResetDownloadButton() {
panelColumn.style.display = ''
}
const downloadButton = document.getElementsByClassName('nc-download')[0]
if (downloadButton === undefined) {
return
}
sideBar.removeChild(downloadButton)
}

Expand Down
19 changes: 12 additions & 7 deletions src/files/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, () =>
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -193,7 +199,6 @@ export class FileHandle {
},
{
type: 'text',
customData: { meta },
isDeleted: false,
fillStyle: 'solid',
strokeWidth: 1,
Expand Down
6 changes: 6 additions & 0 deletions src/settings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<NcTextField :label="t('whiteboard', 'Shared secret')"
:value.sync="secret" />
</p>
<p>
<NcTextField :label="t('whiteboard', 'Max file size')"
:value.sync="maxFileSize" />
</p>
<p>
<NcButton type="submit"
:disabled="!serverUrl"
Expand Down Expand Up @@ -65,6 +69,7 @@ export default {
return {
serverUrl: loadState('whiteboard', 'url', ''),
secret: loadState('whiteboard', 'secret', ''),
maxFileSize: loadState('whiteboard', 'maxFileSize', 10),
validConnection: undefined,
connectionError: undefined,
}
Expand All @@ -77,6 +82,7 @@ export default {
const { data } = await axios.post(generateUrl('/apps/whiteboard/settings'), {
serverUrl: this.serverUrl,
secret: this.secret,
maxFileSize: this.maxFileSize,
})
await this.verifyConnection(data)
},
Expand Down

0 comments on commit 0a57c45

Please sign in to comment.