From d6cde2fc0f83d1e6dcb495fb8e2080be6afc6490 Mon Sep 17 00:00:00 2001 From: prusswan Date: Mon, 27 Jan 2025 21:33:42 +0800 Subject: [PATCH] limit accepted file types to .pmtiles only, improved error handling --- src/components/AppToolbar.tsx | 16 ++++++++++++++-- src/components/MapMaplibreGl.tsx | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/components/AppToolbar.tsx b/src/components/AppToolbar.tsx index fc690378..6ac98d21 100644 --- a/src/components/AppToolbar.tsx +++ b/src/components/AppToolbar.tsx @@ -17,7 +17,7 @@ import maputnikLogo from 'maputnik-design/logos/logo-color.svg?inline' import { withTranslation, WithTranslation } from 'react-i18next'; import { supportedLanguages } from '../i18n'; -import Dropzone from 'react-dropzone'; +import { default as Dropzone, FileRejection } from 'react-dropzone'; // This is required because of , there isn't another way to detect support that I'm aware of. const browser = detect(); @@ -142,6 +142,14 @@ class AppToolbarInternal extends React.Component { this.props.onLocalPMTilesSelected(file); } + onFileRejected = (r: FileRejection[]) => { + const errorMessageLine = r.map(e => { + return e.errors.map(f => f.message).join("\n") + }).join("\n"); + console.error("Dropzone file rejected:", errorMessageLine); + alert(errorMessageLine); + } + render() { const t = this.props.t; const views = [ @@ -182,6 +190,10 @@ class AppToolbarInternal extends React.Component { }, ]; + const acceptedFileTypes = { + 'application/octet-stream': [".pmtiles"] + } + const currentView = views.find((view) => { return view.id === this.props.mapState; }); @@ -298,7 +310,7 @@ class AppToolbarInternal extends React.Component { {t("Help")} - + {({getRootProps, getInputProps}) => (
diff --git a/src/components/MapMaplibreGl.tsx b/src/components/MapMaplibreGl.tsx index 50ede15f..f466cfc0 100644 --- a/src/components/MapMaplibreGl.tsx +++ b/src/components/MapMaplibreGl.tsx @@ -148,6 +148,8 @@ class MapMaplibreGlInternal extends React.Component { + console.error(`Error in reading local PMTiles file: ${e}`); }); } }