MOBILE-3639 file: Allow reading files from other folders
parent
8a6954a23c
commit
5c814909cd
|
@ -473,28 +473,39 @@ export class CoreFileProvider {
|
||||||
*
|
*
|
||||||
* @param path Relative path to the file.
|
* @param path Relative path to the file.
|
||||||
* @param format Format to read the file.
|
* @param format Format to read the file.
|
||||||
|
* @param folder Absolute path to the folder where the file is. Use it to read files outside of the app's data folder.
|
||||||
* @return Promise to be resolved when the file is read.
|
* @return Promise to be resolved when the file is read.
|
||||||
*/
|
*/
|
||||||
readFile(
|
readFile(
|
||||||
path: string,
|
path: string,
|
||||||
format?: CoreFileFormat.FORMATTEXT | CoreFileFormat.FORMATDATAURL | CoreFileFormat.FORMATBINARYSTRING,
|
format?: CoreFileFormat.FORMATTEXT | CoreFileFormat.FORMATDATAURL | CoreFileFormat.FORMATBINARYSTRING,
|
||||||
|
folder?: string,
|
||||||
): Promise<string>;
|
): Promise<string>;
|
||||||
readFile(path: string, format: CoreFileFormat.FORMATARRAYBUFFER): Promise<ArrayBuffer>;
|
readFile(path: string, format: CoreFileFormat.FORMATARRAYBUFFER, folder?: string): Promise<ArrayBuffer>;
|
||||||
readFile<T = unknown>(path: string, format: CoreFileFormat.FORMATJSON): Promise<T>;
|
readFile<T = unknown>(path: string, format: CoreFileFormat.FORMATJSON, folder?: string): Promise<T>;
|
||||||
readFile(path: string, format: CoreFileFormat = CoreFileFormat.FORMATTEXT): Promise<string | ArrayBuffer | unknown> {
|
readFile(
|
||||||
// Remove basePath if it's in the path.
|
path: string,
|
||||||
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
format: CoreFileFormat = CoreFileFormat.FORMATTEXT,
|
||||||
this.logger.debug('Read file ' + path + ' with format ' + format);
|
folder?: string,
|
||||||
|
): Promise<string | ArrayBuffer | unknown> {
|
||||||
|
if (!folder) {
|
||||||
|
folder = this.basePath;
|
||||||
|
|
||||||
|
// Remove basePath if it's in the path.
|
||||||
|
path = this.removeStartingSlash(path.replace(this.basePath, ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.debug(`Read file ${path} with format ${format} in folder ${folder}`);
|
||||||
|
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case CoreFileFormat.FORMATDATAURL:
|
case CoreFileFormat.FORMATDATAURL:
|
||||||
return File.readAsDataURL(this.basePath, path);
|
return File.readAsDataURL(folder, path);
|
||||||
case CoreFileFormat.FORMATBINARYSTRING:
|
case CoreFileFormat.FORMATBINARYSTRING:
|
||||||
return File.readAsBinaryString(this.basePath, path);
|
return File.readAsBinaryString(folder, path);
|
||||||
case CoreFileFormat.FORMATARRAYBUFFER:
|
case CoreFileFormat.FORMATARRAYBUFFER:
|
||||||
return File.readAsArrayBuffer(this.basePath, path);
|
return File.readAsArrayBuffer(folder, path);
|
||||||
case CoreFileFormat.FORMATJSON:
|
case CoreFileFormat.FORMATJSON:
|
||||||
return File.readAsText(this.basePath, path).then((text) => {
|
return File.readAsText(folder, path).then((text) => {
|
||||||
const parsed = CoreTextUtils.parseJSON(text, null);
|
const parsed = CoreTextUtils.parseJSON(text, null);
|
||||||
|
|
||||||
if (parsed == null && text != null) {
|
if (parsed == null && text != null) {
|
||||||
|
@ -504,7 +515,7 @@ export class CoreFileProvider {
|
||||||
return parsed;
|
return parsed;
|
||||||
});
|
});
|
||||||
default:
|
default:
|
||||||
return File.readAsText(this.basePath, path);
|
return File.readAsText(folder, path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue