From 7325a1dd6d1e0b8d11c16709b6287040cab61e2a Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 29 Nov 2023 16:50:43 +0100 Subject: [PATCH] MOBILE-3947 native: Fix zip plugin --- package-lock.json | 13 ------- package.json | 1 - src/core/features/emulator/emulator.module.ts | 2 +- src/core/features/emulator/services/zip.ts | 8 ++--- src/core/features/native/native.module.ts | 3 +- src/core/features/native/plugins/index.ts | 2 ++ src/core/features/native/plugins/zip.ts | 35 +++++++++++++++++++ src/core/singletons/index.ts | 2 -- src/types/cordova-plugin-zip.d.ts | 27 ++++++++++++++ 9 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 src/core/features/native/plugins/zip.ts create mode 100644 src/types/cordova-plugin-zip.d.ts diff --git a/package-lock.json b/package-lock.json index 8cff3413b..71595c964 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,7 +39,6 @@ "@awesome-cordova-plugins/sqlite": "^6.3.0", "@awesome-cordova-plugins/status-bar": "^6.3.0", "@awesome-cordova-plugins/web-intent": "^6.3.0", - "@awesome-cordova-plugins/zip": "^6.3.0", "@ionic/angular": "^7.0.0", "@ionic/cordova-builders": "^10.0.0", "@moodlehq/cordova-plugin-advanced-http": "3.3.1-moodle.1", @@ -1034,18 +1033,6 @@ "rxjs": "^5.5.0 || ^6.5.0 || ^7.3.0" } }, - "node_modules/@awesome-cordova-plugins/zip": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@awesome-cordova-plugins/zip/-/zip-6.4.0.tgz", - "integrity": "sha512-s6Bg+sBepwhVvN+8fdns8QOOY5Mo5pg9Iy1aJiFDBUipuQOLaO++zw5u+no0igObnYcaQAdSO2XyEBX/h0T59g==", - "dependencies": { - "@types/cordova": "latest" - }, - "peerDependencies": { - "@awesome-cordova-plugins/core": "^6.0.1", - "rxjs": "^5.5.0 || ^6.5.0 || ^7.3.0" - } - }, "node_modules/@babel/code-frame": { "version": "7.23.4", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.23.4.tgz", diff --git a/package.json b/package.json index f385530cd..cdf914c8b 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,6 @@ "@awesome-cordova-plugins/sqlite": "^6.3.0", "@awesome-cordova-plugins/status-bar": "^6.3.0", "@awesome-cordova-plugins/web-intent": "^6.3.0", - "@awesome-cordova-plugins/zip": "^6.3.0", "@ionic/angular": "^7.0.0", "@ionic/cordova-builders": "^10.0.0", "@moodlehq/cordova-plugin-advanced-http": "3.3.1-moodle.1", diff --git a/src/core/features/emulator/emulator.module.ts b/src/core/features/emulator/emulator.module.ts index a5436fa31..9ebb477c8 100644 --- a/src/core/features/emulator/emulator.module.ts +++ b/src/core/features/emulator/emulator.module.ts @@ -27,7 +27,7 @@ import { Geolocation } from '@awesome-cordova-plugins/geolocation/ngx'; import { InAppBrowser } from '@awesome-cordova-plugins/in-app-browser/ngx'; import { LocalNotifications } from '@awesome-cordova-plugins/local-notifications/ngx'; import { MediaCapture } from '@awesome-cordova-plugins/media-capture/ngx'; -import { Zip } from '@awesome-cordova-plugins/zip/ngx'; +import { Zip } from '@features/native/plugins/zip'; // Mock services. import { CameraMock } from './services/camera'; diff --git a/src/core/features/emulator/services/zip.ts b/src/core/features/emulator/services/zip.ts index 3f4ea7e6e..56654bd99 100644 --- a/src/core/features/emulator/services/zip.ts +++ b/src/core/features/emulator/services/zip.ts @@ -13,7 +13,7 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { Zip } from '@awesome-cordova-plugins/zip/ngx'; +import { Zip } from '@features/native/plugins/zip'; import * as JSZip from 'jszip'; import { CorePath } from '@singletons/path'; import { File } from '@singletons'; @@ -50,12 +50,10 @@ export class ZipMock extends Zip { * * @param source Path to the source ZIP file. * @param destination Destination folder. - * @param onProgressFunction Optional callback to be called on progress update + * @param onProgress Optional callback to be called on progress update * @returns Promise that resolves with a number. 0 is success, -1 is error. */ - async unzip(source: string, destination: string, onProgressFunction?: Function): Promise { - const onProgress = onProgressFunction as (ev: {loaded: number; total: number}) => void; - + async unzip(source: string, destination: string, onProgress?: (ev: {loaded: number; total: number}) => void): Promise { // Replace all %20 with spaces. source = source.replace(/%20/g, ' '); destination = destination.replace(/%20/g, ' '); diff --git a/src/core/features/native/native.module.ts b/src/core/features/native/native.module.ts index b1bd71648..18449e534 100644 --- a/src/core/features/native/native.module.ts +++ b/src/core/features/native/native.module.ts @@ -36,7 +36,7 @@ import { SplashScreen } from '@awesome-cordova-plugins/splash-screen/ngx'; import { SQLite } from '@awesome-cordova-plugins/sqlite/ngx'; import { StatusBar } from '@awesome-cordova-plugins/status-bar/ngx'; import { WebIntent } from '@awesome-cordova-plugins/web-intent/ngx'; -import { Zip } from '@awesome-cordova-plugins/zip/ngx'; +import { Zip } from '@features/native/plugins/zip'; export const CORE_NATIVE_SERVICES = [ Badge, @@ -87,7 +87,6 @@ export const CORE_NATIVE_SERVICES = [ StatusBar, WebIntent, WebView, - Zip, ], }) export class CoreNativeModule {} diff --git a/src/core/features/native/plugins/index.ts b/src/core/features/native/plugins/index.ts index 8efe862c2..ee50e1680 100644 --- a/src/core/features/native/plugins/index.ts +++ b/src/core/features/native/plugins/index.ts @@ -14,5 +14,7 @@ import { makeSingleton } from '@singletons'; import { Chooser as ChooserService } from './chooser'; +import { Zip as ZipService } from './zip'; export const Chooser = makeSingleton(ChooserService); +export const Zip = makeSingleton(ZipService); diff --git a/src/core/features/native/plugins/zip.ts b/src/core/features/native/plugins/zip.ts new file mode 100644 index 000000000..aeda6ee60 --- /dev/null +++ b/src/core/features/native/plugins/zip.ts @@ -0,0 +1,35 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable } from '@angular/core'; + +/** + * Zip plugin wrapper. + */ +@Injectable({ providedIn: 'root' }) +export class Zip { + + /** + * Extracts files from a ZIP archive + * + * @param source Source ZIP file + * @param destination Destination folder + * @param onProgress Callback to be called on progress update + * @returns 0 is success, -1 is error + */ + unzip(source: string, destination: string, onProgress?: (ev: {loaded: number; total: number}) => void): Promise { + return new Promise(resolve => window.zip.unzip(source, destination, (result: number) => resolve(result), onProgress)); + } + +} diff --git a/src/core/singletons/index.ts b/src/core/singletons/index.ts index ece235b95..8059bd553 100644 --- a/src/core/singletons/index.ts +++ b/src/core/singletons/index.ts @@ -58,7 +58,6 @@ import { StatusBar as StatusBarService } from '@awesome-cordova-plugins/status-b import { SplashScreen as SplashScreenService } from '@awesome-cordova-plugins/splash-screen/ngx'; import { SQLite as SQLiteService } from '@awesome-cordova-plugins/sqlite/ngx'; import { WebIntent as WebIntentService } from '@awesome-cordova-plugins/web-intent/ngx'; -import { Zip as ZipService } from '@awesome-cordova-plugins/zip/ngx'; import { TranslateService } from '@ngx-translate/core'; @@ -192,7 +191,6 @@ export const SplashScreen = makeSingleton(SplashScreenService); export const SQLite = makeSingleton(SQLiteService); export const WebIntent = makeSingleton(WebIntentService); export const WebView = makeSingleton(WebViewService); -export const Zip = makeSingleton(ZipService); export const Camera = makeSingleton(CameraService); diff --git a/src/types/cordova-plugin-zip.d.ts b/src/types/cordova-plugin-zip.d.ts new file mode 100644 index 000000000..7d7bda00c --- /dev/null +++ b/src/types/cordova-plugin-zip.d.ts @@ -0,0 +1,27 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * Types for file cordova plugin. + * + * @see https://github.com/moodlemobile/cordova-plugin-zip + */ + +interface Window { + + zip: { + unzip(source: string, destination: string, onSuccess: Function, onProgress?: Function): void; + }; + +}