From 4a9aad8e103cb2206a94a697dc319d8f931cc4c0 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 28 May 2024 01:25:43 +0200 Subject: [PATCH] Fix #1617: give correct error message if an image is too large --- .../ImageProviders/ImageUploadManager.ts | 31 ++++++++++++------- src/UI/Image/UploadImage.svelte | 13 ++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/Logic/ImageProviders/ImageUploadManager.ts b/src/Logic/ImageProviders/ImageUploadManager.ts index 7864eaffe..f34eb29bf 100644 --- a/src/Logic/ImageProviders/ImageUploadManager.ts +++ b/src/Logic/ImageProviders/ImageUploadManager.ts @@ -8,6 +8,7 @@ import { OsmConnection } from "../Osm/OsmConnection" import { Changes } from "../Osm/Changes" import Translations from "../../UI/i18n/Translations" import NoteCommentElement from "../../UI/Popup/Notes/NoteCommentElement" +import { Translation } from "../../UI/i18n/Translation" /** * The ImageUploadManager has a @@ -72,6 +73,19 @@ export class ImageUploadManager { } } + public canBeUploaded(file: File): true | {error: Translation} { + const sizeInBytes = file.size + const self = this + if (sizeInBytes > this._uploader.maxFileSizeInMegabytes * 1000000) { + const error = Translations.t.image.toBig.Subs({ + actual_size: Math.floor(sizeInBytes / 1000000) + "MB", + max_size: self._uploader.maxFileSizeInMegabytes + "MB", + }) + return {error} + } + return true + } + /** * Uploads the given image, applies the correct title and license for the known user. * Will then add this image to the OSM-feature or the OSM-note @@ -84,19 +98,14 @@ export class ImageUploadManager { tagsStore: UIEventSource, targetKey?: string ): Promise { - const sizeInBytes = file.size - const tags = tagsStore.data - const featureId = tags.id - const self = this - if (sizeInBytes > this._uploader.maxFileSizeInMegabytes * 1000000) { - this.increaseCountFor(this._uploadStarted, featureId) - this.increaseCountFor(this._uploadFailed, featureId) - throw Translations.t.image.toBig.Subs({ - actual_size: Math.floor(sizeInBytes / 1000000) + "MB", - max_size: self._uploader.maxFileSizeInMegabytes + "MB", - }).txt + + const canBeUploaded = this.canBeUploaded(file) + if(canBeUploaded !== true){ + throw canBeUploaded.error } + const tags = tagsStore.data + const featureId = tags.id const licenseStore = this._osmConnection?.GetPreference("pictures-license", "CC0") const license = licenseStore?.data ?? "CC0" diff --git a/src/UI/Image/UploadImage.svelte b/src/UI/Image/UploadImage.svelte index 8e47f02a2..d77508b9e 100644 --- a/src/UI/Image/UploadImage.svelte +++ b/src/UI/Image/UploadImage.svelte @@ -13,6 +13,7 @@ import FileSelector from "../Base/FileSelector.svelte" import Camera_plus from "../../assets/svg/Camera_plus.svelte" import LoginButton from "../Base/LoginButton.svelte" + import { Translation } from "../i18n/Translation" export let state: SpecialVisualizationState @@ -31,16 +32,25 @@ let licenseStore = state?.userRelatedState?.imageLicense ?? new ImmutableStore("CC0") + let errors = new UIEventSource([]) + function handleFiles(files: FileList) { + const errs = [] for (let i = 0; i < files.length; i++) { const file = files.item(i) console.log("Got file", file.name) try { + const canBeUploaded = state?.imageUploadManager?.canBeUploaded(file) + if (canBeUploaded !== true) { + errs.push(canBeUploaded.error) + continue + } state?.imageUploadManager.uploadImageAndApply(file, tags, targetKey) } catch (e) { alert(e) } } + errors.setData(errs) } @@ -50,6 +60,9 @@
+ {#each $errors as error} + + {/each}