Storing the GPX export (#9) in Download directory and checking if the file allready exiss.
This commit is contained in:
parent
0b96918f3d
commit
269f35a0a5
|
@ -14,24 +14,54 @@ const GpxExport: React.FC<{ gpx: any }> = (props: { gpx: any }) => {
|
||||||
|
|
||||||
const hiddenLinkElement = useRef<HTMLAnchorElement>(null);
|
const hiddenLinkElement = useRef<HTMLAnchorElement>(null);
|
||||||
|
|
||||||
var downloadName: string;
|
var downloadBaseName: string;
|
||||||
|
|
||||||
if (props.gpx.metadata.name !== undefined) {
|
if (props.gpx.metadata.name !== undefined) {
|
||||||
downloadName = props.gpx.metadata.name;
|
downloadBaseName = props.gpx.metadata.name.substr(
|
||||||
|
0,
|
||||||
|
props.gpx.metadata.name.lastIndexOf('.')
|
||||||
|
);
|
||||||
} else if (props.gpx.gpx.metadata.name !== undefined) {
|
} else if (props.gpx.gpx.metadata.name !== undefined) {
|
||||||
downloadName = `${props.gpx.gpx.metadata.name}.gpx`;
|
downloadBaseName = props.gpx.gpx.metadata.name;
|
||||||
} else {
|
} else {
|
||||||
downloadName = `track-${props.gpx.metadata.lastModified}.gpx`;
|
downloadBaseName = `track-${props.gpx.metadata.lastModified}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const downloadExt = 'gpx';
|
||||||
|
|
||||||
|
const downloadName = `${downloadBaseName}.${downloadExt}`;
|
||||||
|
|
||||||
|
const getNewFilename: any = async (
|
||||||
|
filename: string,
|
||||||
|
extension: string,
|
||||||
|
i = 0
|
||||||
|
) => {
|
||||||
|
const filenameToTest =
|
||||||
|
i === 0 ? `${filename}.${extension}` : `${filename}(${i}).${extension}`;
|
||||||
|
try {
|
||||||
|
const statResult = await Filesystem.stat({
|
||||||
|
path: filenameToTest,
|
||||||
|
directory: Directory.Documents,
|
||||||
|
});
|
||||||
|
return getNewFilename(filename, extension, i + 1);
|
||||||
|
} catch {
|
||||||
|
return filenameToTest;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const download = async (event: any) => {
|
const download = async (event: any) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
console.log('download()');
|
console.log('download()');
|
||||||
const gpxAsXml = await getGpxAsXmlString(db, props.gpx._id);
|
const gpxAsXml = await getGpxAsXmlString(db, props.gpx._id);
|
||||||
console.log(`gpxAsXml: ${gpxAsXml}`);
|
console.log(`gpxAsXml: ${gpxAsXml}`);
|
||||||
if (isCapacitor) {
|
if (isCapacitor) {
|
||||||
|
const filename = await getNewFilename(
|
||||||
|
`../Download/${downloadBaseName}`,
|
||||||
|
downloadExt
|
||||||
|
);
|
||||||
|
console.log(`filename: ${filename}`);
|
||||||
const fileUrl = await Filesystem.writeFile({
|
const fileUrl = await Filesystem.writeFile({
|
||||||
path: `Download/${downloadName}`,
|
path: filename,
|
||||||
data: gpxAsXml,
|
data: gpxAsXml,
|
||||||
directory: Directory.Documents,
|
directory: Directory.Documents,
|
||||||
encoding: Encoding.UTF8,
|
encoding: Encoding.UTF8,
|
||||||
|
|
Loading…
Reference in New Issue