limit accepted file types to .pmtiles only, improved error handling

This commit is contained in:
prusswan
2025-01-27 21:33:42 +08:00
parent a2572c2f5b
commit d6cde2fc0f
2 changed files with 16 additions and 2 deletions
+14 -2
View File
@@ -17,7 +17,7 @@ import maputnikLogo from 'maputnik-design/logos/logo-color.svg?inline'
import { withTranslation, WithTranslation } from 'react-i18next'; import { withTranslation, WithTranslation } from 'react-i18next';
import { supportedLanguages } from '../i18n'; import { supportedLanguages } from '../i18n';
import Dropzone from 'react-dropzone'; import { default as Dropzone, FileRejection } from 'react-dropzone';
// This is required because of <https://stackoverflow.com/a/49846426>, there isn't another way to detect support that I'm aware of. // This is required because of <https://stackoverflow.com/a/49846426>, there isn't another way to detect support that I'm aware of.
const browser = detect(); const browser = detect();
@@ -142,6 +142,14 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
this.props.onLocalPMTilesSelected(file); 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() { render() {
const t = this.props.t; const t = this.props.t;
const views = [ const views = [
@@ -182,6 +190,10 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
}, },
]; ];
const acceptedFileTypes = {
'application/octet-stream': [".pmtiles"]
}
const currentView = views.find((view) => { const currentView = views.find((view) => {
return view.id === this.props.mapState; return view.id === this.props.mapState;
}); });
@@ -298,7 +310,7 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
<IconText>{t("Help")}</IconText> <IconText>{t("Help")}</IconText>
</ToolbarLink> </ToolbarLink>
<Dropzone onDrop={this.onFileSelected}> <Dropzone onDropAccepted={this.onFileSelected} onDropRejected={this.onFileRejected} accept={acceptedFileTypes}>
{({getRootProps, getInputProps}) => ( {({getRootProps, getInputProps}) => (
<div {...getRootProps({className: 'dropzone maputnik-toolbar-link'})}> <div {...getRootProps({className: 'dropzone maputnik-toolbar-link'})}>
<input {...getInputProps()} /> <input {...getInputProps()} />
+2
View File
@@ -148,6 +148,8 @@ class MapMaplibreGlInternal extends React.Component<MapMaplibreGlInternalProps,
// used by maplibre-gl-inspect to pick up inspectable layers // used by maplibre-gl-inspect to pick up inspectable layers
map.style.sourceCaches["source"]._source.vectorLayerIds = layerNames; map.style.sourceCaches["source"]._source.vectorLayerIds = layerNames;
}).catch( e => {
console.error(`Error in reading local PMTiles file: ${e}`);
}); });
} }
} }