-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfile.service.ts
365 lines (332 loc) · 15.3 KB
/
file.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { Inject, Injectable, InjectionToken } from "@angular/core";
import { HttpClient, HttpEventType } from "@angular/common/http";
import { StyleSpecification } from "maplibre-gl";
import { File as FileSystemWrapper, FileEntry } from "@awesome-cordova-plugins/file/ngx";
import { FileTransfer } from "@awesome-cordova-plugins/file-transfer/ngx";
import { SocialSharing } from "@awesome-cordova-plugins/social-sharing/ngx";
import { last } from "lodash-es";
import { firstValueFrom } from "rxjs";
import type { saveAs as saveAsForType } from "file-saver";
import JSZip from "jszip";
import { ImageResizeService } from "./image-resize.service";
import { RunningContextService } from "./running-context.service";
import { SelectedRouteService } from "./selected-route.service";
import { FitBoundsService } from "./fit-bounds.service";
import { SpatialService } from "./spatial.service";
import { LoggingService } from "./logging.service";
import { GpxDataContainerConverterService } from "./gpx-data-container-converter.service";
import { Urls } from "../urls";
import type { DataContainer } from "../models/models";
export const SaveAsFactory = new InjectionToken<typeof saveAsForType>(null);
export type FormatViewModel = {
label: string;
outputFormat: string;
extension: string;
};
@Injectable()
export class FileService {
public formats: FormatViewModel[];
constructor(private readonly httpClient: HttpClient,
private readonly fileSystemWrapper: FileSystemWrapper,
// eslint-disable-next-line
private readonly fileTransfer: FileTransfer,
private readonly runningContextService: RunningContextService,
private readonly imageResizeService: ImageResizeService,
private readonly selectedRouteService: SelectedRouteService,
private readonly fitBoundsService: FitBoundsService,
private readonly gpxDataContainerConverterService: GpxDataContainerConverterService,
private readonly socialSharing: SocialSharing,
private readonly loggingService: LoggingService,
@Inject(SaveAsFactory) private readonly saveAs: typeof saveAsForType) {
this.formats = [
{
label: "GPX version 1.1 (.gpx)",
extension: "gpx",
outputFormat: "gpx"
},
{
label: "Single track GPX (.gpx)",
extension: "gpx",
outputFormat: "gpx_single_track"
},
{
label: "Single route GPX (.gpx)",
extension: "gpx",
outputFormat: "gpx_route"
},
{
label: "Keyhole markup language (.kml)",
extension: "kml",
outputFormat: "kml"
},
{
label: "Comma-separated values (.csv)",
extension: "csv",
outputFormat: "csv"
},
{
label: "Naviguide binary route file (.twl)",
extension: "twl",
outputFormat: "twl",
},
{
label: "All routes to a single Track GPX (.gpx)",
extension: "gpx",
outputFormat: "all_gpx_single_track"
}
];
}
public getFileFromEvent(e: any): File {
const file = e.dataTransfer ? e.dataTransfer.files[0] : e.target.files[0];
if (!file) {
return null;
}
const target = e.target || e.srcElement;
target.value = "";
return file;
}
public getFilesFromEvent(e: any): File[] {
const files: FileList = e.dataTransfer ? e.dataTransfer.files : e.target.files;
if (!files || files.length === 0) {
return [];
}
const filesToReturn = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < files.length; i++) {
filesToReturn.push(files[i]);
}
const target = e.target || e.srcElement;
target.value = ""; // this will reset files so we need to clone the array.
return filesToReturn;
}
public getFullUrl(relativePath: string): string {
return (window.origin || window.location.origin) + "/" + relativePath;
}
public async getStyleJsonContent(url: string, isOffline: boolean): Promise<StyleSpecification> {
try {
if (isOffline || (this.runningContextService.isCapacitor && url.startsWith("."))) {
const styleFileName = last(url.split("/"));
const styleText = await this.fileSystemWrapper.readAsText(this.fileSystemWrapper.dataDirectory, styleFileName);
return JSON.parse(styleText) as StyleSpecification;
}
return await firstValueFrom(this.httpClient.get(url)) as StyleSpecification;
} catch (ex) {
this.loggingService.error(`[Files] Unanle to get style file, isOffline: ${isOffline}, ${(ex as Error).message}`);
return {
version: 8.0,
layers: [],
sources: {}
};
}
}
private async base64StringToBlob(base64: string, type = "application/octet-stream"): Promise<Blob> {
const response = await fetch(`data:${type};base64,${base64}`);
return response.blob();
}
public async saveToFile(fileName: string, format: string, dataContainer: DataContainer) {
const responseData = format === "gpx"
? await this.gpxDataContainerConverterService.toGpx(dataContainer)
: await firstValueFrom(this.httpClient.post(Urls.files + "?format=" + format, dataContainer)) as string;
if (!this.runningContextService.isCapacitor) {
const blobToSave = await this.base64StringToBlob(responseData);
this.saveAs(blobToSave, fileName, { autoBom: false });
return;
}
fileName = fileName.replace(/[/\\?%*:|"<>]/g, "-");
const contentType = format === "gpx" ? "application/gpx+xml" : "application/octet-stream";
this.socialSharing.shareWithOptions({
files: [`df:${fileName};data:${contentType};base64,${responseData}`]
});
}
public async saveLogToZipFile(fileName: string, content: string) {
const zip = new JSZip();
zip.file("log.txt", content);
const blob = await zip.generateAsync({ type: "blob", compression: "DEFLATE", compressionOptions: { level: 6 } });
this.saveAs(blob, fileName, { autoBom: false });
}
public async getFileFromUrl(url: string, type?: string): Promise<File> {
const entry = await this.fileSystemWrapper.resolveLocalFilesystemUrl(url) as FileEntry;
const file = await new Promise((resolve, reject) => {
entry.file((fileContent) => {
const reader = new FileReader();
reader.onload = (event: any) => {
type = type || this.getTypeFromUrl(url);
const blob = new Blob([event.target.result], { type }) as any;
blob.name = entry.name;
if (blob.name.indexOf(".") === -1) {
blob.name += this.getExtensionFromType(type);
}
resolve(blob);
};
reader.readAsArrayBuffer(fileContent);
}, reject);
}) as File;
return file;
}
private getTypeFromUrl(url: string): string {
const fileExtension = url.split("/").pop().split(".").pop().toLocaleLowerCase();
if (fileExtension === "gpx") {
return "application/gpx+xml";
}
if (fileExtension === "kml") {
return "application/kml+xml";
}
if (fileExtension === "jpg" || fileExtension === "jpeg") {
return ImageResizeService.JPEG;
}
return "application/" + fileExtension;
}
private getExtensionFromType(type: string): string {
if (type.indexOf("gpx") !== -1) {
return ".gpx";
}
if (type.indexOf("kml") !== -1) {
return ".kml";
}
if (type.indexOf("jpg") !== -1 || type.indexOf("jpeg") !== -1) {
return ".jpg";
}
return "." + type.split("/").pop();
}
public async addRoutesFromFile(file: File): Promise<void> {
let dataContainer: DataContainer = null;
if (file.type === ImageResizeService.JPEG) {
dataContainer = await this.imageResizeService.resizeImageAndConvert(file);
} else {
const fileConent = await this.getFileContent(file);
if (this.gpxDataContainerConverterService.canConvert(fileConent)) {
dataContainer = await this.gpxDataContainerConverterService.toDataContainer(fileConent);
} else {
const formData = new FormData();
formData.append("file", file, file.name);
dataContainer = await firstValueFrom(this.httpClient.post(Urls.openFile, formData)) as DataContainer;
}
}
if (dataContainer.routes.length === 0 ||
(dataContainer.routes[0].markers.length === 0 && dataContainer.routes[0].segments.length === 0)) {
throw new Error("no geographic information found in file...");
}
this.addRoutesFromContainer(dataContainer);
}
public openFromUrl(url: string): Promise<DataContainer> {
return firstValueFrom(this.httpClient.get(Urls.files + "?url=" + url)) as Promise<DataContainer>;
}
public async addRoutesFromUrl(url: string) {
const container = await this.openFromUrl(url);
this.addRoutesFromContainer(container);
}
private addRoutesFromContainer(container: DataContainer) {
this.selectedRouteService.addRoutes(container.routes);
this.fitBoundsService.fitBounds(SpatialService.getBounds([container.southWest, container.northEast]));
}
public async writeStyles(blob: Blob) {
const zip = new JSZip();
await zip.loadAsync(blob);
const styles = Object.keys(zip.files).filter(name => name.startsWith("styles/") && name.endsWith(".json"));
for (const styleFileName of styles) {
const styleText = (await zip.file(styleFileName).async("text")).trim();
await this.fileSystemWrapper.writeFile(this.fileSystemWrapper.dataDirectory, styleFileName.replace("styles/", ""), styleText,
{ append: false, replace: true, truncate: 0 });
this.loggingService.info(`[Files] Write style finished succefully: ${styleFileName}`);
}
}
public async compressTextToBase64Zip(contents: {name: string; text: string}[]): Promise<string> {
const zip = new JSZip();
for (const content of contents) {
zip.file(content.name, content.text);
}
return zip.generateAsync({ type: "base64", compression: "DEFLATE", compressionOptions: { level: 6 } });
}
private getFileContent(file: File): Promise<string> {
return new Promise((resolve, _) => {
const reader = new FileReader();
reader.onload = (event: any) => {
resolve(event.target.result);
};
reader.readAsText(file);
});
}
public storeFileToCache(fileName: string, content: string | Blob) {
return this.fileSystemWrapper.writeFile(this.fileSystemWrapper.cacheDirectory, fileName, content,
{ replace: true, append: false, truncate: 0 });
}
/**
* Downloads a file while reporting progress
*
* @param url The url of the file
* @param progressCallback reports progress between 0 and 1
*/
public async getFileContentWithProgress(url: string, progressCallback: (value: number) => void): Promise<Blob> {
return new Promise((resolve, reject) => {
this.httpClient.get(url, {
observe: "events",
responseType: "blob",
reportProgress: true
}).subscribe({
next: (event) => {
if (event.type === HttpEventType.DownloadProgress) {
progressCallback(event.loaded / event.total);
}
if (event.type === HttpEventType.Response) {
if (event.ok) {
resolve(event.body);
} else {
reject(new Error(event.statusText));
}
}
}, error: (error) => reject(error)
});
});
}
public async getFileFromCache(url: string): Promise<Blob> {
try {
const fileBuffer = await this.fileSystemWrapper.readAsArrayBuffer(this.fileSystemWrapper.cacheDirectory, url.split("/").pop());
return new Blob([fileBuffer]);
} catch {
return null;
}
}
public async deleteFileFromCache(url: string): Promise<void> {
await this.fileSystemWrapper.removeFile(this.fileSystemWrapper.cacheDirectory, url.split("/").pop());
}
public downloadFileToCache(url: string, progressCallback: (value: number) => void) {
return this.downloadFileToCacheAuthenticated(url, url.split("/").pop(), null, progressCallback);
}
public async downloadFileToCacheAuthenticated(url: string, fileName: string, token: string, progressCallback: (value: number) => void) {
const fileTransferObject = this.fileTransfer.create();
fileTransferObject.onProgress((event) => {
progressCallback(event.loaded / event.total);
});
this.loggingService.info(`[Files] Starting downloading and writing file to cache, file name ${fileName}`);
const options = !token ? undefined : {
headers: {
Authorization: `Bearer ${token}`
}
};
await fileTransferObject.download(url, this.fileSystemWrapper.cacheDirectory + fileName, true, options);
this.loggingService.info(`[Files] Finished downloading and writing file to cache, file name ${fileName}`);
}
public async renameOldDatabases(): Promise<boolean> {
if (!this.runningContextService.isCapacitor) {
return false;
}
let filesExist = false;
const filePrefix = this.runningContextService.isIos ? "" : "databases/";
const originFolder = this.runningContextService.isIos
? this.fileSystemWrapper.documentsDirectory
: this.fileSystemWrapper.applicationStorageDirectory;
for (const fileName of ["Contour.mbtiles", "IHM.mbtiles", "TerrainRGB.mbtiles"]) {
const fullFileName = filePrefix + fileName;
this.loggingService.info(`[Files] Checking if database file exists: ${fullFileName}`);
try {
const fileExists = await this.fileSystemWrapper.checkFile(originFolder, fullFileName);
if (!fileExists) { continue; }
this.loggingService.info(`[Files] Statring renaming database: ${fullFileName}`);
await this.fileSystemWrapper.moveFile(originFolder, fullFileName, originFolder, fullFileName.replace(".mbtiles", ".db"));
this.loggingService.info(`[Files] Finished renaming database: ${fullFileName}`);
filesExist = true;
} catch { } // eslint-disable-line
}
return filesExist;
}
}