MOBILE-2350 scorm: Fix unzip in browser and sync WS calls

main
Dani Palou 2018-04-26 09:59:00 +02:00
parent dffc8c3c6c
commit 5793af108d
5 changed files with 86 additions and 24 deletions

View File

@ -53,7 +53,6 @@ import { CoreEmulatorCaptureHelperProvider } from './providers/capture-helper';
import { CoreAppProvider } from '@providers/app'; import { CoreAppProvider } from '@providers/app';
import { CoreFileProvider } from '@providers/file'; import { CoreFileProvider } from '@providers/file';
import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTextUtilsProvider } from '@providers/utils/text';
import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype';
import { CoreUrlUtilsProvider } from '@providers/utils/url'; import { CoreUrlUtilsProvider } from '@providers/utils/url';
import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreInitDelegate } from '@providers/init'; import { CoreInitDelegate } from '@providers/init';
@ -184,10 +183,10 @@ export const IONIC_NATIVE_PROVIDERS = [
SQLite, SQLite,
{ {
provide: Zip, provide: Zip,
deps: [CoreAppProvider, File, CoreMimetypeUtilsProvider, CoreTextUtilsProvider], deps: [CoreAppProvider, File, CoreTextUtilsProvider],
useFactory: (appProvider: CoreAppProvider, file: File, mimeUtils: CoreMimetypeUtilsProvider): Zip => { useFactory: (appProvider: CoreAppProvider, file: File, textUtils: CoreTextUtilsProvider): Zip => {
// Use platform instead of CoreAppProvider to prevent circular dependencies. // Use platform instead of CoreAppProvider to prevent circular dependencies.
return appProvider.isMobile() ? new Zip() : new ZipMock(file, mimeUtils); return appProvider.isMobile() ? new Zip() : new ZipMock(file, textUtils);
} }
}, },
] ]

View File

@ -14,9 +14,9 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Zip } from '@ionic-native/zip'; import { Zip } from '@ionic-native/zip';
import { JSZip } from 'jszip'; import * as JSZip from 'jszip';
import { File } from '@ionic-native/file'; import { File } from '@ionic-native/file';
import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype'; import { CoreTextUtilsProvider } from '@providers/utils/text';
/** /**
* Emulates the Cordova Zip plugin in desktop apps and in browser. * Emulates the Cordova Zip plugin in desktop apps and in browser.
@ -24,10 +24,36 @@ import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype';
@Injectable() @Injectable()
export class ZipMock extends Zip { export class ZipMock extends Zip {
constructor(private file: File, private mimeUtils: CoreMimetypeUtilsProvider) { constructor(private file: File, private textUtils: CoreTextUtilsProvider) {
super(); super();
} }
/**
* Create a directory. It creates all the foldes in dirPath 1 by 1 to prevent errors.
*
* @param {string} destination Destination parent folder.
* @param {string} dirPath Relative path to the folder.
* @return {Promise<void>} Promise resolved when done.
*/
protected createDir(destination: string, dirPath: string): Promise<void> {
// Create all the folders 1 by 1 in order, otherwise it fails.
const folders = dirPath.split('/');
let promise = Promise.resolve();
for (let i = 0; i < folders.length; i++) {
const folder = folders[i];
promise = promise.then(() => {
return this.file.createDir(destination, folder, true).then(() => {
// Folder created, add it to the destination path.
destination = this.textUtils.concatenatePaths(destination, folder);
});
});
}
return promise;
}
/** /**
* Extracts files from a ZIP archive. * Extracts files from a ZIP archive.
* *
@ -37,35 +63,68 @@ export class ZipMock extends Zip {
* @return {Promise<number>} Promise that resolves with a number. 0 is success, -1 is error. * @return {Promise<number>} Promise that resolves with a number. 0 is success, -1 is error.
*/ */
unzip(source: string, destination: string, onProgress?: Function): Promise<number> { unzip(source: string, destination: string, onProgress?: Function): Promise<number> {
// Replace all %20 with spaces. // Replace all %20 with spaces.
source = source.replace(/%20/g, ' '); source = source.replace(/%20/g, ' ');
destination = destination.replace(/%20/g, ' '); destination = destination.replace(/%20/g, ' ');
const sourceDir = source.substring(0, source.lastIndexOf('/')), const sourceDir = source.substring(0, source.lastIndexOf('/')),
sourceName = source.substr(source.lastIndexOf('/') + 1); sourceName = source.substr(source.lastIndexOf('/') + 1),
zip = new JSZip();
// Read the file first.
return this.file.readAsArrayBuffer(sourceDir, sourceName).then((data) => { return this.file.readAsArrayBuffer(sourceDir, sourceName).then((data) => {
const zip = new JSZip(data),
promises = [],
total = Object.keys(zip.files).length;
let loaded = 0;
if (!zip.files || !zip.files.length) { // Now load the file using the JSZip library.
return zip.loadAsync(data);
}).then((): any => {
if (!zip.files || !Object.keys(zip.files).length) {
// Nothing to extract. // Nothing to extract.
return 0; return 0;
} }
zip.files.forEach((file, name) => { // First of all, create the directory where the files will be unzipped.
let type, const destParent = destination.substring(0, source.lastIndexOf('/')),
promise; destFolderName = destination.substr(source.lastIndexOf('/') + 1);
return this.file.createDir(destParent, destFolderName, false);
}).then(() => {
const promises = [],
total = Object.keys(zip.files).length;
let loaded = 0;
for (const name in zip.files) {
const file = zip.files[name];
let promise;
if (!file.dir) { if (!file.dir) {
// It's a file. Get the mimetype and write the file. // It's a file.
type = this.mimeUtils.getMimeType(this.mimeUtils.getFileExtension(name)); const fileDir = name.substring(0, name.lastIndexOf('/')),
promise = this.file.writeFile(destination, name, new Blob([file.asArrayBuffer()], { type: type })); fileName = name.substr(name.lastIndexOf('/') + 1),
filePromises = [];
let fileData;
if (fileDir) {
// The file is in a subfolder, create it first.
filePromises.push(this.createDir(destination, fileDir));
}
// Read the file contents as a Blob.
filePromises.push(file.async('blob').then((data) => {
fileData = data;
}));
promise = Promise.all(filePromises).then(() => {
// File read and parent folder created, now write the file.
const parentFolder = this.textUtils.concatenatePaths(destination, fileDir);
return this.file.writeFile(parentFolder, fileName, fileData, {replace: true});
});
} else { } else {
// It's a folder, create it if it doesn't exist. // It's a folder, create it if it doesn't exist.
promise = this.file.createDir(destination, name, false); promise = this.createDir(destination, name);
} }
promises.push(promise.then(() => { promises.push(promise.then(() => {
@ -73,7 +132,7 @@ export class ZipMock extends Zip {
loaded++; loaded++;
onProgress && onProgress({ loaded: loaded, total: total }); onProgress && onProgress({ loaded: loaded, total: total });
})); }));
}); }
return Promise.all(promises).then(() => { return Promise.all(promises).then(() => {
return 0; return 0;

View File

@ -38,7 +38,7 @@
</ion-item> </ion-item>
<ion-item text-wrap *ngIf="fileSystemRoot"> <ion-item text-wrap *ngIf="fileSystemRoot">
<h2>{{ 'core.settings.filesystemroot' | translate}}</h2> <h2>{{ 'core.settings.filesystemroot' | translate}}</h2>
<p><a *ngIf="fsClickable" [href]="fileSystemRoot" core-link auto-login="no">{{ filesystemroot }}</a></p> <p><a *ngIf="fsClickable" [href]="fileSystemRoot" core-link auto-login="no">{{ fileSystemRoot }}</a></p>
<p *ngIf="!fsClickable">{{ fileSystemRoot }}</p> <p *ngIf="!fsClickable">{{ fileSystemRoot }}</p>
</ion-item> </ion-item>
<ion-item text-wrap *ngIf="navigator && navigator.userAgent"> <ion-item text-wrap *ngIf="navigator && navigator.userAgent">

View File

@ -731,6 +731,10 @@ export class CoreFileProvider {
destFolder = this.addBasePathIfNeeded(destFolder || this.mimeUtils.removeExtension(path)); destFolder = this.addBasePathIfNeeded(destFolder || this.mimeUtils.removeExtension(path));
return this.zip.unzip(fileEntry.toURL(), destFolder, onProgress); return this.zip.unzip(fileEntry.toURL(), destFolder, onProgress);
}).then((result) => {
if (result == -1) {
return Promise.reject(null);
}
}); });
} }

View File

@ -706,8 +706,8 @@ export class CoreWSProvider {
data = ('response' in xhr) ? xhr.response : xhr.responseText; data = ('response' in xhr) ? xhr.response : xhr.responseText;
// Check status. // Check status.
xhr.status = Math.max(xhr.status === 1223 ? 204 : xhr.status, 0); const status = Math.max(xhr.status === 1223 ? 204 : xhr.status, 0);
if (xhr.status < 200 || xhr.status >= 300) { if (status < 200 || status >= 300) {
// Request failed. // Request failed.
errorResponse.message = data; errorResponse.message = data;