MOBILE-4653 utils: Move file related utils functions
parent
5e3d233272
commit
8ba784564b
|
@ -29,6 +29,7 @@ import { CorePath } from '@singletons/path';
|
||||||
import { CorePlatform } from '@services/platform';
|
import { CorePlatform } from '@services/platform';
|
||||||
import { toBoolean } from '@/core/transforms/boolean';
|
import { toBoolean } from '@/core/transforms/boolean';
|
||||||
import { CoreLoadings } from '@services/loadings';
|
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.
|
* 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 modal = await CoreLoadings.show();
|
||||||
const fileAndDir = CoreFile.getFileAndDirectoryFromPath(this.relativePath);
|
const fileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(this.relativePath);
|
||||||
const newPath = CorePath.concatenatePaths(fileAndDir.directory, newName);
|
const newPath = CorePath.concatenatePaths(fileAndDir.directory, newName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -46,6 +46,7 @@ import { CorePlatform } from '@services/platform';
|
||||||
import { Chooser } from '@features/native/plugins';
|
import { Chooser } from '@features/native/plugins';
|
||||||
import { CoreToasts } from '@services/toasts';
|
import { CoreToasts } from '@services/toasts';
|
||||||
import { CoreLoadings } from '@services/loadings';
|
import { CoreLoadings } from '@services/loadings';
|
||||||
|
import { CoreFileUtils } from '@singletons/file-utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper service to upload files.
|
* Helper service to upload files.
|
||||||
|
@ -210,7 +211,7 @@ export class CoreFileUploaderHelperProvider {
|
||||||
options?: CoreFileUploaderOptions,
|
options?: CoreFileUploaderOptions,
|
||||||
): Promise<FileEntry> {
|
): Promise<FileEntry> {
|
||||||
|
|
||||||
const fileName = options?.fileName || CoreFile.getFileAndDirectoryFromPath(path).name;
|
const fileName = options?.fileName || CoreFileUtils.getFileAndDirectoryFromPath(path).name;
|
||||||
|
|
||||||
// Check that size isn't too large.
|
// Check that size isn't too large.
|
||||||
if (maxSize !== undefined && maxSize != -1) {
|
if (maxSize !== undefined && maxSize != -1) {
|
||||||
|
@ -283,7 +284,7 @@ export class CoreFileUploaderHelperProvider {
|
||||||
* @returns File name, undefined if cannot get it.
|
* @returns File name, undefined if cannot get it.
|
||||||
*/
|
*/
|
||||||
protected getChosenFileNameFromPath(result: ChooserResult): string | undefined {
|
protected getChosenFileNameFromPath(result: ChooserResult): string | undefined {
|
||||||
const nameAndDir = CoreFile.getFileAndDirectoryFromPath(result.uri);
|
const nameAndDir = CoreFileUtils.getFileAndDirectoryFromPath(result.uri);
|
||||||
|
|
||||||
if (!nameAndDir.name) {
|
if (!nameAndDir.name) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -243,7 +243,7 @@ export class CoreFileUploaderProvider {
|
||||||
deleteAfterUpload: !isFromAlbum,
|
deleteAfterUpload: !isFromAlbum,
|
||||||
mimeType: mimetype,
|
mimeType: mimetype,
|
||||||
};
|
};
|
||||||
const fileName = CoreFile.getFileAndDirectoryFromPath(uri).name;
|
const fileName = CoreFileUtils.getFileAndDirectoryFromPath(uri).name;
|
||||||
|
|
||||||
if (isIOS && (mimetype == 'image/jpeg' || mimetype == 'image/png')) {
|
if (isIOS && (mimetype == 'image/jpeg' || mimetype == 'image/png')) {
|
||||||
// In iOS, the pictures can have repeated names, even if they come from the album.
|
// 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) => {
|
originalFiles.forEach((file) => {
|
||||||
const stillInList = currentFiles.some((currentFile) =>
|
const stillInList = currentFiles.some((currentFile) =>
|
||||||
CoreFileHelper.getFileUrl(<CoreWSFile> currentFile) == CoreFileHelper.getFileUrl(file));
|
CoreFileHelper.getFileUrl(<CoreWSFile> currentFile) === CoreFileHelper.getFileUrl(file));
|
||||||
|
|
||||||
if (!stillInList) {
|
if (!stillInList) {
|
||||||
filesToDelete.push({
|
filesToDelete.push({
|
||||||
|
|
|
@ -29,6 +29,7 @@ import {
|
||||||
} from './core';
|
} from './core';
|
||||||
import { CONTENTS_LIBRARIES_TABLE_NAME, CONTENT_TABLE_NAME, CoreH5PLibraryCachedAssetsDBRecord } from '../services/database/h5p';
|
import { CONTENTS_LIBRARIES_TABLE_NAME, CONTENT_TABLE_NAME, CoreH5PLibraryCachedAssetsDBRecord } from '../services/database/h5p';
|
||||||
import { CoreH5PLibraryBeingSaved } from './storage';
|
import { CoreH5PLibraryBeingSaved } from './storage';
|
||||||
|
import { CoreFileUtils } from '@singletons/file-utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Equivalent to Moodle's implementation of H5PFileStorage.
|
* Equivalent to Moodle's implementation of H5PFileStorage.
|
||||||
|
@ -115,7 +116,7 @@ export class CoreH5PFileStorage {
|
||||||
}
|
}
|
||||||
|
|
||||||
treated[url] = url;
|
treated[url] = url;
|
||||||
const assetPathFolder = CoreFile.getFileAndDirectoryFromPath(assetPath).directory;
|
const assetPathFolder = CoreFileUtils.getFileAndDirectoryFromPath(assetPath).directory;
|
||||||
|
|
||||||
fileContent = fileContent.replace(
|
fileContent = fileContent.replace(
|
||||||
new RegExp(CoreText.escapeForRegex(match), 'g'),
|
new RegExp(CoreText.escapeForRegex(match), 'g'),
|
||||||
|
@ -300,7 +301,7 @@ export class CoreH5PFileStorage {
|
||||||
async getContentFolderNameByUrl(fileUrl: string, siteId: string): Promise<string> {
|
async getContentFolderNameByUrl(fileUrl: string, siteId: string): Promise<string> {
|
||||||
const path = await CoreFilepool.getFilePathByUrl(siteId, fileUrl);
|
const path = await CoreFilepool.getFilePathByUrl(siteId, fileUrl);
|
||||||
|
|
||||||
const fileAndDir = CoreFile.getFileAndDirectoryFromPath(path);
|
const fileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(path);
|
||||||
|
|
||||||
return CoreMimetypeUtils.removeExtension(fileAndDir.name);
|
return CoreMimetypeUtils.removeExtension(fileAndDir.name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ import { toBoolean } from '@/core/transforms/boolean';
|
||||||
import { Component, OnInit, Input } from '@angular/core';
|
import { Component, OnInit, Input } from '@angular/core';
|
||||||
import { FileEntry } from '@awesome-cordova-plugins/file/ngx';
|
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 { ModalController, Translate } from '@singletons';
|
||||||
import { CoreSharedFilesComponentsModule } from '../components.module';
|
import { CoreSharedFilesComponentsModule } from '../components.module';
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ export class CoreSharedFilesListModalComponent implements OnInit {
|
||||||
*/
|
*/
|
||||||
calculateTitle(path?: string): void {
|
calculateTitle(path?: string): void {
|
||||||
if (path) {
|
if (path) {
|
||||||
this.title = CoreFile.getFileAndDirectoryFromPath(path).name;
|
this.title = CoreFileUtils.getFileAndDirectoryFromPath(path).name;
|
||||||
} else {
|
} else {
|
||||||
this.title = Translate.instant('core.sharedfiles.sharedfiles');
|
this.title = Translate.instant('core.sharedfiles.sharedfiles');
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ import { CoreFile } from '@services/file';
|
||||||
import { CoreNavigator } from '@services/navigator';
|
import { CoreNavigator } from '@services/navigator';
|
||||||
import { CoreSiteBasicInfo } from '@services/sites';
|
import { CoreSiteBasicInfo } from '@services/sites';
|
||||||
import { CoreDomUtils } from '@services/utils/dom';
|
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.
|
* 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) {
|
if (this.filePath) {
|
||||||
const fileAndDir = CoreFile.getFileAndDirectoryFromPath(this.filePath);
|
const fileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(this.filePath);
|
||||||
this.fileName = fileAndDir.name;
|
this.fileName = fileAndDir.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
|
||||||
import { CoreFile } from '@services/file';
|
import { CoreFileUtils } from '@singletons/file-utils';
|
||||||
import { CoreNavigator } from '@services/navigator';
|
import { CoreNavigator } from '@services/navigator';
|
||||||
import { Translate } from '@singletons';
|
import { Translate } from '@singletons';
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ export class CoreSharedFilesListPage implements OnInit {
|
||||||
*/
|
*/
|
||||||
calculateTitle(path?: string): void {
|
calculateTitle(path?: string): void {
|
||||||
if (path) {
|
if (path) {
|
||||||
this.title = CoreFile.getFileAndDirectoryFromPath(path).name;
|
this.title = CoreFileUtils.getFileAndDirectoryFromPath(path).name;
|
||||||
} else {
|
} else {
|
||||||
this.title = Translate.instant('core.sharedfiles.sharedfiles');
|
this.title = Translate.instant('core.sharedfiles.sharedfiles');
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,15 +80,11 @@ export class CoreFileProvider {
|
||||||
|
|
||||||
static readonly CHUNK_SIZE = 1048576; // 1 MB. Same chunk size as Ionic Native.
|
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 initialized = false;
|
||||||
protected basePath = '';
|
protected basePath = '';
|
||||||
protected isHTMLAPI = false;
|
protected isHTMLAPI = false;
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.logger = CoreLogger.getInstance('CoreFileProvider');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets basePath to use with HTML API. Reserved for core use.
|
* Sets basePath to use with HTML API. Reserved for core use.
|
||||||
*
|
*
|
||||||
|
@ -438,11 +434,11 @@ export class CoreFileProvider {
|
||||||
*
|
*
|
||||||
* @param filename The file name.
|
* @param filename The file name.
|
||||||
* @returns The file name normalized.
|
* @returns The file name normalized.
|
||||||
|
*
|
||||||
|
* @deprecated since 5.0. Not used anymore.
|
||||||
*/
|
*/
|
||||||
normalizeFileName(filename: string): string {
|
normalizeFileName(filename: string): string {
|
||||||
filename = CoreUrl.decodeURIComponent(filename);
|
return CoreUrl.decodeURIComponent(filename);
|
||||||
|
|
||||||
return filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -820,7 +816,7 @@ export class CoreFileProvider {
|
||||||
from = this.removeBasePath(from);
|
from = this.removeBasePath(from);
|
||||||
to = this.removeBasePath(to);
|
to = this.removeBasePath(to);
|
||||||
|
|
||||||
const toFileAndDir = this.getFileAndDirectoryFromPath(to);
|
const toFileAndDir = CoreFileUtils.getFileAndDirectoryFromPath(to);
|
||||||
|
|
||||||
if (toFileAndDir.directory && !destDirExists) {
|
if (toFileAndDir.directory && !destDirExists) {
|
||||||
// Create the target directory if it doesn't exist.
|
// Create the target directory if it doesn't exist.
|
||||||
|
@ -851,23 +847,10 @@ export class CoreFileProvider {
|
||||||
*
|
*
|
||||||
* @param path Path to be extracted.
|
* @param path Path to be extracted.
|
||||||
* @returns Plain object containing the file name and directory.
|
* @returns Plain object containing the file name and directory.
|
||||||
* @description
|
* @deprecated since 5.0. Use CoreFileUtils.getFileAndDirectoryFromPath instead.
|
||||||
* 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'
|
|
||||||
*/
|
*/
|
||||||
getFileAndDirectoryFromPath(path: string): {directory: string; name: string} {
|
getFileAndDirectoryFromPath(path: string): {directory: string; name: string} {
|
||||||
const file = {
|
return CoreFileUtils.getFileAndDirectoryFromPath(path);
|
||||||
directory: '',
|
|
||||||
name: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
file.directory = path.substring(0, path.lastIndexOf('/'));
|
|
||||||
file.name = path.substring(path.lastIndexOf('/') + 1);
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1028,7 +1011,7 @@ export class CoreFileProvider {
|
||||||
const fileEntry = await this.getExternalFile(from);
|
const fileEntry = await this.getExternalFile(from);
|
||||||
|
|
||||||
// Create the destination dir if it doesn't exist.
|
// 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);
|
const dirEntry = await this.createDir(dirAndFile.directory);
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@ import { CoreSite } from '@classes/sites/site';
|
||||||
import { CoreNative } from '@features/native/services/native';
|
import { CoreNative } from '@features/native/services/native';
|
||||||
import { CoreLoadings } from '@services/loadings';
|
import { CoreLoadings } from '@services/loadings';
|
||||||
import { CorePromiseUtils } from '@singletons/promise-utils';
|
import { CorePromiseUtils } from '@singletons/promise-utils';
|
||||||
|
import { CoreFileUtils } from '@singletons/file-utils';
|
||||||
|
|
||||||
type CoreFrameElement = FrameElement & {
|
type CoreFrameElement = FrameElement & {
|
||||||
window?: Window;
|
window?: Window;
|
||||||
|
@ -431,7 +432,7 @@ export class CoreIframeUtilsProvider {
|
||||||
? ('src' in element ? element.src : element.data)
|
? ('src' in element ? element.src : element.data)
|
||||||
: null;
|
: null;
|
||||||
if (src) {
|
if (src) {
|
||||||
const dirAndFile = CoreFile.getFileAndDirectoryFromPath(src);
|
const dirAndFile = CoreFileUtils.getFileAndDirectoryFromPath(src);
|
||||||
if (dirAndFile.directory) {
|
if (dirAndFile.directory) {
|
||||||
url = CorePath.concatenatePaths(dirAndFile.directory, url);
|
url = CorePath.concatenatePaths(dirAndFile.directory, url);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -64,7 +64,7 @@ export class CoreFileUtils {
|
||||||
// Check if there are 2 files with the same name.
|
// Check if there are 2 files with the same name.
|
||||||
for (let i = 0; i < files.length; i++) {
|
for (let i = 0; i < files.length; i++) {
|
||||||
const file = files[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) {
|
if (names.indexOf(name) > -1) {
|
||||||
return Translate.instant('core.filenameexist', { $a: name });
|
return Translate.instant('core.filenameexist', { $a: name });
|
||||||
|
@ -76,4 +76,28 @@ export class CoreFileUtils {
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue