107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using Kreta.BusinessLogic.HelperClasses;
|
|
using Kreta.BusinessLogic.Interfaces;
|
|
using Kreta.Client.FileService.Request;
|
|
using Kreta.Client.FileService.Response;
|
|
using Kreta.Core.ConnectionType;
|
|
using Kreta.Core.Exceptions;
|
|
using Kreta.DataAccessManual;
|
|
using Kreta.Resources;
|
|
|
|
namespace Kreta.BusinessLogic.Helpers
|
|
{
|
|
public class FileHelper : LogicBase
|
|
{
|
|
#region Fields
|
|
|
|
private readonly IFileServiceHelper _fileServiceHelper;
|
|
|
|
#endregion Fields
|
|
|
|
#region Constructors
|
|
|
|
public FileHelper(IConnectionType connectionType, IFileServiceHelper fileServiceHelper) : base(connectionType)
|
|
{
|
|
_fileServiceHelper = fileServiceHelper ?? throw new ArgumentNullException(nameof(fileServiceHelper));
|
|
}
|
|
|
|
public FileHelper(IConnectionType connectionType) : base(connectionType) { }
|
|
|
|
#endregion Constructors
|
|
|
|
#region FileService Functions
|
|
|
|
public void DeleteFile(int id, bool withFileServiceDelete)
|
|
{
|
|
Dal.CustomConnection.Run(ConnectionType, h =>
|
|
{
|
|
if (withFileServiceDelete)
|
|
{
|
|
var file = h.FileDAL().Get(id);
|
|
if (!_fileServiceHelper.Delete(IntezmenyAzonosito, new FileDeleteRequest(file.Utvonal, file.FileGuid.Value)))
|
|
{
|
|
throw new BlException(ErrorResource.FajlTorleseSikertelen);
|
|
}
|
|
}
|
|
|
|
h.FileDAL().Delete(id, FelhasznaloId);
|
|
});
|
|
}
|
|
|
|
public (string Name, byte[] Data) GetFileData(int id)
|
|
{
|
|
var file = Dal.CustomConnection.Run(ConnectionType, dalHandler => dalHandler.FileDAL().Get(id));
|
|
var fileName = string.Concat(file.FileNev.TrimEnd('.'), '.', file.Extension.TrimStart('.'));
|
|
var fileContent = _fileServiceHelper.GetFile(IntezmenyAzonosito, new GetUrlRequest(file.Utvonal, file.FileGuid.Value, fileName));
|
|
|
|
var data = Convert.FromBase64String(fileContent.Tartalom);
|
|
return (fileName, data);
|
|
}
|
|
|
|
public FileDownloadCO GetFileDownload(int id)
|
|
{
|
|
var (name, data) = GetFileData(id);
|
|
return new FileDownloadCO() { Stream = new MemoryStream(data), FileName = name };
|
|
}
|
|
|
|
public int? UploadFile(string fileName, byte[] content, string contentType, string path)
|
|
{
|
|
FileUploadResponse fileUploadResponse = _fileServiceHelper.Upload(new FileUploadRequest(fileName, content, contentType, path));
|
|
|
|
if (fileUploadResponse.IsSuccess)
|
|
{
|
|
return Dal.CustomConnection.Run(ConnectionType, h =>
|
|
{
|
|
var fileDal = h.FileDAL();
|
|
|
|
var file = fileDal.Get();
|
|
file.FeltoltesDatum = DateTime.Now;
|
|
file.FileGuid = fileUploadResponse.FajlAzonosito;
|
|
file.FileSizeByte = fileUploadResponse.FajlMeretByteLength;
|
|
|
|
var lastIndex = fileName.LastIndexOf('.');
|
|
var fileNev = fileName.Substring(0, lastIndex);
|
|
var fileExt = fileName.Substring(lastIndex + 1);
|
|
|
|
file.FileNev = fileNev;
|
|
file.Extension = fileExt;
|
|
file.Utvonal = fileUploadResponse.Utvonal;
|
|
file.TanevId = TanevId;
|
|
file.IntezmenyId = IntezmenyId;
|
|
file.IntezmenyGuid = h.IntezmenyDal().Get(IntezmenyId).Guid.Value;
|
|
file.VeglegesitesDatum = DateTime.Now;
|
|
file.FelhasznaloId = FelhasznaloId;
|
|
|
|
var fileId = fileDal.Insert(file);
|
|
|
|
return fileId;
|
|
});
|
|
}
|
|
|
|
throw new BlException(ErrorResource.FajlFeltolteseSikertelen);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|