using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using Kreta.BusinessLogic.Classes; using Kreta.BusinessLogic.HelperClasses; using Kreta.BusinessLogic.Interfaces; using Kreta.BusinessLogic.Utils; using Kreta.Client.FileService.Request; using Kreta.Client.FileService.Response; using Kreta.Core; using Kreta.Core.ConnectionType; using Kreta.Core.Enum; using Kreta.Core.Exceptions; using Kreta.DataAccess.Interfaces; using Kreta.DataAccessManual; using Kreta.DataAccessManual.Interfaces; using Kreta.Resources; namespace Kreta.BusinessLogic.Helpers { public class DktFileHelper : LogicBase { #region Fields private readonly IFileServiceHelper fileServiceHelper; #endregion Fields #region Constructors public DktFileHelper(IConnectionType connectionType, IFileServiceHelper fileServiceHelper) : base(connectionType) { this.fileServiceHelper = fileServiceHelper ?? throw new ArgumentNullException(nameof(fileServiceHelper)); } public DktFileHelper(IConnectionType connectionType) : base(connectionType) { } #endregion Constructors #region FileService Functions public void DeleteCsatolmany(int id, bool withFileServiceDelete) { Dal.CustomConnection.Run(ConnectionType, dalHandler => { if (withFileServiceDelete) { IDKT_File csatolmany = dalHandler.DKT_FileDAL().Get(id); if (!fileServiceHelper.Delete(IntezmenyAzonosito, new FileDeleteRequest(csatolmany.Utvonal, csatolmany.FileGuid.Value))) { throw new BlException(ErrorResource.FajlTorleseSikertelen); } } dalHandler.DKT_FileDAL().Delete(id, FelhasznaloId); }); } public (string Name, byte[] Data) GetCsatolmanyData(IDalHandler h, int id) { IDKT_File csatolmany = h.DKT_FileDAL().Get(id); var fileName = string.Concat(csatolmany.FileNev.TrimEnd('.'), '.', csatolmany.Extension.TrimStart('.')); var fileContent = fileServiceHelper.GetFile(IntezmenyAzonosito, new GetUrlRequest(csatolmany.Utvonal, csatolmany.FileGuid.Value, fileName)); byte[] data = null; if (!string.IsNullOrWhiteSpace(fileContent.Tartalom)) data = Convert.FromBase64String(fileContent.Tartalom); return (fileName, data); } /// INFO @Tojcsi: Mobil használja public (string Name, byte[] Data) GetCsatolmanyData(IDKT_File csatolmany) { var fileName = string.Concat(csatolmany.FileNev.TrimEnd('.'), '.', csatolmany.Extension.TrimStart('.')); var fileContent = fileServiceHelper.GetFile(IntezmenyAzonosito, new GetUrlRequest(csatolmany.Utvonal, csatolmany.FileGuid.Value, fileName)); byte[] data = Convert.FromBase64String(fileContent.Tartalom); return (fileName, data); } public FileDownloadCO GetCsatolmanyFile(int id) { var (Name, Data) = Dal.CustomConnection.Run(ConnectionType, dalHandler => GetCsatolmanyData(dalHandler, id)); return new FileDownloadCO() { Stream = new MemoryStream(Data), FileName = Name }; } public int? UploadCsatolmany(string fileName, byte[] content, string contentType, int hazifeladatId, Guid globalIntezmenyId) { string path = $"DKT/{globalIntezmenyId}/{TanevId}/hazifeladatok/{hazifeladatId}"; FileUploadResponse fileUploadResponse = fileServiceHelper.Upload(new DKT_FileUploadRequest(fileName, content, contentType, path)); if (fileUploadResponse.IsSuccess) { int result = Dal.CustomConnection.Run(ConnectionType, dalHandler => { IDKT_FileDAL dktFileDal = dalHandler.DKT_FileDAL(); IDKT_File csatolmany = dktFileDal.Get(); csatolmany.FeltoltesDatum = DateTime.Now; csatolmany.FileGuid = fileUploadResponse.FajlAzonosito; csatolmany.FileSizeByte = fileUploadResponse.FajlMeretByteLength; int lastIndex = fileName.LastIndexOf('.'); string fileNev = fileName.Substring(0, lastIndex); string fileExt = fileName.Substring(lastIndex + 1); csatolmany.FileNev = fileNev; csatolmany.Extension = fileExt; csatolmany.Utvonal = fileUploadResponse.Utvonal; csatolmany.TanevId = TanevId; csatolmany.IntezmenyId = IntezmenyId; csatolmany.IntezmenyGuid = dalHandler.IntezmenyDal().Get(IntezmenyId).Guid.Value; csatolmany.VeglegesitesDatum = DateTime.Now; csatolmany.FeltoltoAlkalmazottId = FelhasznaloId; var csatolmanyId = dktFileDal.Insert(csatolmany); dktFileDal.ConnectToHaziFeladat(IntezmenyId, TanevId, hazifeladatId, csatolmanyId); return csatolmanyId; }); return result; } throw new BlException(ErrorResource.FajlFeltolteseSikertelen); } public int UploadFile(string fileName, byte[] content, string contentType, string path) { FileUploadResponse fileUploadResponse = fileServiceHelper.Upload(new FileUploadRequest(fileName, content, contentType, path)); if (fileUploadResponse.IsSuccess) { int result = Dal.CustomConnection.Run(ConnectionType, dalHandler => { IDKT_FileDAL dktFileDal = dalHandler.DKT_FileDAL(); IDKT_File csatolmany = dktFileDal.Get(); csatolmany.FeltoltesDatum = DateTime.Now; csatolmany.FileGuid = fileUploadResponse.FajlAzonosito; csatolmany.FileSizeByte = fileUploadResponse.FajlMeretByteLength; int lastIndex = fileName.LastIndexOf('.'); string fileNev = fileName.Substring(0, lastIndex); string fileExt = fileName.Substring(lastIndex + 1); csatolmany.FileNev = fileNev; csatolmany.Extension = fileExt; csatolmany.Utvonal = fileUploadResponse.Utvonal; csatolmany.TanevId = TanevId; csatolmany.IntezmenyId = IntezmenyId; csatolmany.IntezmenyGuid = dalHandler.IntezmenyDal().Get(IntezmenyId).Guid.Value; csatolmany.VeglegesitesDatum = DateTime.Now; csatolmany.FeltoltoAlkalmazottId = FelhasznaloId; return dktFileDal.Insert(csatolmany); }); return result; } throw new BlException(ErrorResource.FajlFeltolteseSikertelen); } #endregion public double GetHaziFeladatCsatolmanyokOsszMeret(int? felhasznaloId = null) { double result = Dal.CustomConnection.Run(ConnectionType, dalHandler => dalHandler.DKT_FileDAL().GetHaziFeladatCsatolmanyokOsszMeret(TanevId, felhasznaloId ?? FelhasznaloId)); return result; } public int GetMaxAdatmennyisegFelhasznalokent(int felhasznaloId) { IUserProfile userProfile = Dal.CustomConnection.Run(ConnectionType, dalHandler => dalHandler.Felhasznalo().Get(felhasznaloId).UserProfile.FirstOrDefault(x => !x.Torolt && x.TanevId == TanevId)); int result = userProfile?.OsszFeltolthetoFajlKb ?? Constants.General.MaxFeltolthetoAdatmennyisegInKByte; return result; } public List GetHazifeladatCsatolmanyCoList(HFCsatolmanyokSearchCO co) { var result = new List(); DataSet dataSet = Dal.CustomConnection.Run(ConnectionType, dalHandler => dalHandler.DKT_FileDAL(GridParameters).GetHaziFeladatCsatolmanyok(TanevId, co.ConvertCoToPco())); foreach (DataRow dataRow in dataSet.Tables[0].AsEnumerable()) { result.Add(new HaziFeladatCsatolmanyItemCo(dataRow)); } return result; } public List<(int CsatolmanyId, string FajlNev)> GetCsatolmanyokForHaziFeladat(int haziFeladatId) { DataSet dataSet = GetCsatolmanyokForHaziFeladatDataSet(haziFeladatId); var result = new List<(int CsatolmanyId, string FajlNev)>(); foreach (DataRow row in dataSet.Tables[0].Rows) { int id = SDAConvert.ToInt32(row["ID"]); string fajlNev = $"{SDAConvert.ToString(row["FajlNev"]).TrimEnd('.')}.{SDAConvert.ToString(row["FajlKiterjesztes"]).TrimStart('.')}"; result.Add((id, fajlNev)); } return result; } public DataSet GetCsatolmanyokForHaziFeladatDataSet(int haziFeladatId) { DataSet result = Dal.CustomConnection.Run(ConnectionType, dalHandler => dalHandler.DKT_FileDAL().GetCsatolmanyokForHaziFeladatDataSet(TanevId, IntezmenyId, haziFeladatId)); result.Tables[0].Columns.Add("FajlMeretMB", typeof(double)); foreach (DataRow dataRow in result.Tables[0].Rows) { double fajlMeret = SDAConvert.ToDouble(dataRow["FajlMeret"]); dataRow["FajlMeretMB"] = CommonUtils.ConvertByteToMByte(fajlMeret); } return result; } /// INFO @Tojcsi: Mobil használja public (bool jogosult, IDKT_File feltoltottFile) GetFile(IDalHandler h, int tanuloId, int fileId) { var file = h.DKT_FileDAL().Get(fileId); if (file.Torolt) { throw new BlException(BlExceptionType.NemLetezoEntitas); } var jogosult = h.DKT_FileDAL().GetJogosultsag(tanuloId, fileId, TanevId); return (jogosult, file); } public bool IsFelhasznaloToltotteFel(IDalHandler h, int id) { return h.DKT_FileDAL().Get(id).FeltoltoAlkalmazottId == FelhasznaloId; } } }