// (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 { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreFileUploaderHelper } from '@features/fileuploader/services/fileuploader-helper'; import { AddonPrivateFiles, AddonPrivateFilesGetUserInfoWSResult } from './privatefiles'; import { CoreError } from '@classes/errors/error'; import { makeSingleton, Translate } from '@singletons'; /** * Service that provides some helper functions regarding private and site files. */ @Injectable({ providedIn: 'root', }) export class AddonPrivateFilesHelperProvider { /** * Select a file, upload it and move it to private files. * * @param info Private files info. See AddonPrivateFilesProvider.getPrivateFilesInfo. * @return Promise resolved when a file is uploaded, rejected otherwise. */ async uploadPrivateFile(info?: AddonPrivateFilesGetUserInfoWSResult): Promise { // Calculate the max size. const currentSite = CoreSites.instance.getCurrentSite(); let maxSize = currentSite?.getInfo()?.usermaxuploadfilesize || -1; let userQuota = currentSite?.getInfo()?.userquota; if (userQuota === 0) { // 0 means ignore user quota. In the app it is -1. userQuota = -1; } else if (userQuota !== undefined && userQuota > 0 && info !== undefined) { userQuota = userQuota - info.filesizewithoutreferences; } if (userQuota !== undefined) { // Use the minimum value. maxSize = Math.min(maxSize, userQuota); } // Select and upload the file. const result = await CoreFileUploaderHelper.instance.selectAndUploadFile(maxSize); if (!result) { throw new CoreError(Translate.instance.instant('core.fileuploader.errorwhileuploading')); } // File uploaded. Move it to private files. const modal = await CoreDomUtils.instance.showModalLoading('core.fileuploader.uploading', true); try { await AddonPrivateFiles.instance.moveFromDraftToPrivate(result.itemid); CoreDomUtils.instance.showToast('core.fileuploader.fileuploaded', true, undefined, 'core-toast-success'); } finally { modal.dismiss(); } } } export class AddonPrivateFilesHelper extends makeSingleton(AddonPrivateFilesHelperProvider) {}