Skip to content

Commit

Permalink
limit accepted file types to .pmtiles only, improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
prusswan committed Jan 27, 2025
1 parent a2572c2 commit d6cde2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/components/AppToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://stackoverflow.com/a/49846426>, there isn't another way to detect support that I'm aware of.
const browser = detect();
Expand Down Expand Up @@ -142,6 +142,14 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
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 = [
Expand Down Expand Up @@ -182,6 +190,10 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
},
];

const acceptedFileTypes = {
'application/octet-stream': [".pmtiles"]
}

const currentView = views.find((view) => {
return view.id === this.props.mapState;
});
Expand Down Expand Up @@ -298,7 +310,7 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
<IconText>{t("Help")}</IconText>
</ToolbarLink>

<Dropzone onDrop={this.onFileSelected}>
<Dropzone onDropAccepted={this.onFileSelected} onDropRejected={this.onFileRejected} accept={acceptedFileTypes}>
{({getRootProps, getInputProps}) => (
<div {...getRootProps({className: 'dropzone maputnik-toolbar-link'})}>
<input {...getInputProps()} />
Expand Down
2 changes: 2 additions & 0 deletions src/components/MapMaplibreGl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class MapMaplibreGlInternal extends React.Component<MapMaplibreGlInternalProps,

// used by maplibre-gl-inspect to pick up inspectable layers
map.style.sourceCaches["source"]._source.vectorLayerIds = layerNames;
}).catch( e => {
console.error(`Error in reading local PMTiles file: ${e}`);
});
}
}
Expand Down

0 comments on commit d6cde2f

Please sign in to comment.