From be8bf92975a48d51170c2f85ff2c7569ad589903 Mon Sep 17 00:00:00 2001 From: Tamara Steinwender <tamara.steinwender@tugraz.at> Date: Wed, 21 Jul 2021 10:21:42 +0200 Subject: [PATCH] Add a max-file-size to file-source --- packages/file-handling/README.md | 4 +++ packages/file-handling/src/file-source.js | 28 +++++++++++++++++-- .../src/i18n/de/translation.json | 4 ++- .../src/i18n/en/translation.json | 4 ++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/file-handling/README.md b/packages/file-handling/README.md index 6c6e3f4c..30e19ea6 100644 --- a/packages/file-handling/README.md +++ b/packages/file-handling/README.md @@ -65,6 +65,10 @@ files from a [Nextcloud](https://nextcloud.com/) instance or to a dbp-clipboard. - example `<dbp-file-source allowed-mime-types='image/*'></dbp-file-source>` ... images (of all sub types) only - example `<dbp-file-source allowed-mime-types='image/png,text/plain'></dbp-file-source>` ... PNGs or TXTs only - example `<dbp-file-source allowed-mime-types='*/*'></dbp-file-source>` ... all file types (default) +- `max-file-size"` (optional): if set accepts only files with maximum of this size (in KB) + - example `<dbp-file-source max-file-size="32000"></dbp-file-source>` ... only files with a max size of + 32 MB are allowed + - `enabled-targets` (optional, default: `local`): sets which sources are enabled - you can use `local`, `nextcloud` and `clipboard` - example `<dbp-file-source enabled-targets="local,nextcloud"></dbp-file-source>` diff --git a/packages/file-handling/src/file-source.js b/packages/file-handling/src/file-source.js index f9ee42a3..06389427 100644 --- a/packages/file-handling/src/file-source.js +++ b/packages/file-handling/src/file-source.js @@ -11,6 +11,7 @@ import MicroModal from './micromodal.es'; import * as fileHandlingStyles from './styles'; import {Clipboard} from "@dbp-toolkit/file-handling/src/clipboard"; import DbpFileHandlingLitElement from "./dbp-file-handling-lit-element"; +import {humanFileSize} from "@dbp-toolkit/common/i18next"; function mimeTypesToAccept(mimeTypes) { // Some operating systems can't handle mime types and @@ -55,6 +56,7 @@ export class FileSource extends ScopedElementsMixin(DbpFileHandlingLitElement) { this.isDialogOpen = false; this.firstOpen = true; this.nextcloudAuthInfo = ''; + this.maxFileSize = ''; this.initialFileHandlingState = {target: '', path: ''}; } @@ -88,6 +90,7 @@ export class FileSource extends ScopedElementsMixin(DbpFileHandlingLitElement) { decompressZip: { type: Boolean, attribute: 'decompress-zip' }, activeTarget: { type: String, attribute: 'active-target' }, isDialogOpen: { type: Boolean, attribute: 'dialog-open' }, + maxFileSize: { type: Number, attribute: 'max-file-size'}, initialFileHandlingState: {type: Object, attribute: 'initial-file-handling-state'}, }; @@ -214,6 +217,10 @@ export class FileSource extends ScopedElementsMixin(DbpFileHandlingLitElement) { console.log('file \'' + file.name + '\' has size=0 and is denied!'); return; } + + if(!this.checkSize(file)) { + return; + } // check if we want to decompress the zip and queue the contained files if (this.decompressZip && (file.type === "application/zip" || file.type === "application/x-zip-compressed")) { @@ -225,10 +232,10 @@ export class FileSource extends ScopedElementsMixin(DbpFileHandlingLitElement) { }); return; - } else if (this.allowedMimeTypes && !this.checkFileType(file)) { + } else if (this.allowedMimeTypes && !this.checkFileType(file) ) { return; } - +console.log("huiiii"); await this.sendFileEvent(file, fileCount); }); @@ -286,6 +293,23 @@ export class FileSource extends ScopedElementsMixin(DbpFileHandlingLitElement) { return true; } + checkSize(file) { + const i18n = this._i18n; + //TODO + console.log("file size", file.size); + if ( this.maxFileSize !== '' && (this.maxFileSize * 1000) <= file.size ) { + console.log(!"tooooo big file", humanFileSize(this.maxFileSize * 1000, true)); + send({ + "summary": i18n.t('file-source.too-big-file-title'), + "body": i18n.t('file-source.too-big-file-body', {size: humanFileSize(this.maxFileSize * 1000, true)}), + "type": "danger", + "timeout": 5, + }); + return false; + } + return true; + } + hasEnabledSource(source) { return this.enabledTargets.split(',').includes(source); } diff --git a/packages/file-handling/src/i18n/de/translation.json b/packages/file-handling/src/i18n/de/translation.json index 90c6a65b..b32f65e8 100644 --- a/packages/file-handling/src/i18n/de/translation.json +++ b/packages/file-handling/src/i18n/de/translation.json @@ -19,7 +19,9 @@ "no-usable-files-hint": "Laden Sie eine ZIP Datei mit Dateien von folgendem Typ hoch: ", "clipboard": "Zwischenablage", "mime-type-title": "Nicht unterstützes Dateiformat", - "mime-type-body": "Das Dateiformat wird von dieser Applikation nicht unterstützt." + "mime-type-body": "Das Dateiformat wird von dieser Applikation nicht unterstützt.", + "too-big-file-title": "Datei ist zu groß!", + "too-big-file-body": "Die Datei darf maximal {{size}} haben." }, "file-sink": { "local-intro": "{{count}} Datei herunterladen", diff --git a/packages/file-handling/src/i18n/en/translation.json b/packages/file-handling/src/i18n/en/translation.json index 129e6cf5..6d86a8ba 100644 --- a/packages/file-handling/src/i18n/en/translation.json +++ b/packages/file-handling/src/i18n/en/translation.json @@ -23,7 +23,9 @@ "clipboard-no-files": "There are currently no files in the clipboard.", "clipboard": "Clipboard", "mime-type-title": "Unsupported file format", - "mime-type-body": "The file format is not supported by this application." + "mime-type-body": "The file format is not supported by this application.", + "too-big-file-title": "The file is too big!", + "too-big-file-body": "The file can have a maximum of {{size}}." }, "file-sink": { "local-intro": "Download {{count}} file", -- GitLab