2020-10-07 08:53:19 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
2020-10-14 06:30:41 +00:00
|
|
|
import { FileEntry } from '@ionic-native/file';
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
import { CoreApp } from '@services/app';
|
|
|
|
import { CoreFile } from '@services/file';
|
|
|
|
import { CoreFilepool } from '@services/filepool';
|
|
|
|
import { CoreSites } from '@services/sites';
|
2020-10-14 06:30:41 +00:00
|
|
|
import { CoreWS, CoreWSExternalFile } from '@services/ws';
|
2020-10-07 08:53:19 +00:00
|
|
|
import { CoreDomUtils } from '@services/utils/dom';
|
|
|
|
import { CoreUrlUtils } from '@services/utils/url';
|
|
|
|
import { CoreUtils } from '@services/utils/utils';
|
|
|
|
import { CoreConstants } from '@core/constants';
|
2020-10-14 06:30:41 +00:00
|
|
|
import { CoreError } from '@classes/errors/error';
|
2020-10-07 08:53:19 +00:00
|
|
|
import { makeSingleton, Translate } from '@singletons/core.singletons';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provider to provide some helper functions regarding files and packages.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreFileHelperProvider {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to open a file, downloading it if needed.
|
|
|
|
*
|
|
|
|
* @param file The file to download.
|
|
|
|
* @param component The component to link the file to.
|
|
|
|
* @param componentId An ID to use in conjunction with the component.
|
|
|
|
* @param state The file's state. If not provided, it will be calculated.
|
|
|
|
* @param onProgress Function to call on progress.
|
|
|
|
* @param siteId The site ID. If not defined, current site.
|
|
|
|
* @return Resolved on success.
|
|
|
|
*/
|
2020-10-21 14:32:27 +00:00
|
|
|
async downloadAndOpenFile(
|
|
|
|
file: CoreWSExternalFile,
|
|
|
|
component: string,
|
|
|
|
componentId: string | number,
|
|
|
|
state?: string,
|
|
|
|
onProgress?: CoreFileHelperOnProgress,
|
|
|
|
siteId?: string,
|
|
|
|
): Promise<void> {
|
2020-10-07 08:53:19 +00:00
|
|
|
siteId = siteId || CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
2020-10-21 14:32:27 +00:00
|
|
|
const fileUrl = file.fileurl;
|
2020-10-07 08:53:19 +00:00
|
|
|
const timemodified = this.getFileTimemodified(file);
|
|
|
|
|
|
|
|
if (!this.isOpenableInApp(file)) {
|
|
|
|
await this.showConfirmOpenUnsupportedFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
let url = await this.downloadFileIfNeeded(file, fileUrl, component, componentId, timemodified, state, onProgress, siteId);
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CoreUrlUtils.instance.isLocalFileUrl(url)) {
|
|
|
|
/* In iOS, if we use the same URL in embedded browser and background download then the download only
|
|
|
|
downloads a few bytes (cached ones). Add a hash to the URL so both URLs are different. */
|
|
|
|
url = url + '#moodlemobile-embedded';
|
|
|
|
|
|
|
|
try {
|
|
|
|
await CoreUtils.instance.openOnlineFile(url);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
// Error opening the file, some apps don't allow opening online files.
|
|
|
|
if (!CoreFile.instance.isAvailable()) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the state.
|
|
|
|
if (!state) {
|
|
|
|
state = await CoreFilepool.instance.getFileStateByUrl(siteId, fileUrl, timemodified);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == CoreConstants.DOWNLOADING) {
|
2020-10-14 06:30:41 +00:00
|
|
|
throw new CoreError(Translate.instance.instant('core.erroropenfiledownloading'));
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state === CoreConstants.NOT_DOWNLOADED) {
|
|
|
|
// File is not downloaded, download and then return the local URL.
|
|
|
|
url = await this.downloadFile(fileUrl, component, componentId, timemodified, onProgress, file, siteId);
|
|
|
|
} else {
|
|
|
|
// File is outdated and can't be opened in online, return the local URL.
|
|
|
|
url = await CoreFilepool.instance.getInternalUrlByUrl(siteId, fileUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CoreUtils.instance.openFile(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a file if it needs to be downloaded.
|
|
|
|
*
|
|
|
|
* @param file The file to download.
|
|
|
|
* @param fileUrl The file URL.
|
|
|
|
* @param component The component to link the file to.
|
|
|
|
* @param componentId An ID to use in conjunction with the component.
|
|
|
|
* @param timemodified The time this file was modified.
|
|
|
|
* @param state The file's state. If not provided, it will be calculated.
|
|
|
|
* @param onProgress Function to call on progress.
|
|
|
|
* @param siteId The site ID. If not defined, current site.
|
|
|
|
* @return Resolved with the URL to use on success.
|
|
|
|
*/
|
2020-10-21 14:32:27 +00:00
|
|
|
protected async downloadFileIfNeeded(
|
|
|
|
file: CoreWSExternalFile,
|
|
|
|
fileUrl: string,
|
|
|
|
component?: string,
|
|
|
|
componentId?: string | number,
|
|
|
|
timemodified?: number,
|
|
|
|
state?: string,
|
|
|
|
onProgress?: CoreFileHelperOnProgress,
|
|
|
|
siteId?: string,
|
|
|
|
): Promise<string> {
|
2020-10-07 08:53:19 +00:00
|
|
|
siteId = siteId || CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
2020-10-21 14:32:27 +00:00
|
|
|
const site = await CoreSites.instance.getSite(siteId);
|
|
|
|
const fixedUrl = await site.checkAndFixPluginfileURL(fileUrl);
|
|
|
|
|
|
|
|
if (!CoreFile.instance.isAvailable()) {
|
|
|
|
// Use the online URL.
|
|
|
|
return fixedUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
// Calculate the state.
|
|
|
|
state = await CoreFilepool.instance.getFileStateByUrl(siteId, fileUrl, timemodified);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file system is available.
|
|
|
|
const isWifi = CoreApp.instance.isWifi();
|
|
|
|
const isOnline = CoreApp.instance.isOnline();
|
|
|
|
|
|
|
|
if (state == CoreConstants.DOWNLOADED) {
|
|
|
|
// File is downloaded, get the local file URL.
|
|
|
|
return CoreFilepool.instance.getUrlByUrl(siteId, fileUrl, component, componentId, timemodified, false, false, file);
|
|
|
|
} else {
|
|
|
|
if (!isOnline && !this.isStateDownloaded(state)) {
|
|
|
|
// Not downloaded and user is offline, reject.
|
|
|
|
throw new CoreError(Translate.instance.instant('core.networkerrormsg'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onProgress) {
|
|
|
|
// This call can take a while. Send a fake event to notify that we're doing some calculations.
|
|
|
|
onProgress({ calculating: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await CoreFilepool.instance.shouldDownloadBeforeOpen(fixedUrl, file.filesize || 0);
|
|
|
|
} catch (error) {
|
|
|
|
// Start the download if in wifi, but return the URL right away so the file is opened.
|
|
|
|
if (isWifi) {
|
|
|
|
this.downloadFile(fileUrl, component, componentId, timemodified, onProgress, file, siteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isStateDownloaded(state) || isOnline) {
|
|
|
|
// Not downloaded or online, return the online URL.
|
|
|
|
return fixedUrl;
|
2020-10-07 08:53:19 +00:00
|
|
|
} else {
|
2020-10-21 14:32:27 +00:00
|
|
|
// Outdated but offline, so we return the local URL.
|
|
|
|
return CoreFilepool.instance.getUrlByUrl(
|
|
|
|
siteId, fileUrl, component, componentId, timemodified, false, false, file);
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
2020-10-21 14:32:27 +00:00
|
|
|
}
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-10-21 14:32:27 +00:00
|
|
|
// Download the file first.
|
|
|
|
if (state == CoreConstants.DOWNLOADING) {
|
|
|
|
// It's already downloading, stop.
|
2020-10-07 08:53:19 +00:00
|
|
|
return fixedUrl;
|
|
|
|
}
|
2020-10-21 14:32:27 +00:00
|
|
|
|
|
|
|
// Download and then return the local URL.
|
|
|
|
return this.downloadFile(fileUrl, component, componentId, timemodified, onProgress, file, siteId);
|
|
|
|
}
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download the file.
|
|
|
|
*
|
|
|
|
* @param fileUrl The file URL.
|
|
|
|
* @param component The component to link the file to.
|
|
|
|
* @param componentId An ID to use in conjunction with the component.
|
|
|
|
* @param timemodified The time this file was modified.
|
|
|
|
* @param onProgress Function to call on progress.
|
|
|
|
* @param file The file to download.
|
|
|
|
* @param siteId The site ID. If not defined, current site.
|
|
|
|
* @return Resolved with internal URL on success, rejected otherwise.
|
|
|
|
*/
|
2020-10-21 14:32:27 +00:00
|
|
|
async downloadFile(
|
|
|
|
fileUrl: string,
|
|
|
|
component?: string,
|
|
|
|
componentId?: string | number,
|
|
|
|
timemodified?: number,
|
|
|
|
onProgress?: (event: ProgressEvent) => void,
|
|
|
|
file?: CoreWSExternalFile,
|
|
|
|
siteId?: string,
|
|
|
|
): Promise<string> {
|
2020-10-07 08:53:19 +00:00
|
|
|
siteId = siteId || CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
|
|
|
// Get the site and check if it can download files.
|
2020-10-21 14:32:27 +00:00
|
|
|
const site = await CoreSites.instance.getSite(siteId);
|
|
|
|
|
|
|
|
if (!site.canDownloadFiles()) {
|
|
|
|
throw new CoreError(Translate.instance.instant('core.cannotdownloadfiles'));
|
|
|
|
}
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-10-21 14:32:27 +00:00
|
|
|
try {
|
|
|
|
return await CoreFilepool.instance.downloadUrl(siteId, fileUrl, false, component, componentId, timemodified,
|
|
|
|
onProgress, undefined, file);
|
|
|
|
} catch (error) {
|
|
|
|
// Download failed, check the state again to see if the file was downloaded before.
|
|
|
|
const state = await CoreFilepool.instance.getFileStateByUrl(siteId, fileUrl, timemodified);
|
|
|
|
|
|
|
|
if (this.isStateDownloaded(state)) {
|
|
|
|
return CoreFilepool.instance.getInternalUrlByUrl(siteId, fileUrl);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file's URL.
|
|
|
|
*
|
|
|
|
* @param file The file.
|
2020-10-14 06:30:41 +00:00
|
|
|
* @deprecated since 3.9.5. Get directly the fileurl instead.
|
2020-10-07 08:53:19 +00:00
|
|
|
*/
|
2020-10-21 14:32:27 +00:00
|
|
|
getFileUrl(file: CoreWSExternalFile): string | undefined {
|
2020-10-14 06:30:41 +00:00
|
|
|
return file.fileurl;
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file's timemodified.
|
|
|
|
*
|
|
|
|
* @param file The file.
|
|
|
|
*/
|
2020-10-14 06:30:41 +00:00
|
|
|
getFileTimemodified(file: CoreWSExternalFile): number {
|
2020-10-07 08:53:19 +00:00
|
|
|
return file.timemodified || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a state is downloaded or outdated.
|
|
|
|
*
|
|
|
|
* @param state The state to check.
|
|
|
|
*/
|
|
|
|
isStateDownloaded(state: string): boolean {
|
|
|
|
return state === CoreConstants.DOWNLOADED || state === CoreConstants.OUTDATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the file has to be opened in browser (external repository).
|
|
|
|
* The file must have a mimetype attribute.
|
|
|
|
*
|
|
|
|
* @param file The file to check.
|
|
|
|
* @return Whether the file should be opened in browser.
|
|
|
|
*/
|
2020-10-14 06:30:41 +00:00
|
|
|
shouldOpenInBrowser(file: CoreWSExternalFile): boolean {
|
2020-10-07 08:53:19 +00:00
|
|
|
if (!file || !file.isexternalfile || !file.mimetype) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mimetype = file.mimetype;
|
|
|
|
if (mimetype.indexOf('application/vnd.google-apps.') != -1) {
|
|
|
|
// Google Docs file, always open in browser.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.repositorytype == 'onedrive') {
|
|
|
|
// In OneDrive, open in browser the office docs
|
|
|
|
return mimetype.indexOf('application/vnd.openxmlformats-officedocument') != -1 ||
|
|
|
|
mimetype == 'text/plain' || mimetype == 'document/unknown';
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the total size of the given files.
|
|
|
|
*
|
|
|
|
* @param files The files to check.
|
|
|
|
* @return Total files size.
|
|
|
|
*/
|
2020-10-14 06:30:41 +00:00
|
|
|
async getTotalFilesSize(files: (CoreWSExternalFile | FileEntry)[]): Promise<number> {
|
2020-10-07 08:53:19 +00:00
|
|
|
let totalSize = 0;
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
totalSize += await this.getFileSize(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the file size.
|
|
|
|
*
|
|
|
|
* @param file The file to check.
|
|
|
|
* @return File size.
|
|
|
|
*/
|
2020-10-14 06:30:41 +00:00
|
|
|
async getFileSize(file: CoreWSExternalFile | FileEntry): Promise<number> {
|
|
|
|
if ('filesize' in file && (file.filesize || file.filesize === 0)) {
|
2020-10-07 08:53:19 +00:00
|
|
|
return file.filesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a remote file. First check if we have the file downloaded since it's more reliable.
|
2020-10-14 06:30:41 +00:00
|
|
|
if ('filename' in file) {
|
|
|
|
const fileUrl = file.fileurl;
|
|
|
|
|
2020-10-07 08:53:19 +00:00
|
|
|
try {
|
|
|
|
const siteId = CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
2020-10-14 06:30:41 +00:00
|
|
|
const path = await CoreFilepool.instance.getFilePathByUrl(siteId, fileUrl);
|
2020-10-07 08:53:19 +00:00
|
|
|
const fileEntry = await CoreFile.instance.getFile(path);
|
|
|
|
const fileObject = await CoreFile.instance.getFileObjectFromFileEntry(fileEntry);
|
|
|
|
|
|
|
|
return fileObject.size;
|
|
|
|
} catch (error) {
|
|
|
|
// Error getting the file, maybe it's not downloaded. Get remote size.
|
2020-10-14 06:30:41 +00:00
|
|
|
const size = await CoreWS.instance.getRemoteFileSize(fileUrl);
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
if (size === -1) {
|
2020-10-14 06:30:41 +00:00
|
|
|
throw new CoreError(`Couldn't determine file size: ${fileUrl}`);
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a local file, get its size.
|
2020-10-14 06:30:41 +00:00
|
|
|
if ('name' in file) {
|
2020-10-07 08:53:19 +00:00
|
|
|
const fileObject = await CoreFile.instance.getFileObjectFromFileEntry(file);
|
|
|
|
|
|
|
|
return fileObject.size;
|
|
|
|
}
|
|
|
|
|
2020-10-14 06:30:41 +00:00
|
|
|
throw new CoreError('Couldn\'t determine file size');
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the file openable in app.
|
|
|
|
*
|
|
|
|
* @param file The file to check.
|
|
|
|
* @return bool.
|
|
|
|
*/
|
2020-10-14 06:30:41 +00:00
|
|
|
isOpenableInApp(file: {filename?: string; name?: string}): boolean {
|
2020-10-21 14:32:27 +00:00
|
|
|
const regex = /(?:\.([^.]+))?$/;
|
|
|
|
const regexResult = regex.exec(file.filename || file.name || '');
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-10-21 14:32:27 +00:00
|
|
|
if (!regexResult || !regexResult[1]) {
|
|
|
|
// Couldn't find the extension. Assume it's openable.
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-10-21 14:32:27 +00:00
|
|
|
return !this.isFileTypeExcludedInApp(regexResult[1]);
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a confirm asking the user if we wants to open the file.
|
|
|
|
*
|
|
|
|
* @param onlyDownload Whether the user is only downloading the file, not opening it.
|
|
|
|
* @return Promise resolved if confirmed, rejected otherwise.
|
|
|
|
*/
|
|
|
|
showConfirmOpenUnsupportedFile(onlyDownload?: boolean): Promise<void> {
|
|
|
|
const message = Translate.instance.instant('core.cannotopeninapp' + (onlyDownload ? 'download' : ''));
|
|
|
|
const okButton = Translate.instance.instant(onlyDownload ? 'core.downloadfile' : 'core.openfile');
|
|
|
|
|
|
|
|
return CoreDomUtils.instance.showConfirm(message, undefined, okButton, undefined, { cssClass: 'core-modal-force-on-top' });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the file type excluded to open in app.
|
|
|
|
*
|
|
|
|
* @param file The file to check.
|
|
|
|
* @return bool.
|
|
|
|
*/
|
|
|
|
isFileTypeExcludedInApp(fileType: string): boolean {
|
|
|
|
const currentSite = CoreSites.instance.getCurrentSite();
|
2020-10-21 14:32:27 +00:00
|
|
|
const fileTypeExcludeList = currentSite?.getStoredConfig('tool_mobile_filetypeexclusionlist');
|
2020-10-07 08:53:19 +00:00
|
|
|
|
|
|
|
if (!fileTypeExcludeList) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const regEx = new RegExp('(,|^)' + fileType + '(,|$)', 'g');
|
|
|
|
|
|
|
|
return !!fileTypeExcludeList.match(regEx);
|
|
|
|
}
|
2020-10-14 06:30:41 +00:00
|
|
|
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CoreFileHelper extends makeSingleton(CoreFileHelperProvider) {}
|
2020-10-14 06:30:41 +00:00
|
|
|
|
|
|
|
export type CoreFileHelperOnProgress = (event?: ProgressEvent | { calculating: true }) => void;
|
|
|
|
|