MOBILE-4145 core: Clean up file path methods

main
Noel De Martin 2022-10-26 16:53:01 +02:00
parent b96b349cf2
commit 609a948945
1 changed files with 15 additions and 31 deletions

View File

@ -217,8 +217,7 @@ export class CoreFileProvider {
): Promise<FileEntry | DirectoryEntry> { ): Promise<FileEntry | DirectoryEntry> {
await this.init(); await this.init();
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
base = base || this.basePath; base = base || this.basePath;
if (path.indexOf('/') == -1) { if (path.indexOf('/') == -1) {
@ -280,8 +279,7 @@ export class CoreFileProvider {
async removeDir(path: string): Promise<void> { async removeDir(path: string): Promise<void> {
await this.init(); await this.init();
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
this.logger.debug('Remove directory: ' + path); this.logger.debug('Remove directory: ' + path);
await File.removeRecursively(this.basePath, path); await File.removeRecursively(this.basePath, path);
@ -296,8 +294,7 @@ export class CoreFileProvider {
async removeFile(path: string): Promise<void> { async removeFile(path: string): Promise<void> {
await this.init(); await this.init();
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
this.logger.debug('Remove file: ' + path); this.logger.debug('Remove file: ' + path);
try { try {
@ -333,8 +330,7 @@ export class CoreFileProvider {
async getDirectoryContents(path: string): Promise<(FileEntry | DirectoryEntry)[]> { async getDirectoryContents(path: string): Promise<(FileEntry | DirectoryEntry)[]> {
await this.init(); await this.init();
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
this.logger.debug('Get contents of dir: ' + path); this.logger.debug('Get contents of dir: ' + path);
const result = await File.listDir(this.basePath, path); const result = await File.listDir(this.basePath, path);
@ -402,8 +398,7 @@ export class CoreFileProvider {
* @return Promise to be resolved when the size is calculated. * @return Promise to be resolved when the size is calculated.
*/ */
getDirectorySize(path: string): Promise<number> { getDirectorySize(path: string): Promise<number> {
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
this.logger.debug('Get size of dir: ' + path); this.logger.debug('Get size of dir: ' + path);
@ -417,8 +412,7 @@ export class CoreFileProvider {
* @return Promise to be resolved when the size is calculated. * @return Promise to be resolved when the size is calculated.
*/ */
getFileSize(path: string): Promise<number> { getFileSize(path: string): Promise<number> {
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
this.logger.debug('Get size of file: ' + path); this.logger.debug('Get size of file: ' + path);
@ -491,8 +485,7 @@ export class CoreFileProvider {
if (!folder) { if (!folder) {
folder = this.basePath; folder = this.basePath;
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
} }
this.logger.debug(`Read file ${path} with format ${format} in folder ${folder}`); this.logger.debug(`Read file ${path} with format ${format} in folder ${folder}`);
@ -593,8 +586,7 @@ export class CoreFileProvider {
async writeFile(path: string, data: string | Blob, append?: boolean): Promise<FileEntry> { async writeFile(path: string, data: string | Blob, append?: boolean): Promise<FileEntry> {
await this.init(); await this.init();
// Remove basePath if it's in the path. path = this.removeBasePath(path);
path = this.removeStartingSlash(path.replace(this.basePath, ''));
this.logger.debug('Write file: ' + path); this.logger.debug('Write file: ' + path);
// Create file (and parent folders) to prevent errors. // Create file (and parent folders) to prevent errors.
@ -840,9 +832,8 @@ export class CoreFileProvider {
await this.init(); await this.init();
// Paths cannot start with "/". Remove basePath if present. from = this.removeBasePath(from);
from = this.removeStartingSlash(from.replace(this.basePath, '')); to = this.removeBasePath(to);
to = this.removeStartingSlash(to.replace(this.basePath, ''));
const toFileAndDir = this.getFileAndDirectoryFromPath(to); const toFileAndDir = this.getFileAndDirectoryFromPath(to);
@ -925,17 +916,13 @@ export class CoreFileProvider {
} }
/** /**
* Remove the base path from a path. If basePath isn't found, return false. * Remove the base path from a path.
* *
* @param path Path to treat. * @param path Path to treat.
* @return Path without basePath if basePath was found, undefined otherwise. * @return Path without basePath.
*/ */
removeBasePath(path: string): string { removeBasePath(path: string): string {
if (path.indexOf(this.basePath) > -1) { return CoreText.removeStartingSlash(path.replace(this.basePath, ''));
return path.replace(this.basePath, '');
}
return path;
} }
/** /**
@ -1036,13 +1023,10 @@ export class CoreFileProvider {
* *
* @param path Path. * @param path Path.
* @return Path without a slash in the first position. * @return Path without a slash in the first position.
* @deprecated since 4.1. Use CoreText.removeStartingSlash instead.
*/ */
removeStartingSlash(path: string): string { removeStartingSlash(path: string): string {
if (path[0] == '/') { return CoreText.removeStartingSlash(path);
return path.substring(1);
}
return path;
} }
/** /**