diff --git a/src/addons/mod/page/services/page-helper.ts b/src/addons/mod/page/services/page-helper.ts index bb07c18c0..6721f6303 100644 --- a/src/addons/mod/page/services/page-helper.ts +++ b/src/addons/mod/page/services/page-helper.ts @@ -38,28 +38,26 @@ export class AddonModPageHelperProvider { * @returns The HTML of the page. */ async getPageHtml(contents: CoreCourseModuleContentFile[], moduleId: number): Promise { - let indexUrl: string | undefined; + let indexFile: CoreCourseModuleContentFile | undefined; const paths: Record = {}; // Extract the information about paths from the module contents. contents.forEach((content) => { - const url = content.fileurl; - if (this.isMainPage(content)) { // This seems to be the most reliable way to spot the index page. - indexUrl = url; + indexFile = content; } else { let key = content.filename; if (content.filepath !== '/') { // Add the folders without the leading slash. key = content.filepath.substring(1) + key; } - paths[CoreUrl.decodeURIComponent(key)] = url; + paths[CoreUrl.decodeURIComponent(key)] = content.fileurl; } }); // Promise handling when we are in a browser. - if (!indexUrl) { + if (!indexFile) { // If ever that happens. throw new CoreError('Could not locate the index page'); } @@ -69,14 +67,15 @@ export class AddonModPageHelperProvider { // The file system is available. url = await CoreFilepool.downloadUrl( CoreSites.getCurrentSiteId(), - indexUrl, + indexFile.fileurl, false, ADDON_MOD_PAGE_COMPONENT, moduleId, + indexFile.timemodified, ); } else { // We return the live URL. - url = await CoreSites.getCurrentSite()?.checkAndFixPluginfileURL(indexUrl) || ''; + url = await CoreSites.getCurrentSite()?.checkAndFixPluginfileURL(indexFile.fileurl) || ''; } const content = await CoreWS.getText(url); diff --git a/src/core/singletons/tests/url.test.ts b/src/core/singletons/tests/url.test.ts index 7903de55f..697b383d8 100644 --- a/src/core/singletons/tests/url.test.ts +++ b/src/core/singletons/tests/url.test.ts @@ -316,12 +316,12 @@ describe('CoreUrl singleton', () => { .toEqual(['6', 'mod_foo', 'content', '14', 'foo.txt']); expect(CoreUrl.getPluginFileArgs('http://mysite.com/webservice/pluginfile.php/6/mod_foo/content/14/foo.txt')) .toEqual(['6', 'mod_foo', 'content', '14', 'foo.txt']); + expect(CoreUrl.getPluginFileArgs('http://mysite.com/tokenpluginfile.php/abcdef123456/6/mod_foo/content/14/foo.txt')) + .toEqual(['6', 'mod_foo', 'content', '14', 'foo.txt']); - // It doesn't work with tokenpluginfile or other URLs, and also when pluginfile doesn't have enough params. + // It doesn't work with other URLs, and also when pluginfile doesn't have enough params. expect(CoreUrl.getPluginFileArgs('http://mysite.com')).toEqual(undefined); expect(CoreUrl.getPluginFileArgs('http://mysite.com/pluginfile.php/6/')).toEqual(undefined); - expect(CoreUrl.getPluginFileArgs('http://mysite.com/tokenpluginfile.php/abcdef123456/6/mod_foo/content/14/foo.txt')) - .toEqual(undefined); }); }); diff --git a/src/core/singletons/url.ts b/src/core/singletons/url.ts index dafbe1c3b..29e7db27d 100644 --- a/src/core/singletons/url.ts +++ b/src/core/singletons/url.ts @@ -693,19 +693,22 @@ export class CoreUrl { } /** - * Return the array of arguments of the pluginfile url. + * Return the array of arguments of the pluginfile or tokenpluginfile url. * * @param url URL to get the args. * @returns The args found, undefined if not a pluginfile. */ static getPluginFileArgs(url: string): string[] | undefined { - if (!CoreUrl.isPluginFileUrl(url)) { - // Not pluginfile, return. - return; - } + let args: string[] = []; - const relativePath = url.substring(url.indexOf('/pluginfile.php') + 16); - const args = relativePath.split('/'); + if (CoreUrl.isPluginFileUrl(url)) { + const relativePath = url.substring(url.indexOf('/pluginfile.php') + 16); + args = relativePath.split('/'); + } else if (CoreUrl.isTokenPluginFileUrl(url)) { + const relativePath = url.substring(url.indexOf('/tokenpluginfile.php') + 21); + args = relativePath.split('/'); + args.shift(); // Remove the token. + } if (args.length < 3) { // To be a plugin file it should have at least contextId, Component and Filearea.