Storing the GPX export (#9) in Download directory and checking if the file allready exiss.

This commit is contained in:
Eric van der Vlist 2022-10-02 18:22:49 +02:00
parent 0b96918f3d
commit 269f35a0a5
1 changed files with 35 additions and 5 deletions

View File

@ -14,24 +14,54 @@ const GpxExport: React.FC<{ gpx: any }> = (props: { gpx: any }) => {
const hiddenLinkElement = useRef<HTMLAnchorElement>(null);
var downloadName: string;
var downloadBaseName: string;
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) {
downloadName = `${props.gpx.gpx.metadata.name}.gpx`;
downloadBaseName = props.gpx.gpx.metadata.name;
} 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) => {
event.preventDefault();
console.log('download()');
const gpxAsXml = await getGpxAsXmlString(db, props.gpx._id);
console.log(`gpxAsXml: ${gpxAsXml}`);
if (isCapacitor) {
const filename = await getNewFilename(
`../Download/${downloadBaseName}`,
downloadExt
);
console.log(`filename: ${filename}`);
const fileUrl = await Filesystem.writeFile({
path: `Download/${downloadName}`,
path: filename,
data: gpxAsXml,
directory: Directory.Documents,
encoding: Encoding.UTF8,