Skip to content

Commit

Permalink
Resolves #1946 - Replace pako and jszip with fflate
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Oct 9, 2023
1 parent fed62b0 commit 645b054
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
13 changes: 6 additions & 7 deletions IsraelHiking.Web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions IsraelHiking.Web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@
"d3-regression": "^1.3.10",
"deepmerge": "^4.3.1",
"dexie": "^3.2.4",
"fflate": "^0.8.1",
"file-saver-es": "^2.0.5",
"font-awesome": "^4.7.0",
"geojson-path-finder": "^2.0.2",
"immer": "^10.0.3",
"intl": "^1.2.5",
"invert-color": "^2.0.0",
"isomorphic-xml2js": "^0.1.3",
"jszip": "^3.10.1",
"linear-interpolator": "^1.0.2",
"lodash-es": "^4.17.21",
"lottie-web": "^5.12.2",
Expand All @@ -77,7 +77,6 @@
"ngx-lottie": "^10.0.0",
"ngx-progressbar": "^11.1.0",
"osmtogeojson": "^3.0.0-beta.5",
"pako": "^2.1.0",
"patch-package": "^8.0.0",
"pbf": "^3.2.1",
"photoswipe": "^5.4.2",
Expand Down
4 changes: 2 additions & 2 deletions IsraelHiking.Web/src/application/services/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Injectable } from "@angular/core";
import { Store } from "@ngxs/store";
import { debounceTime } from "rxjs/operators";
import { CapacitorSQLite, SQLiteDBConnection, SQLiteConnection} from "@capacitor-community/sqlite";
import { gunzipSync } from "fflate";
import Dexie from "dexie";
import deepmerge from "deepmerge";
import maplibregl from "maplibre-gl";
import * as pako from "pako";

import { LoggingService } from "./logging.service";
import { RunningContextService } from "./running-context.service";
Expand Down Expand Up @@ -191,7 +191,7 @@ export class DatabaseService {
let binData = new Uint8Array(hexData.match(/.{1,2}/g).map((byte: string) => parseInt(byte, 16)));
const isGzipped = binData[0] === 0x1f && binData[1] === 0x8b;
if (isGzipped) {
binData = pako.inflate(binData);
binData = gunzipSync(binData);
}
return binData.buffer;
}
Expand Down
21 changes: 12 additions & 9 deletions IsraelHiking.Web/src/application/services/file.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { File as FileSystemWrapper } 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 { StyleSpecification } from "maplibre-gl";
import JSZip from "jszip";
import { decode } from "base64-arraybuffer";
import { strToU8, zipSync, unzipSync, strFromU8 } from "fflate";

import { FileService, SaveAsFactory } from "./file.service";
import { ImageResizeService } from "./image-resize.service";
Expand Down Expand Up @@ -296,20 +297,22 @@ describe("FileService", () => {
const spy = jasmine.createSpy();
fileSystemWrapper.writeFile = spy;

const zip = new JSZip();
zip.folder("styles");
zip.file("styles/style.json", JSON.stringify({}));
const zipOutput = await zip.generateAsync({type: "blob"});

await service.writeStyles(zipOutput);
const result = zipSync({
"styles/style.json": strToU8(JSON.stringify({}))
});
await service.writeStyles(new Blob([result]));

expect(spy).toHaveBeenCalled();
}));

it("Should compress text to base 64 zip", inject([FileService],
async (service: FileService) => {

expect(async () => await service.compressTextToBase64Zip([{ name: "log.txt.", text: "some text" }])).not.toThrow();
const contents = [{ name: "log.txt", text: "some text" }];
let compressed = await service.compressTextToBase64Zip(contents);

let files = unzipSync(new Uint8Array(decode(compressed)));
expect(Object.keys(files)).toEqual([contents[0].name]);
expect(strFromU8(files[contents[0].name])).toEqual(contents[0].text);
}));

it("Should store file to cache", inject([FileService, FileSystemWrapper],
Expand Down
29 changes: 16 additions & 13 deletions IsraelHiking.Web/src/application/services/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ 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 { zipSync, strToU8, unzipSync, strFromU8, Zippable } from 'fflate';
import { encode } from "base64-arraybuffer";
import type { saveAs as saveAsForType } from "file-saver";
import JSZip from "jszip";

import { ImageResizeService } from "./image-resize.service";
import { RunningContextService } from "./running-context.service";
Expand Down Expand Up @@ -154,10 +155,9 @@ export class FileService {
}

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 });
const result = zipSync({ 'log.txt': strToU8(content) });
const resultBlob = new Blob([result]);
this.saveAs(resultBlob, fileName, { autoBom: false });
}

public async getFileFromUrl(url: string, type?: string): Promise<File> {
Expand Down Expand Up @@ -243,23 +243,26 @@ export class FileService {
}

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();
const zipData = new Uint8Array(await blob.arrayBuffer());
const files = unzipSync(zipData, {
filter: file => file.name.startsWith("styles/") && file.name.endsWith(".json")
});

for (const styleFileName in files) {
const styleText = strFromU8(files[styleFileName]);
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();
let zippable: Zippable = {};
for (const content of contents) {
zip.file(content.name, content.text);
zippable[content.name] = strToU8(content.text);
}
return zip.generateAsync({ type: "base64", compression: "DEFLATE", compressionOptions: { level: 6 } });
const result = zipSync(zippable);
return encode(await new Response(result).arrayBuffer());
}

private getFileContent(file: File): Promise<string> {
Expand Down

0 comments on commit 645b054

Please sign in to comment.