MOBILE-3432 core: Move isOpenableInApp check to more reusable functions

main
Dani Palou 2020-09-21 10:41:43 +02:00
parent 9dce6d408b
commit b046d57f2b
4 changed files with 91 additions and 103 deletions

View File

@ -183,19 +183,7 @@ export class AddonModResourceHelperProvider {
* @param courseId Course Id, used for completion purposes. * @param courseId Course Id, used for completion purposes.
* @return Resolved when done. * @return Resolved when done.
*/ */
async openModuleFile(module: any, courseId: number): Promise<any> { openModuleFile(module: any, courseId: number): Promise<any> {
// 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;
}
}
const modal = this.domUtils.showModalLoading(); const modal = this.domUtils.showModalLoading();
// Download and open the file from the resource contents. // Download and open the file from the resource contents.

View File

@ -156,13 +156,6 @@ export class CoreFileComponent implements OnInit, OnDestroy {
return; 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) { if (!this.canDownload || !this.state || this.state == CoreConstants.NOT_DOWNLOADABLE) {
// File cannot be downloaded, just open it. // File cannot be downloaded, just open it.
if (this.file.toURL) { if (this.file.toURL) {
@ -188,32 +181,45 @@ export class CoreFileComponent implements OnInit, OnDestroy {
if (openAfterDownload) { if (openAfterDownload) {
// File needs to be opened now. // File needs to be opened now.
this.openFile().catch((error) => { try {
await this.openFile();
} catch (error) {
this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true); this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true);
}); }
} else { } else {
// File doesn't need to be opened (it's a prefetch). Show confirm modal if file size is defined and it's big. // File doesn't need to be opened (it's a prefetch).
this.pluginFileDelegate.getFileSize({fileurl: this.fileUrl, filesize: this.fileSize}, this.siteId).then((size) => { 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(() => { if (size) {
// User confirmed, add the file to queue. await this.domUtils.confirmDownloadSize({ size: size, total: true });
return this.filepoolProvider.invalidateFileByUrl(this.siteId, this.fileUrl).finally(() => { }
this.isDownloading = true;
this.filepoolProvider.addToQueueByUrl(this.siteId, this.fileUrl, this.component, // User confirmed, add the file to queue.
this.componentId, this.timemodified, undefined, undefined, 0, this.file).catch((error) => { await this.utils.ignoreErrors(this.filepoolProvider.invalidateFileByUrl(this.siteId, this.fileUrl));
this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true);
this.calculateState(); this.isDownloading = true;
});
}); try {
}).catch(() => { await this.filepoolProvider.addToQueueByUrl(this.siteId, this.fileUrl, this.component,
// User cancelled. this.componentId, this.timemodified, undefined, undefined, 0, this.file);
}); } catch (error) {
}).catch((error) => { this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true);
this.calculateState();
}
} catch (error) {
this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true); this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true);
}); }
} }
} }

View File

@ -585,6 +585,10 @@ export class CoreCourseHelperProvider {
return Promise.reject(this.utils.createFakeWSError('core.filenotfound', true)); 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); return this.sitesProvider.getSite(siteId);
}).then((site) => { }).then((site) => {
const mainFile = files[0], const mainFile = files[0],

View File

@ -51,63 +51,58 @@ export class CoreFileHelperProvider {
* @param siteId The site ID. If not defined, current site. * @param siteId The site ID. If not defined, current site.
* @return Resolved on success. * @return Resolved on success.
*/ */
downloadAndOpenFile(file: any, component: string, componentId: string | number, state?: string, async downloadAndOpenFile(file: any, component: string, componentId: string | number, state?: string,
onProgress?: (event: any) => any, siteId?: string): Promise<any> { onProgress?: (event: any) => any, siteId?: string): Promise<void> {
siteId = siteId || this.sitesProvider.getCurrentSiteId(); siteId = siteId || this.sitesProvider.getCurrentSiteId();
const fileUrl = this.getFileUrl(file), const fileUrl = this.getFileUrl(file);
timemodified = this.getFileTimemodified(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; 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)) { return this.utils.openFile(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);
}
});
} }
/** /**
@ -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. * @param onlyDownload Whether the user is only downloading the file, not opening it.
* @return bool. * @return Promise resolved if confirmed, rejected otherwise.
*/ */
async showConfirmOpenUnsupportedFile(): Promise<boolean> { showConfirmOpenUnsupportedFile(onlyDownload?: boolean): Promise<void> {
try { const message = this.translate.instant('core.cannotopeninapp' + (onlyDownload ? 'download' : ''));
await this.domUtils.showConfirm(this.translate.instant('core.cannotopeninapp'), undefined, const okButton = this.translate.instant(onlyDownload ? 'core.downloadfile' : 'core.openfile');
this.translate.instant('core.openfile'));
return true; return this.domUtils.showConfirm(message, undefined, okButton, undefined, { cssClass: 'core-modal-force-on-top' });
}
catch (e) {
return false;
}
} }
/** /**