Skip to content

Commit d6cde2f

Browse files
committed
limit accepted file types to .pmtiles only, improved error handling
1 parent a2572c2 commit d6cde2f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/components/AppToolbar.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import maputnikLogo from 'maputnik-design/logos/logo-color.svg?inline'
1717
import { withTranslation, WithTranslation } from 'react-i18next';
1818
import { supportedLanguages } from '../i18n';
1919

20-
import Dropzone from 'react-dropzone';
20+
import { default as Dropzone, FileRejection } from 'react-dropzone';
2121

2222
// This is required because of <https://stackoverflow.com/a/49846426>, there isn't another way to detect support that I'm aware of.
2323
const browser = detect();
@@ -142,6 +142,14 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
142142
this.props.onLocalPMTilesSelected(file);
143143
}
144144

145+
onFileRejected = (r: FileRejection[]) => {
146+
const errorMessageLine = r.map(e => {
147+
return e.errors.map(f => f.message).join("\n")
148+
}).join("\n");
149+
console.error("Dropzone file rejected:", errorMessageLine);
150+
alert(errorMessageLine);
151+
}
152+
145153
render() {
146154
const t = this.props.t;
147155
const views = [
@@ -182,6 +190,10 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
182190
},
183191
];
184192

193+
const acceptedFileTypes = {
194+
'application/octet-stream': [".pmtiles"]
195+
}
196+
185197
const currentView = views.find((view) => {
186198
return view.id === this.props.mapState;
187199
});
@@ -298,7 +310,7 @@ class AppToolbarInternal extends React.Component<AppToolbarInternalProps> {
298310
<IconText>{t("Help")}</IconText>
299311
</ToolbarLink>
300312

301-
<Dropzone onDrop={this.onFileSelected}>
313+
<Dropzone onDropAccepted={this.onFileSelected} onDropRejected={this.onFileRejected} accept={acceptedFileTypes}>
302314
{({getRootProps, getInputProps}) => (
303315
<div {...getRootProps({className: 'dropzone maputnik-toolbar-link'})}>
304316
<input {...getInputProps()} />

src/components/MapMaplibreGl.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ class MapMaplibreGlInternal extends React.Component<MapMaplibreGlInternalProps,
148148

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

0 commit comments

Comments
 (0)