MOBILE-3657 file: Add helper function to get filename from path

main
Pau Ferrer Ocaña 2021-04-16 09:32:42 +02:00
parent 8fda3f39f2
commit 8d31beb0a7
3 changed files with 19 additions and 2 deletions

View File

@ -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);

View File

@ -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);

View File

@ -1390,7 +1390,7 @@ export class CoreDomUtilsProvider {
async showErrorModalDefault(
error: CoreAnyError,
defaultError: string,
needsTranslate?: boolean,
needsTranslate = false,
autocloseTime?: number,
): Promise<HTMLIonAlertElement | null> {
if (this.isCanceledError(error)) {