diff --git a/src/core/components/local-file/local-file.ts b/src/core/components/local-file/local-file.ts index 89cc829fe..f1f5fdcad 100644 --- a/src/core/components/local-file/local-file.ts +++ b/src/core/components/local-file/local-file.ts @@ -29,6 +29,7 @@ import { CorePath } from '@singletons/path'; import { CorePlatform } from '@services/platform'; import { toBoolean } from '@/core/transforms/boolean'; import { CoreLoadings } from '@services/loadings'; +import { CoreFileUtils } from '@singletons/file-utils'; /** * Component to handle a local file. Only files inside the app folder can be managed. @@ -182,7 +183,7 @@ export class CoreLocalFileComponent implements OnInit { } const modal = await CoreLoadings.show(); - const fileAndDir = CoreFile.getFileAndDirectoryFromPath(this.relativePath); + const fileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(this.relativePath); const newPath = CorePath.concatenatePaths(fileAndDir.directory, newName); try { diff --git a/src/core/features/fileuploader/services/fileuploader-helper.ts b/src/core/features/fileuploader/services/fileuploader-helper.ts index c79a55413..a51b8a29c 100644 --- a/src/core/features/fileuploader/services/fileuploader-helper.ts +++ b/src/core/features/fileuploader/services/fileuploader-helper.ts @@ -46,6 +46,7 @@ import { CorePlatform } from '@services/platform'; import { Chooser } from '@features/native/plugins'; import { CoreToasts } from '@services/toasts'; import { CoreLoadings } from '@services/loadings'; +import { CoreFileUtils } from '@singletons/file-utils'; /** * Helper service to upload files. @@ -210,7 +211,7 @@ export class CoreFileUploaderHelperProvider { options?: CoreFileUploaderOptions, ): Promise { - const fileName = options?.fileName || CoreFile.getFileAndDirectoryFromPath(path).name; + const fileName = options?.fileName || CoreFileUtils.getFileAndDirectoryFromPath(path).name; // Check that size isn't too large. if (maxSize !== undefined && maxSize != -1) { @@ -283,7 +284,7 @@ export class CoreFileUploaderHelperProvider { * @returns File name, undefined if cannot get it. */ protected getChosenFileNameFromPath(result: ChooserResult): string | undefined { - const nameAndDir = CoreFile.getFileAndDirectoryFromPath(result.uri); + const nameAndDir = CoreFileUtils.getFileAndDirectoryFromPath(result.uri); if (!nameAndDir.name) { return; diff --git a/src/core/features/fileuploader/services/fileuploader.ts b/src/core/features/fileuploader/services/fileuploader.ts index 4e331f001..34270f5ee 100644 --- a/src/core/features/fileuploader/services/fileuploader.ts +++ b/src/core/features/fileuploader/services/fileuploader.ts @@ -243,7 +243,7 @@ export class CoreFileUploaderProvider { deleteAfterUpload: !isFromAlbum, mimeType: mimetype, }; - const fileName = CoreFile.getFileAndDirectoryFromPath(uri).name; + const fileName = CoreFileUtils.getFileAndDirectoryFromPath(uri).name; if (isIOS && (mimetype == 'image/jpeg' || mimetype == 'image/png')) { // In iOS, the pictures can have repeated names, even if they come from the album. @@ -287,7 +287,7 @@ export class CoreFileUploaderProvider { originalFiles.forEach((file) => { const stillInList = currentFiles.some((currentFile) => - CoreFileHelper.getFileUrl( currentFile) == CoreFileHelper.getFileUrl(file)); + CoreFileHelper.getFileUrl( currentFile) === CoreFileHelper.getFileUrl(file)); if (!stillInList) { filesToDelete.push({ diff --git a/src/core/features/h5p/classes/file-storage.ts b/src/core/features/h5p/classes/file-storage.ts index 3462cd75e..afa16af17 100644 --- a/src/core/features/h5p/classes/file-storage.ts +++ b/src/core/features/h5p/classes/file-storage.ts @@ -29,6 +29,7 @@ import { } from './core'; import { CONTENTS_LIBRARIES_TABLE_NAME, CONTENT_TABLE_NAME, CoreH5PLibraryCachedAssetsDBRecord } from '../services/database/h5p'; import { CoreH5PLibraryBeingSaved } from './storage'; +import { CoreFileUtils } from '@singletons/file-utils'; /** * Equivalent to Moodle's implementation of H5PFileStorage. @@ -115,7 +116,7 @@ export class CoreH5PFileStorage { } treated[url] = url; - const assetPathFolder = CoreFile.getFileAndDirectoryFromPath(assetPath).directory; + const assetPathFolder = CoreFileUtils.getFileAndDirectoryFromPath(assetPath).directory; fileContent = fileContent.replace( new RegExp(CoreText.escapeForRegex(match), 'g'), @@ -300,7 +301,7 @@ export class CoreH5PFileStorage { async getContentFolderNameByUrl(fileUrl: string, siteId: string): Promise { const path = await CoreFilepool.getFilePathByUrl(siteId, fileUrl); - const fileAndDir = CoreFile.getFileAndDirectoryFromPath(path); + const fileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(path); return CoreMimetypeUtils.removeExtension(fileAndDir.name); } diff --git a/src/core/features/sharedfiles/components/list-modal/list-modal.ts b/src/core/features/sharedfiles/components/list-modal/list-modal.ts index 6138c50fd..d113e72af 100644 --- a/src/core/features/sharedfiles/components/list-modal/list-modal.ts +++ b/src/core/features/sharedfiles/components/list-modal/list-modal.ts @@ -17,7 +17,7 @@ import { toBoolean } from '@/core/transforms/boolean'; import { Component, OnInit, Input } from '@angular/core'; import { FileEntry } from '@awesome-cordova-plugins/file/ngx'; -import { CoreFile } from '@services/file'; +import { CoreFileUtils } from '@singletons/file-utils'; import { ModalController, Translate } from '@singletons'; import { CoreSharedFilesComponentsModule } from '../components.module'; @@ -58,7 +58,7 @@ export class CoreSharedFilesListModalComponent implements OnInit { */ calculateTitle(path?: string): void { if (path) { - this.title = CoreFile.getFileAndDirectoryFromPath(path).name; + this.title = CoreFileUtils.getFileAndDirectoryFromPath(path).name; } else { this.title = Translate.instant('core.sharedfiles.sharedfiles'); } diff --git a/src/core/features/sharedfiles/pages/choose-site/choose-site.ts b/src/core/features/sharedfiles/pages/choose-site/choose-site.ts index 271ce4ada..02ff14e50 100644 --- a/src/core/features/sharedfiles/pages/choose-site/choose-site.ts +++ b/src/core/features/sharedfiles/pages/choose-site/choose-site.ts @@ -20,6 +20,7 @@ import { CoreFile } from '@services/file'; import { CoreNavigator } from '@services/navigator'; import { CoreSiteBasicInfo } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; +import { CoreFileUtils } from '@singletons/file-utils'; /** * Page to display the list of sites to choose one to store a shared file. @@ -57,7 +58,7 @@ export class CoreSharedFilesChooseSitePage implements OnInit { } if (this.filePath) { - const fileAndDir = CoreFile.getFileAndDirectoryFromPath(this.filePath); + const fileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(this.filePath); this.fileName = fileAndDir.name; } diff --git a/src/core/features/sharedfiles/pages/list/list.ts b/src/core/features/sharedfiles/pages/list/list.ts index 2525cd2db..c25f49452 100644 --- a/src/core/features/sharedfiles/pages/list/list.ts +++ b/src/core/features/sharedfiles/pages/list/list.ts @@ -14,7 +14,7 @@ import { Component, OnInit } from '@angular/core'; -import { CoreFile } from '@services/file'; +import { CoreFileUtils } from '@singletons/file-utils'; import { CoreNavigator } from '@services/navigator'; import { Translate } from '@singletons'; @@ -54,7 +54,7 @@ export class CoreSharedFilesListPage implements OnInit { */ calculateTitle(path?: string): void { if (path) { - this.title = CoreFile.getFileAndDirectoryFromPath(path).name; + this.title = CoreFileUtils.getFileAndDirectoryFromPath(path).name; } else { this.title = Translate.instant('core.sharedfiles.sharedfiles'); } diff --git a/src/core/services/file.ts b/src/core/services/file.ts index 3f6fe80c3..9e3f4f19b 100644 --- a/src/core/services/file.ts +++ b/src/core/services/file.ts @@ -80,15 +80,11 @@ export class CoreFileProvider { static readonly CHUNK_SIZE = 1048576; // 1 MB. Same chunk size as Ionic Native. - protected logger: CoreLogger; + protected logger = CoreLogger.getInstance('CoreFileProvider'); protected initialized = false; protected basePath = ''; protected isHTMLAPI = false; - constructor() { - this.logger = CoreLogger.getInstance('CoreFileProvider'); - } - /** * Sets basePath to use with HTML API. Reserved for core use. * @@ -438,11 +434,11 @@ export class CoreFileProvider { * * @param filename The file name. * @returns The file name normalized. + * + * @deprecated since 5.0. Not used anymore. */ normalizeFileName(filename: string): string { - filename = CoreUrl.decodeURIComponent(filename); - - return filename; + return CoreUrl.decodeURIComponent(filename); } /** @@ -820,7 +816,7 @@ export class CoreFileProvider { from = this.removeBasePath(from); to = this.removeBasePath(to); - const toFileAndDir = this.getFileAndDirectoryFromPath(to); + const toFileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(to); if (toFileAndDir.directory && !destDirExists) { // Create the target directory if it doesn't exist. @@ -851,23 +847,10 @@ export class CoreFileProvider { * * @param path Path to be extracted. * @returns Plain object containing the file name and directory. - * @description - * file.pdf -> directory: '', name: 'file.pdf' - * /file.pdf -> directory: '', name: 'file.pdf' - * path/file.pdf -> directory: 'path', name: 'file.pdf' - * path/ -> directory: 'path', name: '' - * path -> directory: '', name: 'path' + * @deprecated since 5.0. Use CoreFileUtils.getFileAndDirectoryFromPath instead. */ getFileAndDirectoryFromPath(path: string): {directory: string; name: string} { - const file = { - directory: '', - name: '', - }; - - file.directory = path.substring(0, path.lastIndexOf('/')); - file.name = path.substring(path.lastIndexOf('/') + 1); - - return file; + return CoreFileUtils.getFileAndDirectoryFromPath(path); } /** @@ -1028,7 +1011,7 @@ export class CoreFileProvider { const fileEntry = await this.getExternalFile(from); // Create the destination dir if it doesn't exist. - const dirAndFile = this.getFileAndDirectoryFromPath(to); + const dirAndFile = CoreFileUtils.getFileAndDirectoryFromPath(to); const dirEntry = await this.createDir(dirAndFile.directory); diff --git a/src/core/services/utils/iframe.ts b/src/core/services/utils/iframe.ts index 082bbec19..062c8571c 100644 --- a/src/core/services/utils/iframe.ts +++ b/src/core/services/utils/iframe.ts @@ -38,6 +38,7 @@ import { CoreSite } from '@classes/sites/site'; import { CoreNative } from '@features/native/services/native'; import { CoreLoadings } from '@services/loadings'; import { CorePromiseUtils } from '@singletons/promise-utils'; +import { CoreFileUtils } from '@singletons/file-utils'; type CoreFrameElement = FrameElement & { window?: Window; @@ -431,7 +432,7 @@ export class CoreIframeUtilsProvider { ? ('src' in element ? element.src : element.data) : null; if (src) { - const dirAndFile = CoreFile.getFileAndDirectoryFromPath(src); + const dirAndFile = CoreFileUtils.getFileAndDirectoryFromPath(src); if (dirAndFile.directory) { url = CorePath.concatenatePaths(dirAndFile.directory, url); } else { diff --git a/src/core/singletons/file-utils.ts b/src/core/singletons/file-utils.ts index 8791544b4..85e33ad5a 100644 --- a/src/core/singletons/file-utils.ts +++ b/src/core/singletons/file-utils.ts @@ -64,7 +64,7 @@ export class CoreFileUtils { // Check if there are 2 files with the same name. for (let i = 0; i < files.length; i++) { const file = files[i]; - const name = (this.isFileEntry(file) ? file.name : file.filename) || ''; + const name = (CoreFileUtils.isFileEntry(file) ? file.name : file.filename) || ''; if (names.indexOf(name) > -1) { return Translate.instant('core.filenameexist', { $a: name }); @@ -76,4 +76,28 @@ export class CoreFileUtils { return false; } + /** + * Extract the file name and directory from a given path. + * + * @param path Path to be extracted. + * @returns Plain object containing the file name and directory. + * @description + * file.pdf -> directory: '', name: 'file.pdf' + * /file.pdf -> directory: '', name: 'file.pdf' + * path/file.pdf -> directory: 'path', name: 'file.pdf' + * path/ -> directory: 'path', name: '' + * path -> directory: '', name: 'path' + */ + static getFileAndDirectoryFromPath(path: string): {directory: string; name: string} { + const file = { + directory: '', + name: '', + }; + + file.directory = path.substring(0, path.lastIndexOf('/')); + file.name = path.substring(path.lastIndexOf('/') + 1); + + return file; + } + }