MOBILE-3565 core: Fix some ESLint of CoreFileProvider
parent
a790a093d1
commit
bb23f71028
|
@ -14,15 +14,18 @@
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
import { FileEntry, DirectoryEntry, Entry, Metadata } from '@ionic-native/file';
|
import { FileEntry, DirectoryEntry, Entry, Metadata, IFile } from '@ionic-native/file';
|
||||||
|
|
||||||
import { CoreApp } from '@services/app';
|
import { CoreApp } from '@services/app';
|
||||||
|
import { CoreWSExternalFile } from '@services/ws';
|
||||||
import { CoreMimetypeUtils } from '@services/utils/mimetype';
|
import { CoreMimetypeUtils } from '@services/utils/mimetype';
|
||||||
import { CoreTextUtils } from '@services/utils/text';
|
import { CoreTextUtils } from '@services/utils/text';
|
||||||
|
import { CoreUtils } from '@services/utils/utils';
|
||||||
import CoreConfigConstants from '@app/config.json';
|
import CoreConfigConstants from '@app/config.json';
|
||||||
|
import { CoreError } from '@classes/errors/error';
|
||||||
|
|
||||||
import { CoreLogger } from '@singletons/logger';
|
import { CoreLogger } from '@singletons/logger';
|
||||||
import { makeSingleton, File, Zip, Platform } from '@singletons/core.singletons';
|
import { makeSingleton, File, Zip, Platform, WebView } from '@singletons/core.singletons';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Progress event used when writing a file data into a file.
|
* Progress event used when writing a file data into a file.
|
||||||
|
@ -49,23 +52,35 @@ export type CoreFileProgressEvent = {
|
||||||
*/
|
*/
|
||||||
export type CoreFileProgressFunction = (event: CoreFileProgressEvent) => void;
|
export type CoreFileProgressFunction = (event: CoreFileProgressEvent) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constants to define the format to read a file.
|
||||||
|
*/
|
||||||
|
export const enum CoreFileFormat {
|
||||||
|
FORMATTEXT = 0,
|
||||||
|
FORMATDATAURL = 1,
|
||||||
|
FORMATBINARYSTRING = 2,
|
||||||
|
FORMATARRAYBUFFER = 3,
|
||||||
|
FORMATJSON = 4,
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory to interact with the file system.
|
* Factory to interact with the file system.
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CoreFileProvider {
|
export class CoreFileProvider {
|
||||||
|
|
||||||
// Formats to read a file.
|
// Formats to read a file.
|
||||||
static FORMATTEXT = 0;
|
static readonly FORMATTEXT = CoreFileFormat.FORMATTEXT;
|
||||||
static FORMATDATAURL = 1;
|
static readonly FORMATDATAURL = CoreFileFormat.FORMATDATAURL;
|
||||||
static FORMATBINARYSTRING = 2;
|
static readonly FORMATBINARYSTRING = CoreFileFormat.FORMATBINARYSTRING;
|
||||||
static FORMATARRAYBUFFER = 3;
|
static readonly FORMATARRAYBUFFER = CoreFileFormat.FORMATARRAYBUFFER;
|
||||||
static FORMATJSON = 4;
|
static readonly FORMATJSON = CoreFileFormat.FORMATJSON;
|
||||||
|
|
||||||
// Folders.
|
// Folders.
|
||||||
static SITESFOLDER = 'sites';
|
static readonly SITESFOLDER = 'sites';
|
||||||
static TMPFOLDER = 'tmp';
|
static readonly TMPFOLDER = 'tmp';
|
||||||
|
|
||||||
static CHUNK_SIZE = 1048576; // 1 MB. Same chunk size as Ionic Native.
|
static readonly CHUNK_SIZE = 1048576; // 1 MB. Same chunk size as Ionic Native.
|
||||||
|
|
||||||
protected logger: CoreLogger;
|
protected logger: CoreLogger;
|
||||||
protected initialized = false;
|
protected initialized = false;
|
||||||
|
@ -73,73 +88,9 @@ export class CoreFileProvider {
|
||||||
protected isHTMLAPI = false;
|
protected isHTMLAPI = false;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
||||||
this.logger = CoreLogger.getInstance('CoreFileProvider');
|
this.logger = CoreLogger.getInstance('CoreFileProvider');
|
||||||
|
|
||||||
if (CoreApp.instance.isAndroid() && !Object.getOwnPropertyDescriptor(FileReader.prototype, 'onloadend')) {
|
// @todo: Check if redefining FileReader getters and setters is still needed in Android.
|
||||||
// Cordova File plugin creates some getters and setter for FileReader, but Ionic's polyfills override them in Android.
|
|
||||||
// Create the getters and setters again. This code comes from FileReader.js in cordova-plugin-file.
|
|
||||||
// @todo: Check if this is still needed.
|
|
||||||
this.defineGetterSetter(FileReader.prototype, 'readyState', function(): any {
|
|
||||||
return this._localURL ? this._readyState : this._realReader.readyState;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.defineGetterSetter(FileReader.prototype, 'error', function(): any {
|
|
||||||
return this._localURL ? this._error : this._realReader.error;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.defineGetterSetter(FileReader.prototype, 'result', function(): any {
|
|
||||||
return this._localURL ? this._result : this._realReader.result;
|
|
||||||
});
|
|
||||||
|
|
||||||
this.defineEvent('onloadstart');
|
|
||||||
this.defineEvent('onprogress');
|
|
||||||
this.defineEvent('onload');
|
|
||||||
this.defineEvent('onerror');
|
|
||||||
this.defineEvent('onloadend');
|
|
||||||
this.defineEvent('onabort');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Define an event for FileReader.
|
|
||||||
*
|
|
||||||
* @param eventName Name of the event.
|
|
||||||
*/
|
|
||||||
protected defineEvent(eventName: string): void {
|
|
||||||
this.defineGetterSetter(FileReader.prototype, eventName, function(): any {
|
|
||||||
return this._realReader[eventName] || null;
|
|
||||||
}, function(value: any): void {
|
|
||||||
this._realReader[eventName] = value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Define a getter and, optionally, a setter for a certain property in an object.
|
|
||||||
*
|
|
||||||
* @param obj Object to set the getter/setter for.
|
|
||||||
* @param key Name of the property where to set them.
|
|
||||||
* @param getFunc The getter function.
|
|
||||||
* @param setFunc The setter function.
|
|
||||||
*/
|
|
||||||
protected defineGetterSetter(obj: any, key: string, getFunc: () => any, setFunc?: (value?: any) => any): void {
|
|
||||||
if (Object.defineProperty) {
|
|
||||||
const desc: any = {
|
|
||||||
get: getFunc,
|
|
||||||
configurable: true
|
|
||||||
};
|
|
||||||
|
|
||||||
if (setFunc) {
|
|
||||||
desc.set = setFunc;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.defineProperty(obj, key, desc);
|
|
||||||
} else {
|
|
||||||
obj.__defineGetter__(key, getFunc);
|
|
||||||
if (setFunc) {
|
|
||||||
obj.__defineSetter__(key, setFunc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -172,7 +123,6 @@ export class CoreFileProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Platform.instance.ready().then(() => {
|
return Platform.instance.ready().then(() => {
|
||||||
|
|
||||||
if (CoreApp.instance.isAndroid()) {
|
if (CoreApp.instance.isAndroid()) {
|
||||||
this.basePath = File.instance.externalApplicationStorageDirectory || this.basePath;
|
this.basePath = File.instance.externalApplicationStorageDirectory || this.basePath;
|
||||||
} else if (CoreApp.instance.isIOS()) {
|
} else if (CoreApp.instance.isIOS()) {
|
||||||
|
@ -180,7 +130,7 @@ export class CoreFileProvider {
|
||||||
} else if (!this.isAvailable() || this.basePath === '') {
|
} else if (!this.isAvailable() || this.basePath === '') {
|
||||||
this.logger.error('Error getting device OS.');
|
this.logger.error('Error getting device OS.');
|
||||||
|
|
||||||
return Promise.reject(null);
|
return Promise.reject(new CoreError('Error getting device OS to initialize file system.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
|
@ -208,9 +158,7 @@ export class CoreFileProvider {
|
||||||
this.logger.debug('Get file: ' + path);
|
this.logger.debug('Get file: ' + path);
|
||||||
|
|
||||||
return File.instance.resolveLocalFilesystemUrl(this.addBasePathIfNeeded(path));
|
return File.instance.resolveLocalFilesystemUrl(this.addBasePathIfNeeded(path));
|
||||||
}).then((entry) => {
|
}).then((entry) => <FileEntry> entry);
|
||||||
return <FileEntry> entry;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -246,8 +194,10 @@ export class CoreFileProvider {
|
||||||
* @param base Base path to create the dir/file in. If not set, use basePath.
|
* @param base Base path to create the dir/file in. If not set, use basePath.
|
||||||
* @return Promise to be resolved when the dir/file is created.
|
* @return Promise to be resolved when the dir/file is created.
|
||||||
*/
|
*/
|
||||||
protected create(isDirectory: boolean, path: string, failIfExists?: boolean, base?: string): Promise<any> {
|
protected async create(isDirectory: boolean, path: string, failIfExists?: boolean, base?: string):
|
||||||
return this.init().then(() => {
|
Promise<FileEntry | DirectoryEntry> {
|
||||||
|
await this.init();
|
||||||
|
|
||||||
// Remove basePath if it's in the path.
|
// Remove basePath if it's in the path.
|
||||||
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
||||||
base = base || this.basePath;
|
base = base || this.basePath;
|
||||||
|
@ -270,15 +220,10 @@ export class CoreFileProvider {
|
||||||
|
|
||||||
this.logger.debug('Create dir ' + firstDir + ' in ' + base);
|
this.logger.debug('Create dir ' + firstDir + ' in ' + base);
|
||||||
|
|
||||||
return File.instance.createDir(base, firstDir, true).then((newDirEntry) => {
|
const newDirEntry = await File.instance.createDir(base, firstDir, true);
|
||||||
return this.create(isDirectory, restOfPath, failIfExists, newDirEntry.toURL());
|
|
||||||
}).catch((error) => {
|
|
||||||
this.logger.error('Error creating directory ' + firstDir + ' in ' + base);
|
|
||||||
|
|
||||||
return Promise.reject(error);
|
return this.create(isDirectory, restOfPath, failIfExists, newDirEntry.toURL());
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -288,8 +233,10 @@ export class CoreFileProvider {
|
||||||
* @param failIfExists True if it should fail if the directory exists, false otherwise.
|
* @param failIfExists True if it should fail if the directory exists, false otherwise.
|
||||||
* @return Promise to be resolved when the directory is created.
|
* @return Promise to be resolved when the directory is created.
|
||||||
*/
|
*/
|
||||||
createDir(path: string, failIfExists?: boolean): Promise<DirectoryEntry> {
|
async createDir(path: string, failIfExists?: boolean): Promise<DirectoryEntry> {
|
||||||
return this.create(true, path, failIfExists);
|
const entry = <DirectoryEntry> await this.create(true, path, failIfExists);
|
||||||
|
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -299,8 +246,10 @@ export class CoreFileProvider {
|
||||||
* @param failIfExists True if it should fail if the file exists, false otherwise..
|
* @param failIfExists True if it should fail if the file exists, false otherwise..
|
||||||
* @return Promise to be resolved when the file is created.
|
* @return Promise to be resolved when the file is created.
|
||||||
*/
|
*/
|
||||||
createFile(path: string, failIfExists?: boolean): Promise<FileEntry> {
|
async createFile(path: string, failIfExists?: boolean): Promise<FileEntry> {
|
||||||
return this.create(false, path, failIfExists);
|
const entry = <FileEntry> await this.create(true, path, failIfExists);
|
||||||
|
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -309,14 +258,14 @@ export class CoreFileProvider {
|
||||||
* @param path Relative path to the directory.
|
* @param path Relative path to the directory.
|
||||||
* @return Promise to be resolved when the directory is deleted.
|
* @return Promise to be resolved when the directory is deleted.
|
||||||
*/
|
*/
|
||||||
removeDir(path: string): Promise<any> {
|
async removeDir(path: string): Promise<void> {
|
||||||
return this.init().then(() => {
|
await this.init();
|
||||||
|
|
||||||
// Remove basePath if it's in the path.
|
// Remove basePath if it's in the path.
|
||||||
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
||||||
this.logger.debug('Remove directory: ' + path);
|
this.logger.debug('Remove directory: ' + path);
|
||||||
|
|
||||||
return File.instance.removeRecursively(this.basePath, path);
|
await File.instance.removeRecursively(this.basePath, path);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -325,23 +274,25 @@ export class CoreFileProvider {
|
||||||
* @param path Relative path to the file.
|
* @param path Relative path to the file.
|
||||||
* @return Promise to be resolved when the file is deleted.
|
* @return Promise to be resolved when the file is deleted.
|
||||||
*/
|
*/
|
||||||
removeFile(path: string): Promise<any> {
|
async removeFile(path: string): Promise<void> {
|
||||||
return this.init().then(() => {
|
await this.init();
|
||||||
|
|
||||||
// Remove basePath if it's in the path.
|
// Remove basePath if it's in the path.
|
||||||
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
||||||
this.logger.debug('Remove file: ' + path);
|
this.logger.debug('Remove file: ' + path);
|
||||||
|
|
||||||
return File.instance.removeFile(this.basePath, path).catch((error) => {
|
try {
|
||||||
|
await File.instance.removeFile(this.basePath, path);
|
||||||
|
} catch (error) {
|
||||||
// The delete can fail if the path has encoded characters. Try again if that's the case.
|
// The delete can fail if the path has encoded characters. Try again if that's the case.
|
||||||
const decodedPath = decodeURI(path);
|
const decodedPath = decodeURI(path);
|
||||||
|
|
||||||
if (decodedPath != path) {
|
if (decodedPath != path) {
|
||||||
return File.instance.removeFile(this.basePath, decodedPath);
|
await File.instance.removeFile(this.basePath, decodedPath);
|
||||||
} else {
|
} else {
|
||||||
return Promise.reject(error);
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -350,10 +301,8 @@ export class CoreFileProvider {
|
||||||
* @param fileEntry File Entry.
|
* @param fileEntry File Entry.
|
||||||
* @return Promise resolved when the file is deleted.
|
* @return Promise resolved when the file is deleted.
|
||||||
*/
|
*/
|
||||||
removeFileByFileEntry(fileEntry: any): Promise<any> {
|
removeFileByFileEntry(entry: Entry): Promise<void> {
|
||||||
return new Promise((resolve, reject): void => {
|
return new Promise((resolve, reject) => entry.remove(resolve, reject));
|
||||||
fileEntry.remove(resolve, reject);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -362,14 +311,26 @@ export class CoreFileProvider {
|
||||||
* @param path Relative path to the directory.
|
* @param path Relative path to the directory.
|
||||||
* @return Promise to be resolved when the contents are retrieved.
|
* @return Promise to be resolved when the contents are retrieved.
|
||||||
*/
|
*/
|
||||||
getDirectoryContents(path: string): Promise<any> {
|
async getDirectoryContents(path: string): Promise<(FileEntry | DirectoryEntry)[]> {
|
||||||
return this.init().then(() => {
|
await this.init();
|
||||||
|
|
||||||
// Remove basePath if it's in the path.
|
// Remove basePath if it's in the path.
|
||||||
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
||||||
this.logger.debug('Get contents of dir: ' + path);
|
this.logger.debug('Get contents of dir: ' + path);
|
||||||
|
|
||||||
return File.instance.listDir(this.basePath, path);
|
const result = await File.instance.listDir(this.basePath, path);
|
||||||
});
|
|
||||||
|
return <(FileEntry | DirectoryEntry)[]> result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type guard to check if the param is a DirectoryEntry.
|
||||||
|
*
|
||||||
|
* @param entry Param to check.
|
||||||
|
* @return Whether the param is a DirectoryEntry.
|
||||||
|
*/
|
||||||
|
protected isDirectoryEntry(entry: FileEntry | DirectoryEntry): entry is DirectoryEntry {
|
||||||
|
return entry.isDirectory === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -378,19 +339,18 @@ export class CoreFileProvider {
|
||||||
* @param entry Directory or file.
|
* @param entry Directory or file.
|
||||||
* @return Promise to be resolved when the size is calculated.
|
* @return Promise to be resolved when the size is calculated.
|
||||||
*/
|
*/
|
||||||
protected getSize(entry: any): Promise<number> {
|
protected getSize(entry: DirectoryEntry | FileEntry): Promise<number> {
|
||||||
return new Promise((resolve, reject): void => {
|
return new Promise((resolve, reject) => {
|
||||||
if (entry.isDirectory) {
|
if (this.isDirectoryEntry(entry)) {
|
||||||
const directoryReader = entry.createReader();
|
const directoryReader = entry.createReader();
|
||||||
directoryReader.readEntries((entries) => {
|
|
||||||
|
|
||||||
|
directoryReader.readEntries((entries: (DirectoryEntry | FileEntry)[]) => {
|
||||||
const promises = [];
|
const promises = [];
|
||||||
for (let i = 0; i < entries.length; i++) {
|
for (let i = 0; i < entries.length; i++) {
|
||||||
promises.push(this.getSize(entries[i]));
|
promises.push(this.getSize(entries[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
Promise.all(promises).then((sizes) => {
|
Promise.all(promises).then((sizes) => {
|
||||||
|
|
||||||
let directorySize = 0;
|
let directorySize = 0;
|
||||||
for (let i = 0; i < sizes.length; i++) {
|
for (let i = 0; i < sizes.length; i++) {
|
||||||
const fileSize = Number(sizes[i]);
|
const fileSize = Number(sizes[i]);
|
||||||
|
@ -402,12 +362,9 @@ export class CoreFileProvider {
|
||||||
directorySize += fileSize;
|
directorySize += fileSize;
|
||||||
}
|
}
|
||||||
resolve(directorySize);
|
resolve(directorySize);
|
||||||
|
|
||||||
}, reject);
|
}, reject);
|
||||||
|
|
||||||
}, reject);
|
}, reject);
|
||||||
|
} else {
|
||||||
} else if (entry.isFile) {
|
|
||||||
entry.file((file) => {
|
entry.file((file) => {
|
||||||
resolve(file.size);
|
resolve(file.size);
|
||||||
}, reject);
|
}, reject);
|
||||||
|
@ -427,9 +384,7 @@ export class CoreFileProvider {
|
||||||
|
|
||||||
this.logger.debug('Get size of dir: ' + path);
|
this.logger.debug('Get size of dir: ' + path);
|
||||||
|
|
||||||
return this.getDir(path).then((dirEntry) => {
|
return this.getDir(path).then((dirEntry) => this.getSize(dirEntry));
|
||||||
return this.getSize(dirEntry);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -444,9 +399,7 @@ export class CoreFileProvider {
|
||||||
|
|
||||||
this.logger.debug('Get size of file: ' + path);
|
this.logger.debug('Get size of file: ' + path);
|
||||||
|
|
||||||
return this.getFile(path).then((fileEntry) => {
|
return this.getFile(path).then((fileEntry) => this.getSize(fileEntry));
|
||||||
return this.getSize(fileEntry);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -455,7 +408,7 @@ export class CoreFileProvider {
|
||||||
* @param path Relative path to the file.
|
* @param path Relative path to the file.
|
||||||
* @return Promise to be resolved when the file is retrieved.
|
* @return Promise to be resolved when the file is retrieved.
|
||||||
*/
|
*/
|
||||||
getFileObjectFromFileEntry(entry: FileEntry): Promise<any> {
|
getFileObjectFromFileEntry(entry: FileEntry): Promise<IFile> {
|
||||||
return new Promise((resolve, reject): void => {
|
return new Promise((resolve, reject): void => {
|
||||||
this.logger.debug('Get file object of: ' + entry.fullPath);
|
this.logger.debug('Get file object of: ' + entry.fullPath);
|
||||||
entry.file(resolve, reject);
|
entry.file(resolve, reject);
|
||||||
|
@ -496,15 +449,10 @@ export class CoreFileProvider {
|
||||||
* Read a file from local file system.
|
* Read a file from local file system.
|
||||||
*
|
*
|
||||||
* @param path Relative path to the file.
|
* @param path Relative path to the file.
|
||||||
* @param format Format to read the file. Must be one of:
|
* @param format Format to read the file.
|
||||||
* FORMATTEXT
|
|
||||||
* FORMATDATAURL
|
|
||||||
* FORMATBINARYSTRING
|
|
||||||
* FORMATARRAYBUFFER
|
|
||||||
* FORMATJSON
|
|
||||||
* @return Promise to be resolved when the file is read.
|
* @return Promise to be resolved when the file is read.
|
||||||
*/
|
*/
|
||||||
readFile(path: string, format: number = CoreFileProvider.FORMATTEXT): Promise<any> {
|
readFile(path: string, format: CoreFileFormat = CoreFileProvider.FORMATTEXT): Promise<string | ArrayBuffer | unknown> {
|
||||||
// Remove basePath if it's in the path.
|
// Remove basePath if it's in the path.
|
||||||
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
||||||
this.logger.debug('Read file ' + path + ' with format ' + format);
|
this.logger.debug('Read file ' + path + ' with format ' + format);
|
||||||
|
@ -521,7 +469,7 @@ export class CoreFileProvider {
|
||||||
const parsed = CoreTextUtils.instance.parseJSON(text, null);
|
const parsed = CoreTextUtils.instance.parseJSON(text, null);
|
||||||
|
|
||||||
if (parsed == null && text != null) {
|
if (parsed == null && text != null) {
|
||||||
return Promise.reject('Error parsing JSON file: ' + path);
|
return Promise.reject(new CoreError('Error parsing JSON file: ' + path));
|
||||||
}
|
}
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
|
@ -535,27 +483,21 @@ export class CoreFileProvider {
|
||||||
* Read file contents from a file data object.
|
* Read file contents from a file data object.
|
||||||
*
|
*
|
||||||
* @param fileData File's data.
|
* @param fileData File's data.
|
||||||
* @param format Format to read the file. Must be one of:
|
* @param format Format to read the file.
|
||||||
* FORMATTEXT
|
|
||||||
* FORMATDATAURL
|
|
||||||
* FORMATBINARYSTRING
|
|
||||||
* FORMATARRAYBUFFER
|
|
||||||
* FORMATJSON
|
|
||||||
* @return Promise to be resolved when the file is read.
|
* @return Promise to be resolved when the file is read.
|
||||||
*/
|
*/
|
||||||
readFileData(fileData: any, format: number = CoreFileProvider.FORMATTEXT): Promise<any> {
|
readFileData(fileData: IFile, format: CoreFileFormat = CoreFileProvider.FORMATTEXT): Promise<string | ArrayBuffer | unknown> {
|
||||||
format = format || CoreFileProvider.FORMATTEXT;
|
format = format || CoreFileProvider.FORMATTEXT;
|
||||||
this.logger.debug('Read file from file data with format ' + format);
|
this.logger.debug('Read file from file data with format ' + format);
|
||||||
|
|
||||||
return new Promise((resolve, reject): void => {
|
return new Promise((resolve, reject): void => {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
|
|
||||||
reader.onloadend = (evt): void => {
|
reader.onloadend = (event): void => {
|
||||||
const target = <any> evt.target; // Convert to <any> to be able to use non-standard properties.
|
if (event.target.result !== undefined && event.target.result !== null) {
|
||||||
if (target.result !== undefined && target.result !== null) {
|
|
||||||
if (format == CoreFileProvider.FORMATJSON) {
|
if (format == CoreFileProvider.FORMATJSON) {
|
||||||
// Convert to object.
|
// Convert to object.
|
||||||
const parsed = CoreTextUtils.instance.parseJSON(target.result, null);
|
const parsed = CoreTextUtils.instance.parseJSON(<string> event.target.result, null);
|
||||||
|
|
||||||
if (parsed == null) {
|
if (parsed == null) {
|
||||||
reject('Error parsing JSON file.');
|
reject('Error parsing JSON file.');
|
||||||
|
@ -563,10 +505,10 @@ export class CoreFileProvider {
|
||||||
|
|
||||||
resolve(parsed);
|
resolve(parsed);
|
||||||
} else {
|
} else {
|
||||||
resolve(target.result);
|
resolve(event.target.result);
|
||||||
}
|
}
|
||||||
} else if (target.error !== undefined && target.error !== null) {
|
} else if (event.target.error !== undefined && event.target.error !== null) {
|
||||||
reject(target.error);
|
reject(event.target.error);
|
||||||
} else {
|
} else {
|
||||||
reject({ code: null, message: 'READER_ONLOADEND_ERR' });
|
reject({ code: null, message: 'READER_ONLOADEND_ERR' });
|
||||||
}
|
}
|
||||||
|
@ -575,7 +517,7 @@ export class CoreFileProvider {
|
||||||
// Check if the load starts. If it doesn't start in 3 seconds, reject.
|
// Check if the load starts. If it doesn't start in 3 seconds, reject.
|
||||||
// Sometimes in Android the read doesn't start for some reason, so the promise never finishes.
|
// Sometimes in Android the read doesn't start for some reason, so the promise never finishes.
|
||||||
let hasStarted = false;
|
let hasStarted = false;
|
||||||
reader.onloadstart = (evt): void => {
|
reader.onloadstart = () => {
|
||||||
hasStarted = true;
|
hasStarted = true;
|
||||||
};
|
};
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -597,7 +539,6 @@ export class CoreFileProvider {
|
||||||
default:
|
default:
|
||||||
reader.readAsText(fileData);
|
reader.readAsText(fileData);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -609,7 +550,7 @@ export class CoreFileProvider {
|
||||||
* @param append Whether to append the data to the end of the file.
|
* @param append Whether to append the data to the end of the file.
|
||||||
* @return Promise to be resolved when the file is written.
|
* @return Promise to be resolved when the file is written.
|
||||||
*/
|
*/
|
||||||
writeFile(path: string, data: any, append?: boolean): Promise<FileEntry> {
|
writeFile(path: string, data: string | Blob, append?: boolean): Promise<FileEntry> {
|
||||||
return this.init().then(() => {
|
return this.init().then(() => {
|
||||||
// Remove basePath if it's in the path.
|
// Remove basePath if it's in the path.
|
||||||
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
||||||
|
@ -624,9 +565,8 @@ export class CoreFileProvider {
|
||||||
data = new Blob([data], { type: type || 'text/plain' });
|
data = new Blob([data], { type: type || 'text/plain' });
|
||||||
}
|
}
|
||||||
|
|
||||||
return File.instance.writeFile(this.basePath, path, data, { replace: !append, append: !!append }).then(() => {
|
return File.instance.writeFile(this.basePath, path, data, { replace: !append, append: !!append })
|
||||||
return fileEntry;
|
.then(() => fileEntry);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -645,7 +585,6 @@ export class CoreFileProvider {
|
||||||
*/
|
*/
|
||||||
async writeFileDataInFile(file: Blob, path: string, onProgress?: CoreFileProgressFunction, offset: number = 0,
|
async writeFileDataInFile(file: Blob, path: string, onProgress?: CoreFileProgressFunction, offset: number = 0,
|
||||||
append?: boolean): Promise<FileEntry> {
|
append?: boolean): Promise<FileEntry> {
|
||||||
|
|
||||||
offset = offset || 0;
|
offset = offset || 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -659,7 +598,7 @@ export class CoreFileProvider {
|
||||||
onProgress && onProgress({
|
onProgress && onProgress({
|
||||||
lengthComputable: true,
|
lengthComputable: true,
|
||||||
loaded: offset,
|
loaded: offset,
|
||||||
total: file.size
|
total: file.size,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (offset >= file.size) {
|
if (offset >= file.size) {
|
||||||
|
@ -671,8 +610,8 @@ export class CoreFileProvider {
|
||||||
return this.writeFileDataInFile(file, path, onProgress, offset, true);
|
return this.writeFileDataInFile(file, path, onProgress, offset, true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error && error.target && error.target.error) {
|
if (error && error.target && error.target.error) {
|
||||||
// Error returned by the writer, get the "real" error.
|
// Error returned by the writer, throw the "real" error.
|
||||||
error = error.target.error;
|
throw error.target.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw error;
|
throw error;
|
||||||
|
@ -686,9 +625,7 @@ export class CoreFileProvider {
|
||||||
* @return Promise to be resolved when the file is retrieved.
|
* @return Promise to be resolved when the file is retrieved.
|
||||||
*/
|
*/
|
||||||
getExternalFile(fullPath: string): Promise<FileEntry> {
|
getExternalFile(fullPath: string): Promise<FileEntry> {
|
||||||
return File.instance.resolveLocalFilesystemUrl(fullPath).then((entry) => {
|
return File.instance.resolveLocalFilesystemUrl(fullPath).then((entry) => <FileEntry> entry);
|
||||||
return <FileEntry> entry;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -709,11 +646,11 @@ export class CoreFileProvider {
|
||||||
* @param fullPath Absolute path to the file.
|
* @param fullPath Absolute path to the file.
|
||||||
* @return Promise to be resolved when the file is removed.
|
* @return Promise to be resolved when the file is removed.
|
||||||
*/
|
*/
|
||||||
removeExternalFile(fullPath: string): Promise<any> {
|
async removeExternalFile(fullPath: string): Promise<void> {
|
||||||
const directory = fullPath.substring(0, fullPath.lastIndexOf('/'));
|
const directory = fullPath.substring(0, fullPath.lastIndexOf('/'));
|
||||||
const filename = fullPath.substr(fullPath.lastIndexOf('/') + 1);
|
const filename = fullPath.substr(fullPath.lastIndexOf('/') + 1);
|
||||||
|
|
||||||
return File.instance.removeFile(directory, filename);
|
await File.instance.removeFile(directory, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -742,9 +679,7 @@ export class CoreFileProvider {
|
||||||
return this.init().then(() => {
|
return this.init().then(() => {
|
||||||
if (CoreApp.instance.isIOS()) {
|
if (CoreApp.instance.isIOS()) {
|
||||||
// In iOS we want the internal URL (cdvfile://localhost/persistent/...).
|
// In iOS we want the internal URL (cdvfile://localhost/persistent/...).
|
||||||
return File.instance.resolveDirectoryUrl(this.basePath).then((dirEntry) => {
|
return File.instance.resolveDirectoryUrl(this.basePath).then((dirEntry) => dirEntry.toInternalURL());
|
||||||
return dirEntry.toInternalURL();
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// In the other platforms we use the basePath as it is (file://...).
|
// In the other platforms we use the basePath as it is (file://...).
|
||||||
return this.basePath;
|
return this.basePath;
|
||||||
|
@ -776,8 +711,10 @@ export class CoreFileProvider {
|
||||||
* try to create it (slower).
|
* try to create it (slower).
|
||||||
* @return Promise resolved when the entry is moved.
|
* @return Promise resolved when the entry is moved.
|
||||||
*/
|
*/
|
||||||
moveDir(originalPath: string, newPath: string, destDirExists?: boolean): Promise<any> {
|
async moveDir(originalPath: string, newPath: string, destDirExists?: boolean): Promise<DirectoryEntry> {
|
||||||
return this.copyOrMoveFileOrDir(originalPath, newPath, true, false, destDirExists);
|
const entry = await this.copyOrMoveFileOrDir(originalPath, newPath, true, false, destDirExists);
|
||||||
|
|
||||||
|
return <DirectoryEntry> entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -789,8 +726,10 @@ export class CoreFileProvider {
|
||||||
* try to create it (slower).
|
* try to create it (slower).
|
||||||
* @return Promise resolved when the entry is moved.
|
* @return Promise resolved when the entry is moved.
|
||||||
*/
|
*/
|
||||||
moveFile(originalPath: string, newPath: string, destDirExists?: boolean): Promise<any> {
|
async moveFile(originalPath: string, newPath: string, destDirExists?: boolean): Promise<FileEntry> {
|
||||||
return this.copyOrMoveFileOrDir(originalPath, newPath, false, false, destDirExists);
|
const entry = await this.copyOrMoveFileOrDir(originalPath, newPath, false, false, destDirExists);
|
||||||
|
|
||||||
|
return <FileEntry> entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -802,8 +741,10 @@ export class CoreFileProvider {
|
||||||
* try to create it (slower).
|
* try to create it (slower).
|
||||||
* @return Promise resolved when the entry is copied.
|
* @return Promise resolved when the entry is copied.
|
||||||
*/
|
*/
|
||||||
copyDir(from: string, to: string, destDirExists?: boolean): Promise<any> {
|
async copyDir(from: string, to: string, destDirExists?: boolean): Promise<DirectoryEntry> {
|
||||||
return this.copyOrMoveFileOrDir(from, to, true, true, destDirExists);
|
const entry = await this.copyOrMoveFileOrDir(from, to, true, true, destDirExists);
|
||||||
|
|
||||||
|
return <DirectoryEntry> entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -815,8 +756,10 @@ export class CoreFileProvider {
|
||||||
* try to create it (slower).
|
* try to create it (slower).
|
||||||
* @return Promise resolved when the entry is copied.
|
* @return Promise resolved when the entry is copied.
|
||||||
*/
|
*/
|
||||||
copyFile(from: string, to: string, destDirExists?: boolean): Promise<any> {
|
async copyFile(from: string, to: string, destDirExists?: boolean): Promise<FileEntry> {
|
||||||
return this.copyOrMoveFileOrDir(from, to, false, true, destDirExists);
|
const entry = await this.copyOrMoveFileOrDir(from, to, false, true, destDirExists);
|
||||||
|
|
||||||
|
return <FileEntry> entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -830,16 +773,16 @@ export class CoreFileProvider {
|
||||||
* try to create it (slower).
|
* try to create it (slower).
|
||||||
* @return Promise resolved when the entry is copied.
|
* @return Promise resolved when the entry is copied.
|
||||||
*/
|
*/
|
||||||
protected async copyOrMoveFileOrDir(from: string, to: string, isDir?: boolean, copy?: boolean, destDirExists?: boolean)
|
protected async copyOrMoveFileOrDir(from: string, to: string, isDir?: boolean, copy?: boolean, destDirExists?: boolean):
|
||||||
: Promise<Entry> {
|
Promise<FileEntry | DirectoryEntry> {
|
||||||
|
|
||||||
const fileIsInAppFolder = this.isPathInAppFolder(from);
|
const fileIsInAppFolder = this.isPathInAppFolder(from);
|
||||||
|
|
||||||
if (!fileIsInAppFolder) {
|
if (!fileIsInAppFolder) {
|
||||||
return this.copyOrMoveExternalFile(from, to, copy);
|
return this.copyOrMoveExternalFile(from, to, copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
const moveCopyFn = copy ?
|
const moveCopyFn: (path: string, dirName: string, newPath: string, newDirName: string) =>
|
||||||
|
Promise<FileEntry | DirectoryEntry> = copy ?
|
||||||
(isDir ? File.instance.copyDir.bind(File.instance) : File.instance.copyFile.bind(File.instance)) :
|
(isDir ? File.instance.copyDir.bind(File.instance) : File.instance.copyFile.bind(File.instance)) :
|
||||||
(isDir ? File.instance.moveDir.bind(File.instance) : File.instance.moveFile.bind(File.instance));
|
(isDir ? File.instance.moveDir.bind(File.instance) : File.instance.moveFile.bind(File.instance));
|
||||||
|
|
||||||
|
@ -885,10 +828,10 @@ export class CoreFileProvider {
|
||||||
* path/ -> directory: 'path', name: ''
|
* path/ -> directory: 'path', name: ''
|
||||||
* path -> directory: '', name: 'path'
|
* path -> directory: '', name: 'path'
|
||||||
*/
|
*/
|
||||||
getFileAndDirectoryFromPath(path: string): {directory: string, name: string} {
|
getFileAndDirectoryFromPath(path: string): {directory: string; name: string} {
|
||||||
const file = {
|
const file = {
|
||||||
directory: '',
|
directory: '',
|
||||||
name: ''
|
name: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
file.directory = path.substring(0, path.lastIndexOf('/'));
|
file.directory = path.substring(0, path.lastIndexOf('/'));
|
||||||
|
@ -949,7 +892,8 @@ export class CoreFileProvider {
|
||||||
* @param recreateDir Delete the dest directory before unzipping. Defaults to true.
|
* @param recreateDir Delete the dest directory before unzipping. Defaults to true.
|
||||||
* @return Promise resolved when the file is unzipped.
|
* @return Promise resolved when the file is unzipped.
|
||||||
*/
|
*/
|
||||||
unzipFile(path: string, destFolder?: string, onProgress?: (progress: any) => void, recreateDir: boolean = true): Promise<any> {
|
unzipFile(path: string, destFolder?: string, onProgress?: (progress: ProgressEvent) => void, recreateDir: boolean = true):
|
||||||
|
Promise<void> {
|
||||||
// Get the source file.
|
// Get the source file.
|
||||||
let fileEntry: FileEntry;
|
let fileEntry: FileEntry;
|
||||||
|
|
||||||
|
@ -960,10 +904,10 @@ export class CoreFileProvider {
|
||||||
// Make sure the dest dir doesn't exist already.
|
// Make sure the dest dir doesn't exist already.
|
||||||
return this.removeDir(destFolder).catch(() => {
|
return this.removeDir(destFolder).catch(() => {
|
||||||
// Ignore errors.
|
// Ignore errors.
|
||||||
}).then(() => {
|
}).then(() =>
|
||||||
// Now create the dir, otherwise if any of the ancestor dirs doesn't exist the unzip would fail.
|
// Now create the dir, otherwise if any of the ancestor dirs doesn't exist the unzip would fail.
|
||||||
return this.createDir(destFolder);
|
this.createDir(destFolder),
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
// If destFolder is not set, use same location as ZIP file. We need to use absolute paths (including basePath).
|
// If destFolder is not set, use same location as ZIP file. We need to use absolute paths (including basePath).
|
||||||
|
@ -972,7 +916,7 @@ export class CoreFileProvider {
|
||||||
return Zip.instance.unzip(fileEntry.toURL(), destFolder, onProgress);
|
return Zip.instance.unzip(fileEntry.toURL(), destFolder, onProgress);
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
return Promise.reject('Unzip failed.');
|
return Promise.reject(new CoreError('Unzip failed.'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -985,18 +929,18 @@ export class CoreFileProvider {
|
||||||
* @param newValue New value.
|
* @param newValue New value.
|
||||||
* @return Promise resolved in success.
|
* @return Promise resolved in success.
|
||||||
*/
|
*/
|
||||||
replaceInFile(path: string, search: string | RegExp, newValue: string): Promise<any> {
|
async replaceInFile(path: string, search: string | RegExp, newValue: string): Promise<void> {
|
||||||
return this.readFile(path).then((content) => {
|
let content = <string> await this.readFile(path);
|
||||||
|
|
||||||
if (typeof content == 'undefined' || content === null || !content.replace) {
|
if (typeof content == 'undefined' || content === null || !content.replace) {
|
||||||
return Promise.reject(null);
|
throw new CoreError(`Error reading file ${path}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content.match(search)) {
|
if (content.match(search)) {
|
||||||
content = content.replace(search, newValue);
|
content = content.replace(search, newValue);
|
||||||
|
|
||||||
return this.writeFile(path, content);
|
await this.writeFile(path, content);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1007,7 +951,7 @@ export class CoreFileProvider {
|
||||||
*/
|
*/
|
||||||
getMetadata(fileEntry: Entry): Promise<Metadata> {
|
getMetadata(fileEntry: Entry): Promise<Metadata> {
|
||||||
if (!fileEntry || !fileEntry.getMetadata) {
|
if (!fileEntry || !fileEntry.getMetadata) {
|
||||||
return Promise.reject(null);
|
return Promise.reject(new CoreError('Cannot get metadata from file entry.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject): void => {
|
return new Promise((resolve, reject): void => {
|
||||||
|
@ -1022,7 +966,7 @@ export class CoreFileProvider {
|
||||||
* @param isDir True if directory, false if file.
|
* @param isDir True if directory, false if file.
|
||||||
* @return Promise resolved with metadata.
|
* @return Promise resolved with metadata.
|
||||||
*/
|
*/
|
||||||
getMetadataFromPath(path: string, isDir?: boolean): Promise<any> {
|
getMetadataFromPath(path: string, isDir?: boolean): Promise<Metadata> {
|
||||||
let promise;
|
let promise;
|
||||||
if (isDir) {
|
if (isDir) {
|
||||||
promise = this.getDir(path);
|
promise = this.getDir(path);
|
||||||
|
@ -1030,9 +974,7 @@ export class CoreFileProvider {
|
||||||
promise = this.getFile(path);
|
promise = this.getFile(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
return promise.then((entry) => {
|
return promise.then((entry) => this.getMetadata(entry));
|
||||||
return this.getMetadata(entry);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1057,22 +999,22 @@ export class CoreFileProvider {
|
||||||
* @param copy True to copy, false to move.
|
* @param copy True to copy, false to move.
|
||||||
* @return Promise resolved when the entry is copied/moved.
|
* @return Promise resolved when the entry is copied/moved.
|
||||||
*/
|
*/
|
||||||
protected copyOrMoveExternalFile(from: string, to: string, copy?: boolean): Promise<any> {
|
protected copyOrMoveExternalFile(from: string, to: string, copy?: boolean): Promise<FileEntry> {
|
||||||
// Get the file to copy/move.
|
// Get the file to copy/move.
|
||||||
return this.getExternalFile(from).then((fileEntry) => {
|
return this.getExternalFile(from).then((fileEntry) => {
|
||||||
// Create the destination dir if it doesn't exist.
|
// Create the destination dir if it doesn't exist.
|
||||||
const dirAndFile = this.getFileAndDirectoryFromPath(to);
|
const dirAndFile = this.getFileAndDirectoryFromPath(to);
|
||||||
|
|
||||||
return this.createDir(dirAndFile.directory).then((dirEntry) => {
|
return this.createDir(dirAndFile.directory).then((dirEntry) =>
|
||||||
// Now copy/move the file.
|
// Now copy/move the file.
|
||||||
return new Promise((resolve, reject): void => {
|
new Promise((resolve, reject): void => {
|
||||||
if (copy) {
|
if (copy) {
|
||||||
fileEntry.copyTo(dirEntry, dirAndFile.name, resolve, reject);
|
fileEntry.copyTo(dirEntry, dirAndFile.name, (entry: FileEntry) => resolve(entry), reject);
|
||||||
} else {
|
} else {
|
||||||
fileEntry.moveTo(dirEntry, dirAndFile.name, resolve, reject);
|
fileEntry.moveTo(dirEntry, dirAndFile.name, (entry: FileEntry) => resolve(entry), reject);
|
||||||
}
|
}
|
||||||
});
|
}),
|
||||||
});
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1083,7 +1025,7 @@ export class CoreFileProvider {
|
||||||
* @param to Relative new path of the file (inside the app folder).
|
* @param to Relative new path of the file (inside the app folder).
|
||||||
* @return Promise resolved when the entry is copied.
|
* @return Promise resolved when the entry is copied.
|
||||||
*/
|
*/
|
||||||
copyExternalFile(from: string, to: string): Promise<any> {
|
copyExternalFile(from: string, to: string): Promise<FileEntry> {
|
||||||
return this.copyOrMoveExternalFile(from, to, true);
|
return this.copyOrMoveExternalFile(from, to, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1094,7 +1036,7 @@ export class CoreFileProvider {
|
||||||
* @param to Relative new path of the file (inside the app folder).
|
* @param to Relative new path of the file (inside the app folder).
|
||||||
* @return Promise resolved when the entry is moved.
|
* @return Promise resolved when the entry is moved.
|
||||||
*/
|
*/
|
||||||
moveExternalFile(from: string, to: string): Promise<any> {
|
moveExternalFile(from: string, to: string): Promise<FileEntry> {
|
||||||
return this.copyOrMoveExternalFile(from, to, false);
|
return this.copyOrMoveExternalFile(from, to, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1144,10 +1086,10 @@ export class CoreFileProvider {
|
||||||
// Ask the user what he wants to do.
|
// Ask the user what he wants to do.
|
||||||
return newName;
|
return newName;
|
||||||
}
|
}
|
||||||
}).catch(() => {
|
}).catch(() =>
|
||||||
// Folder doesn't exist, name is unique. Clean it and return it.
|
// Folder doesn't exist, name is unique. Clean it and return it.
|
||||||
return CoreTextUtils.instance.removeSpecialCharactersForFiles(CoreTextUtils.instance.decodeURIComponent(fileName));
|
CoreTextUtils.instance.removeSpecialCharactersForFiles(CoreTextUtils.instance.decodeURIComponent(fileName)),
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1155,10 +1097,9 @@ export class CoreFileProvider {
|
||||||
*
|
*
|
||||||
* @return Promise resolved when done.
|
* @return Promise resolved when done.
|
||||||
*/
|
*/
|
||||||
clearTmpFolder(): Promise<any> {
|
async clearTmpFolder(): Promise<void> {
|
||||||
return this.removeDir(CoreFileProvider.TMPFOLDER).catch(() => {
|
|
||||||
// Ignore errors because the folder might not exist.
|
// Ignore errors because the folder might not exist.
|
||||||
});
|
await CoreUtils.instance.ignoreErrors(this.removeDir(CoreFileProvider.TMPFOLDER));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1168,19 +1109,21 @@ export class CoreFileProvider {
|
||||||
* @param files List of used files.
|
* @param files List of used files.
|
||||||
* @return Promise resolved when done, rejected if failure.
|
* @return Promise resolved when done, rejected if failure.
|
||||||
*/
|
*/
|
||||||
removeUnusedFiles(dirPath: string, files: any[]): Promise<any> {
|
async removeUnusedFiles(dirPath: string, files: (CoreWSExternalFile | FileEntry)[]): Promise<void> {
|
||||||
// Get the directory contents.
|
// Get the directory contents.
|
||||||
return this.getDirectoryContents(dirPath).then((contents) => {
|
try {
|
||||||
|
const contents = await this.getDirectoryContents(dirPath);
|
||||||
|
|
||||||
if (!contents.length) {
|
if (!contents.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filesMap = {};
|
const filesMap: {[fullPath: string]: FileEntry} = {};
|
||||||
const promises = [];
|
const promises = [];
|
||||||
|
|
||||||
// Index the received files by fullPath and ignore the invalid ones.
|
// Index the received files by fullPath and ignore the invalid ones.
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
if (file.fullPath) {
|
if ('fullPath' in file) {
|
||||||
filesMap[file.fullPath] = file;
|
filesMap[file.fullPath] = file;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1193,10 +1136,10 @@ export class CoreFileProvider {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}).catch(() => {
|
} catch (error) {
|
||||||
// Ignore errors, maybe it doesn't exist.
|
// Ignore errors, maybe it doesn't exist.
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1246,7 +1189,7 @@ export class CoreFileProvider {
|
||||||
* @return Converted src.
|
* @return Converted src.
|
||||||
*/
|
*/
|
||||||
convertFileSrc(src: string): string {
|
convertFileSrc(src: string): string {
|
||||||
return CoreApp.instance.isIOS() ? (<any> window).Ionic.WebView.convertFileSrc(src) : src;
|
return CoreApp.instance.isIOS() ? WebView.instance.convertFileSrc(src) : src;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1272,6 +1215,7 @@ export class CoreFileProvider {
|
||||||
protected isPathInAppFolder(path: string): boolean {
|
protected isPathInAppFolder(path: string): boolean {
|
||||||
return !path || !path.match(/^[a-z0-9]+:\/\//i) || path.indexOf(this.basePath) != -1;
|
return !path || !path.match(/^[a-z0-9]+:\/\//i) || path.indexOf(this.basePath) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CoreFile extends makeSingleton(CoreFileProvider) {}
|
export class CoreFile extends makeSingleton(CoreFileProvider) {}
|
||||||
|
|
Loading…
Reference in New Issue