From e91e5d1cceb6dd3d6236c510148019037c432a87 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 23 Dec 2021 12:27:57 +0100 Subject: [PATCH] MOBILE-3897 link: Wait for download to finish before open file --- src/core/directives/external-content.ts | 2 +- src/core/directives/link.ts | 21 +++++++++++++++++++ .../services/module-prefetch-delegate.ts | 7 ++----- src/core/services/filepool.ts | 12 ++++++++--- upgrade.txt | 1 + 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/core/directives/external-content.ts b/src/core/directives/external-content.ts index 50bd68e34..6bbf6d9f4 100644 --- a/src/core/directives/external-content.ts +++ b/src/core/directives/external-content.ts @@ -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. diff --git a/src/core/directives/link.ts b/src/core/directives/link.ts index 809934e2b..81670c941 100644 --- a/src/core/directives/link.ts +++ b/src/core/directives/link.ts @@ -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); diff --git a/src/core/features/course/services/module-prefetch-delegate.ts b/src/core/features/course/services/module-prefetch-delegate.ts index 31984758f..a787928db 100644 --- a/src/core/features/course/services/module-prefetch-delegate.ts +++ b/src/core/features/course/services/module-prefetch-delegate.ts @@ -457,13 +457,10 @@ export class CoreCourseModulePrefetchDelegateService extends CoreDelegate { + async isFileDownloadingByUrl(siteId: string, fileUrl: string): Promise { const file = await this.fixPluginfileURL(siteId, fileUrl); const fileId = this.getFileIdByUrl(CoreFileHelper.getFileUrl(file)); - await this.hasFileInQueue(siteId, fileId); + try { + await this.hasFileInQueue(siteId, fileId); + + return true; + } catch { + return false; + } } /** diff --git a/upgrade.txt b/upgrade.txt index 75a4f115f..b373c0db4 100644 --- a/upgrade.txt +++ b/upgrade.txt @@ -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 instead of relying on resolve/reject. === 3.9.5 ===