diff --git a/src/addon/mod/resource/providers/helper.ts b/src/addon/mod/resource/providers/helper.ts index 8f43d77ee..62a00e511 100644 --- a/src/addon/mod/resource/providers/helper.ts +++ b/src/addon/mod/resource/providers/helper.ts @@ -183,19 +183,7 @@ export class AddonModResourceHelperProvider { * @param courseId Course Id, used for completion purposes. * @return Resolved when done. */ - async openModuleFile(module: any, courseId: number): Promise { - // Check whether the file type excluded to open in app. - if (!module.contents.length) { - await this.courseProvider.loadModuleContents(module, courseId); - } - - if (!this.fileHelper.isOpenableInApp(module.contents[0])) { - const confirmed = await this.fileHelper.showConfirmOpenUnsupportedFile(); - if (!confirmed) { - return; - } - } - + openModuleFile(module: any, courseId: number): Promise { const modal = this.domUtils.showModalLoading(); // Download and open the file from the resource contents. diff --git a/src/components/file/file.ts b/src/components/file/file.ts index d920b54f9..34c89be6a 100644 --- a/src/components/file/file.ts +++ b/src/components/file/file.ts @@ -156,13 +156,6 @@ export class CoreFileComponent implements OnInit, OnDestroy { return; } - if (!this.fileHelper.isOpenableInApp(this.file)) { - const confirmed = await this.fileHelper.showConfirmOpenUnsupportedFile(); - if (!confirmed) { - return; - } - } - if (!this.canDownload || !this.state || this.state == CoreConstants.NOT_DOWNLOADABLE) { // File cannot be downloaded, just open it. if (this.file.toURL) { @@ -188,32 +181,45 @@ export class CoreFileComponent implements OnInit, OnDestroy { if (openAfterDownload) { // File needs to be opened now. - this.openFile().catch((error) => { + try { + await this.openFile(); + } catch (error) { this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true); - }); + } } else { - // File doesn't need to be opened (it's a prefetch). Show confirm modal if file size is defined and it's big. - this.pluginFileDelegate.getFileSize({fileurl: this.fileUrl, filesize: this.fileSize}, this.siteId).then((size) => { + // File doesn't need to be opened (it's a prefetch). + if (!this.fileHelper.isOpenableInApp(this.file)) { + try { + await this.fileHelper.showConfirmOpenUnsupportedFile(true); + } catch (error) { + return; // Cancelled, stop. + } + } - const promise = size ? this.domUtils.confirmDownloadSize({ size: size, total: true }) : Promise.resolve(); + try { + // Show confirm modal if file size is defined and it's big. + const size = await this.pluginFileDelegate.getFileSize({fileurl: this.fileUrl, filesize: this.fileSize}, + this.siteId); - return promise.then(() => { - // User confirmed, add the file to queue. - return this.filepoolProvider.invalidateFileByUrl(this.siteId, this.fileUrl).finally(() => { - this.isDownloading = true; + if (size) { + await this.domUtils.confirmDownloadSize({ size: size, total: true }); + } - this.filepoolProvider.addToQueueByUrl(this.siteId, this.fileUrl, this.component, - this.componentId, this.timemodified, undefined, undefined, 0, this.file).catch((error) => { - this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true); - this.calculateState(); - }); - }); - }).catch(() => { - // User cancelled. - }); - }).catch((error) => { + // User confirmed, add the file to queue. + await this.utils.ignoreErrors(this.filepoolProvider.invalidateFileByUrl(this.siteId, this.fileUrl)); + + this.isDownloading = true; + + try { + await this.filepoolProvider.addToQueueByUrl(this.siteId, this.fileUrl, this.component, + this.componentId, this.timemodified, undefined, undefined, 0, this.file); + } catch (error) { + this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true); + this.calculateState(); + } + } catch (error) { this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true); - }); + } } } diff --git a/src/core/course/providers/helper.ts b/src/core/course/providers/helper.ts index ec7939c3d..99e181b96 100644 --- a/src/core/course/providers/helper.ts +++ b/src/core/course/providers/helper.ts @@ -585,6 +585,10 @@ export class CoreCourseHelperProvider { return Promise.reject(this.utils.createFakeWSError('core.filenotfound', true)); } + if (!this.fileHelper.isOpenableInApp(module.contents[0])) { + return this.fileHelper.showConfirmOpenUnsupportedFile(); + } + }).then(() => { return this.sitesProvider.getSite(siteId); }).then((site) => { const mainFile = files[0], diff --git a/src/providers/file-helper.ts b/src/providers/file-helper.ts index 11e0e6345..c2c082038 100644 --- a/src/providers/file-helper.ts +++ b/src/providers/file-helper.ts @@ -51,63 +51,58 @@ export class CoreFileHelperProvider { * @param siteId The site ID. If not defined, current site. * @return Resolved on success. */ - downloadAndOpenFile(file: any, component: string, componentId: string | number, state?: string, - onProgress?: (event: any) => any, siteId?: string): Promise { + async downloadAndOpenFile(file: any, component: string, componentId: string | number, state?: string, + onProgress?: (event: any) => any, siteId?: string): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); - const fileUrl = this.getFileUrl(file), - timemodified = this.getFileTimemodified(file); + const fileUrl = this.getFileUrl(file); + 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 this.utils.openOnlineFile(url); - return this.downloadFileIfNeeded(file, fileUrl, component, componentId, timemodified, state, onProgress, siteId) - .then((url) => { - if (!url) { return; + } catch (error) { + // Error opening the file, some apps don't allow opening online files. + if (!this.fileProvider.isAvailable()) { + throw error; + } + + // Get the state. + if (!state) { + state = await this.filepoolProvider.getFileStateByUrl(siteId, fileUrl, timemodified); + } + + if (state == CoreConstants.DOWNLOADING) { + throw new Error(this.translate.instant('core.erroropenfiledownloading')); + } + + 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 this.filepoolProvider.getInternalUrlByUrl(siteId, fileUrl); + } } + } - 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'; - - return this.utils.openOnlineFile(url).catch((error) => { - // Error opening the file, some apps don't allow opening online files. - if (!this.fileProvider.isAvailable()) { - return Promise.reject(error); - } - - let promise; - - // Get the state. - if (state) { - promise = Promise.resolve(state); - } else { - promise = this.filepoolProvider.getFileStateByUrl(siteId, fileUrl, timemodified); - } - - return promise.then((state) => { - if (state == CoreConstants.DOWNLOADING) { - return Promise.reject(this.translate.instant('core.erroropenfiledownloading')); - } - - let promise; - - if (state === CoreConstants.NOT_DOWNLOADED) { - // File is not downloaded, download and then return the local URL. - promise = this.downloadFile(fileUrl, component, componentId, timemodified, onProgress, file, siteId); - } else { - // File is outdated and can't be opened in online, return the local URL. - promise = this.filepoolProvider.getInternalUrlByUrl(siteId, fileUrl); - } - - return promise.then((url) => { - return this.utils.openFile(url); - }); - }); - }); - } else { - return this.utils.openFile(url); - } - }); + return this.utils.openFile(url); } /** @@ -357,21 +352,16 @@ export class CoreFileHelperProvider { } /** - * Is the file openable in app. + * Show a confirm asking the user if we wants to open the file. * - * @param file The file to check. - * @return bool. + * @param onlyDownload Whether the user is only downloading the file, not opening it. + * @return Promise resolved if confirmed, rejected otherwise. */ - async showConfirmOpenUnsupportedFile(): Promise { - try { - await this.domUtils.showConfirm(this.translate.instant('core.cannotopeninapp'), undefined, - this.translate.instant('core.openfile')); + showConfirmOpenUnsupportedFile(onlyDownload?: boolean): Promise { + const message = this.translate.instant('core.cannotopeninapp' + (onlyDownload ? 'download' : '')); + const okButton = this.translate.instant(onlyDownload ? 'core.downloadfile' : 'core.openfile'); - return true; - } - catch (e) { - return false; - } + return this.domUtils.showConfirm(message, undefined, okButton, undefined, { cssClass: 'core-modal-force-on-top' }); } /**