2019-11-05 12:00:05 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { CoreFileProvider } from '@providers/file';
|
|
|
|
import { CorePluginFileHandler } from '@providers/plugin-file-delegate';
|
|
|
|
import { CoreMimetypeUtilsProvider } from '@providers/utils/mimetype';
|
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
|
|
|
import { CoreUrlUtilsProvider } from '@providers/utils/url';
|
|
|
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
|
|
|
import { CoreH5PProvider } from './h5p';
|
|
|
|
import { CoreWSExternalFile } from '@providers/ws';
|
2019-11-25 14:05:49 +00:00
|
|
|
import { FileEntry } from '@ionic-native/file';
|
2020-02-06 14:06:10 +00:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-11-05 12:00:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler to treat H5P files.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreH5PPluginFileHandler implements CorePluginFileHandler {
|
|
|
|
name = 'CoreH5PPluginFileHandler';
|
|
|
|
|
|
|
|
constructor(protected urlUtils: CoreUrlUtilsProvider,
|
|
|
|
protected mimeUtils: CoreMimetypeUtilsProvider,
|
|
|
|
protected textUtils: CoreTextUtilsProvider,
|
|
|
|
protected utils: CoreUtilsProvider,
|
|
|
|
protected fileProvider: CoreFileProvider,
|
2020-02-06 14:06:10 +00:00
|
|
|
protected h5pProvider: CoreH5PProvider,
|
|
|
|
protected translate: TranslateService) { }
|
2019-11-05 12:00:05 +00:00
|
|
|
|
2019-11-25 15:08:58 +00:00
|
|
|
/**
|
|
|
|
* React to a file being deleted.
|
|
|
|
*
|
|
|
|
* @param fileUrl The file URL used to download the file.
|
|
|
|
* @param path The path of the deleted file.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
|
|
|
fileDeleted(fileUrl: string, path: string, siteId?: string): Promise<any> {
|
|
|
|
// If an h5p file is deleted, remove the contents folder.
|
|
|
|
return this.h5pProvider.deleteContentByUrl(fileUrl, siteId);
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:37:00 +00:00
|
|
|
/**
|
|
|
|
* Check whether a file can be downloaded. If so, return the file to download.
|
|
|
|
*
|
|
|
|
* @param file The file data.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with the file to use. Rejected if cannot download.
|
|
|
|
*/
|
|
|
|
getDownloadableFile(file: CoreWSExternalFile, siteId?: string): Promise<CoreWSExternalFile> {
|
|
|
|
return this.h5pProvider.getTrustedH5PFile(file.fileurl, {}, false, siteId);
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:00:05 +00:00
|
|
|
/**
|
|
|
|
* Given an HTML element, get the URLs of the files that should be downloaded and weren't treated by
|
2019-12-09 14:34:08 +00:00
|
|
|
* CoreFilepoolProvider.extractDownloadableFilesFromHtml.
|
2019-11-05 12:00:05 +00:00
|
|
|
*
|
|
|
|
* @param container Container where to get the URLs from.
|
2019-12-11 07:21:20 +00:00
|
|
|
* @return List of URLs.
|
2019-11-05 12:00:05 +00:00
|
|
|
*/
|
2019-12-04 12:37:00 +00:00
|
|
|
getDownloadableFilesFromHTML(container: HTMLElement): string[] {
|
2019-11-05 12:00:05 +00:00
|
|
|
const iframes = <HTMLIFrameElement[]> Array.from(container.querySelectorAll('iframe.h5p-iframe'));
|
|
|
|
const urls = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < iframes.length; i++) {
|
|
|
|
const params = this.urlUtils.extractUrlParams(iframes[i].src);
|
|
|
|
|
|
|
|
if (params.url) {
|
|
|
|
urls.push(params.url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a file size.
|
|
|
|
*
|
|
|
|
* @param file The file data.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with the size.
|
|
|
|
*/
|
|
|
|
getFileSize(file: CoreWSExternalFile, siteId?: string): Promise<number> {
|
|
|
|
return this.h5pProvider.getTrustedH5PFile(file.fileurl, {}, false, siteId).then((file) => {
|
|
|
|
return file.filesize;
|
|
|
|
}).catch((error): any => {
|
|
|
|
if (this.utils.isWebServiceError(error)) {
|
|
|
|
// WS returned an error, it means it cannot be downloaded.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:34:08 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the handler is enabled on a site level.
|
|
|
|
*
|
|
|
|
* @return Whether or not the handler is enabled on a site level.
|
|
|
|
*/
|
|
|
|
isEnabled(): boolean | Promise<boolean> {
|
|
|
|
return this.h5pProvider.canGetTrustedH5PFileInSite();
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:06:10 +00:00
|
|
|
/**
|
|
|
|
* Check if a file is downloadable.
|
|
|
|
*
|
|
|
|
* @param file The file data.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with a boolean and a reason why it isn't downloadable if needed.
|
|
|
|
*/
|
|
|
|
async isFileDownloadable(file: CoreWSExternalFile, siteId?: string): Promise<{downloadable: boolean, reason?: string}> {
|
|
|
|
const offlineDisabled = await this.h5pProvider.isOfflineDisabled(siteId);
|
|
|
|
|
|
|
|
if (offlineDisabled) {
|
|
|
|
return {
|
|
|
|
downloadable: false,
|
|
|
|
reason: this.translate.instant('core.h5p.offlinedisabled'),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
downloadable: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:00:05 +00:00
|
|
|
/**
|
|
|
|
* Check whether the file should be treated by this handler. It is used in functions where the component isn't used.
|
|
|
|
*
|
|
|
|
* @param file The file data.
|
|
|
|
* @return Whether the file should be treated by this handler.
|
|
|
|
*/
|
|
|
|
shouldHandleFile(file: CoreWSExternalFile): boolean {
|
|
|
|
return this.mimeUtils.guessExtensionFromUrl(file.fileurl) == 'h5p';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Treat a downloaded file.
|
|
|
|
*
|
|
|
|
* @param fileUrl The file URL used to download the file.
|
|
|
|
* @param file The file entry of the downloaded file.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when done.
|
|
|
|
*/
|
|
|
|
treatDownloadedFile(fileUrl: string, file: FileEntry, siteId?: string): Promise<any> {
|
2019-11-07 16:30:37 +00:00
|
|
|
return this.h5pProvider.extractH5PFile(fileUrl, file, siteId);
|
2019-11-05 12:00:05 +00:00
|
|
|
}
|
|
|
|
}
|