MOBILE-3897 link: Wait for download to finish before open file

main
Dani Palou 2021-12-23 12:27:57 +01:00
parent 43ce384da8
commit e91e5d1cce
5 changed files with 34 additions and 9 deletions

View File

@ -364,7 +364,7 @@ export class CoreExternalContentDirective implements AfterViewInit, OnChanges, O
finalUrl = CoreFile.convertFileSrc(finalUrl);
}
if (!CoreUrlUtils.isLocalFileUrl(finalUrl) && !finalUrl.includes('#')) {
if (!CoreUrlUtils.isLocalFileUrl(finalUrl) && !finalUrl.includes('#') && tagName !== 'A') {
/* In iOS, if we use the same URL in embedded file and background download then the download only
downloads a few bytes (cached ones). Add an anchor to the URL so both URLs are different.
Don't add this anchor if the URL already has an anchor, otherwise other anchors might not work.

View File

@ -26,6 +26,7 @@ import { CoreConstants } from '@/core/constants';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreCustomURLSchemes } from '@services/urlschemes';
import { DomSanitizer } from '@singletons';
import { CoreFilepool } from '@services/filepool';
/**
* Directive to open a link in external browser or in the app.
@ -218,6 +219,26 @@ export class CoreLinkDirective implements OnInit {
}
}
if (currentSite.isSitePluginFileUrl(href)) {
// It's a site file. Check if it's being downloaded right now.
const isDownloading = await CoreFilepool.isFileDownloadingByUrl(currentSite.getId(), href);
if (isDownloading) {
// Wait for the download to finish before opening the file to prevent downloading it twice.
const modal = await CoreDomUtils.showModalLoading();
try {
const path = await CoreFilepool.downloadUrl(currentSite.getId(), href);
return this.openLocalFile(path);
} catch {
// Error downloading, just open the original URL.
} finally {
modal.dismiss();
}
}
}
if (this.autoLogin == 'yes') {
if (this.inApp) {
await currentSite.openInAppWithAutoLogin(href);

View File

@ -457,13 +457,10 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate<CoreCo
size += fileSize;
} catch {
// Error getting size. Check if the file is being downloaded.
try {
await CoreFilepool.isFileDownloadingByUrl(siteId, CoreFileHelper.getFileUrl(file));
const isDownloading = await CoreFilepool.isFileDownloadingByUrl(siteId, CoreFileHelper.getFileUrl(file));
if (isDownloading) {
// If downloading, count as downloaded.
size += file.filesize || 0;
} catch {
// Not downloading and not found in disk, don't add any size
}
}
}));

View File

@ -2280,13 +2280,19 @@ export class CoreFilepoolProvider {
*
* @param siteId The site ID.
* @param fileUrl File URL.
* @param Promise resolved if file is downloading, rejected otherwise.
* @param Promise resolved with boolean: whether the file is downloading.
*/
async isFileDownloadingByUrl(siteId: string, fileUrl: string): Promise<void> {
async isFileDownloadingByUrl(siteId: string, fileUrl: string): Promise<boolean> {
const file = await this.fixPluginfileURL(siteId, fileUrl);
const fileId = this.getFileIdByUrl(CoreFileHelper.getFileUrl(file));
try {
await this.hasFileInQueue(siteId, fileId);
return true;
} catch {
return false;
}
}
/**

View File

@ -8,6 +8,7 @@ information provided here is intended especially for developers.
Now you have to pass all items and 3 optional params have been added.
- CoreCourseModulePrefetchDelegate.getPrefetchHandlerFor now admits module name instead of full module object.
- CoreCourse.getModuleBasicInfoByInstance and CoreCourse.getModuleBasicInfo have been modified to accept an "options" parameter instead of only siteId.
- The function CoreFilepool.isFileDownloadingByUrl now returns Promise<boolean> instead of relying on resolve/reject.
=== 3.9.5 ===