using System; using Kreta.BusinessLogic.Caching; using Kreta.BusinessLogic.Interfaces; using Kreta.Client.FileService; using Kreta.Client.FileService.Request; using Kreta.Client.FileService.Response; namespace Kreta.BusinessLogic.Helpers { internal class FileServiceHelper : IFileServiceHelper { private readonly IFileServiceClient fileServiceClient; private readonly IFileServiceClientV3 fileServiceClientV3; private readonly ITokenServiceHelper tokenServiceHelper; private FileServiceCache FileServiceCache => KretaServer.KretaServer.Instance.CacheManager.AquireCache(); public FileServiceHelper(IFileServiceClient fileServiceClient, IFileServiceClientV3 fileServiceClientV3, ITokenServiceHelper tokenServiceHelper) { this.fileServiceClient = fileServiceClient ?? throw new ArgumentNullException(nameof(fileServiceClient)); this.fileServiceClientV3 = fileServiceClientV3 ?? throw new ArgumentNullException(nameof(fileServiceClientV3)); this.tokenServiceHelper = tokenServiceHelper ?? throw new ArgumentNullException(nameof(tokenServiceHelper)); } public FileUploadResponse Upload(IFileUploadRequest fileUploadRequest) { var response = fileServiceClientV3.Upload(FileServiceCache.GetPrivateToken(tokenServiceHelper), fileUploadRequest); if (response.TryAgain) { return fileServiceClientV3.Upload(FileServiceCache.GetPrivateToken(tokenServiceHelper, forceUpdate: true), fileUploadRequest); } return response; } public FileFinalizeResponseV3 Veglegesites(FileFinalizeRequestV3 fileVeglegesitesRequest) { var response = fileServiceClientV3.Veglegesites(FileServiceCache.GetPrivateToken(tokenServiceHelper), fileVeglegesitesRequest); if (response.TryAgain) { response = fileServiceClientV3.Veglegesites(FileServiceCache.GetPrivateToken(tokenServiceHelper, forceUpdate: true), fileVeglegesitesRequest); } return response; } public string GetUrl(string intezmenyAzonosito, GetUrlRequest fileDownloadRequest) { var response = fileServiceClient.GetUrl(intezmenyAzonosito, FileServiceCache.GetPrivateToken(tokenServiceHelper), fileDownloadRequest); if (response.TryAgain) { response = fileServiceClient.GetUrl(intezmenyAzonosito, FileServiceCache.GetPrivateToken(tokenServiceHelper, forceUpdate: true), fileDownloadRequest); } return response.Url; } public GetFileResponse GetFile(string intezmenyAzonosito, GetUrlRequest fileDownloadRequest) { fileDownloadRequest.Utvonal = CalculateFajlUtvonalKorabbiKompatibilitasMiatt(intezmenyAzonosito, fileDownloadRequest.Utvonal); var response = fileServiceClientV3.GetFile(intezmenyAzonosito, FileServiceCache.GetPrivateToken(tokenServiceHelper), fileDownloadRequest); if (response.TryAgain) { response = fileServiceClientV3.GetFile(intezmenyAzonosito, FileServiceCache.GetPrivateToken(tokenServiceHelper, forceUpdate: true), fileDownloadRequest); } return response; } public bool Delete(string intezmenyAzonosito, FileDeleteRequest fileDeleteRequest) { fileDeleteRequest.Utvonal = CalculateFajlUtvonalKorabbiKompatibilitasMiatt(intezmenyAzonosito, fileDeleteRequest.Utvonal); var response = fileServiceClientV3.Delete(intezmenyAzonosito, FileServiceCache.GetPrivateToken(tokenServiceHelper), fileDeleteRequest); if (response.TryAgain) { response = fileServiceClientV3.Delete(intezmenyAzonosito, FileServiceCache.GetPrivateToken(tokenServiceHelper, forceUpdate: true), fileDeleteRequest); } return response.IsSuccess; } private string CalculateFajlUtvonalKorabbiKompatibilitasMiatt(string intezmenyAzonosito, string utvonal) { bool regiUtvonalFormatum = utvonal.Length == 8; if (regiUtvonalFormatum) { var fajlUtvonalEv = utvonal.Substring(0, 4); var fajlUtvonalHonapNap = utvonal.Substring(4, 4); return $"HaziFeladatok/{intezmenyAzonosito}/{fajlUtvonalEv}/{fajlUtvonalHonapNap}"; } return utvonal; } } }