From 8d31beb0a770ccfdc19d19183f91ae5f25e19beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 16 Apr 2021 09:32:42 +0200 Subject: [PATCH] MOBILE-3657 file: Add helper function to get filename from path --- src/addons/mod/assign/services/assign.ts | 3 ++- src/core/services/file-helper.ts | 16 ++++++++++++++++ src/core/services/utils/dom.ts | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/addons/mod/assign/services/assign.ts b/src/addons/mod/assign/services/assign.ts index 5ec111717..72eb9e60e 100644 --- a/src/addons/mod/assign/services/assign.ts +++ b/src/addons/mod/assign/services/assign.ts @@ -33,6 +33,7 @@ import { AddonModAssignSubmissionFormatted } from './assign-helper'; import { CoreWSError } from '@classes/errors/wserror'; import { AddonModAssignAutoSyncData, AddonModAssignManualSyncData, AddonModAssignSyncProvider } from './assign-sync'; import { CoreFormFields } from '@singletons/form'; +import { CoreFileHelper } from '@services/file-helper'; const ROOT_CACHE_KEY = 'mmaModAssign:'; @@ -420,7 +421,7 @@ export class AddonModAssignProvider { filearea.files.forEach((file) => { if (!file.filename) { // We don't have filename, extract it from the path. - file.filename = file.filepath?.charAt(0) == '/' ? file.filepath.substr(1) : file.filepath; + file.filename = CoreFileHelper.getFilenameFromPath(file); } files.push(file); diff --git a/src/core/services/file-helper.ts b/src/core/services/file-helper.ts index aa1745388..566155087 100644 --- a/src/core/services/file-helper.ts +++ b/src/core/services/file-helper.ts @@ -417,6 +417,22 @@ export class CoreFileHelperProvider { return !!fileTypeExcludeList.match(regEx); } + /** + * Extract filename from the path. + * + * @param file The file. + * @return The file name. + */ + getFilenameFromPath(file: CoreFileEntry): string | undefined { + const path = CoreUtils.isFileEntry(file) ? file.fullPath : file.filepath; + + if (typeof path == 'undefined' || path.length == 0) { + return; + } + + return path.split('\\').pop()?.split('/').pop(); + } + } export const CoreFileHelper = makeSingleton(CoreFileHelperProvider); diff --git a/src/core/services/utils/dom.ts b/src/core/services/utils/dom.ts index 606fb9df5..fe1872f05 100644 --- a/src/core/services/utils/dom.ts +++ b/src/core/services/utils/dom.ts @@ -1390,7 +1390,7 @@ export class CoreDomUtilsProvider { async showErrorModalDefault( error: CoreAnyError, defaultError: string, - needsTranslate?: boolean, + needsTranslate = false, autocloseTime?: number, ): Promise { if (this.isCanceledError(error)) {