From dccfb64e1fcdbaac48e19bd9bde6655dde85886a Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 22 May 2018 16:32:56 +0200 Subject: [PATCH] MOBILE-2376 core: Fix issues with pluginfile.php - Improve how to detect if the URL has params. - Replace & with & - Only add webservice to the URL if it's the core pluginfile --- src/classes/site.ts | 2 +- src/core/course/providers/helper.ts | 9 ++++++++- src/providers/utils/url.ts | 14 +++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/classes/site.ts b/src/classes/site.ts index dc0ebd97f..90124a1e8 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -931,7 +931,7 @@ export class CoreSite { * @return {string} Fixed URL. */ fixPluginfileURL(url: string): string { - return this.urlUtils.fixPluginfileURL(url, this.token); + return this.urlUtils.fixPluginfileURL(url, this.token, this.siteUrl); } /** diff --git a/src/core/course/providers/helper.ts b/src/core/course/providers/helper.ts index 02418da21..e55e9e287 100644 --- a/src/core/course/providers/helper.ts +++ b/src/core/course/providers/helper.ts @@ -607,7 +607,14 @@ export class CoreCourseHelperProvider { if (status === CoreConstants.DOWNLOADED) { // Get the local file URL. - return this.filepoolProvider.getInternalUrlByUrl(siteId, fileUrl); + return this.filepoolProvider.getInternalUrlByUrl(siteId, fileUrl).catch((error) => { + // File not found, mark the module as not downloaded and reject. + return this.filepoolProvider.storePackageStatus(siteId, CoreConstants.NOT_DOWNLOADED, component, + componentId).then(() => { + + return Promise.reject(error); + }); + }); } else if (status === CoreConstants.DOWNLOADING && !this.appProvider.isDesktop()) { // Return the online URL. return fixedUrl; diff --git a/src/providers/utils/url.ts b/src/providers/utils/url.ts index d3869b1e0..094640ea8 100644 --- a/src/providers/utils/url.ts +++ b/src/providers/utils/url.ts @@ -14,6 +14,7 @@ import { Injectable } from '@angular/core'; import { CoreLangProvider } from '../lang'; +import { CoreTextUtilsProvider } from './text'; /* * "Utils" service with helper functions for URLs. @@ -21,7 +22,7 @@ import { CoreLangProvider } from '../lang'; @Injectable() export class CoreUrlUtilsProvider { - constructor(private langProvider: CoreLangProvider) { } + constructor(private langProvider: CoreLangProvider, private textUtils: CoreTextUtilsProvider) { } /** * Add or remove 'www' from a URL. The url needs to have http or https protocol. @@ -69,13 +70,16 @@ export class CoreUrlUtilsProvider { * * @param {string} url The url to be fixed. * @param {string} token Token to use. + * @param {string} siteUrl The URL of the site the URL belongs to. * @return {string} Fixed URL. */ - fixPluginfileURL(url: string, token: string): string { + fixPluginfileURL(url: string, token: string, siteUrl: string): string { if (!url || !token) { return ''; } + url = url.replace(/&/g, '&'); + // First check if we need to fix this url or is already fixed. if (url.indexOf('token=') != -1) { return url; @@ -86,8 +90,8 @@ export class CoreUrlUtilsProvider { return url; } - // In which way the server is serving the files? Are we using slash parameters? - if (url.indexOf('?file=') != -1 || url.indexOf('?forcedownload=') != -1 || url.indexOf('?rev=') != -1) { + // Check if the URL already has params. + if (url.match(/\?[^=]+=/)) { url += '&'; } else { url += '?'; @@ -96,7 +100,7 @@ export class CoreUrlUtilsProvider { url += 'token=' + token + '&offline=1'; // Some webservices returns directly the correct download url, others not. - if (url.indexOf('/webservice/pluginfile') == -1) { + if (url.indexOf(this.textUtils.concatenatePaths(siteUrl, 'pluginfile.php')) === 0) { url = url.replace('/pluginfile', '/webservice/pluginfile'); }