using GB5Shared.DTO.ECM;
using GB5Shared.FileUpload;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.DTO.Framework.ResponseStandard;
using Microsoft.AspNetCore.Mvc;
using static GB5Shared.GB5Constant.Constant;
namespace FrameworkSL.Controllers.FileUpload
{
///
/// ECM physical-file attachment service.
/// Upload route mirrors the GB4 signature:
/// POST /File/Upload/Web/With/MetaData/Generic?ObjectId=&ObjectTypeId=&DocumentDetailId=
///
[ApiController]
[Route("File")]
public class ECMUploadService : ControllerBase
{
private readonly IFileUploadBLL _FileUploadBLL;
public ECMUploadService(IFileUploadBLL FileUploadBLL)
{
_FileUploadBLL = FileUploadBLL;
}
// ── Upload ────────────────────────────────────────────────────
///
/// Uploads a file and saves its metadata to TATTACHMENT.
/// Matches GB4: ObjectId, ObjectTypeId, DocumentDetailId passed as query params.
///
[HttpPost("Upload/Web/With/MetaData/Generic")]
[DisableRequestSizeLimit]
public async Task> FileUploadWebWithMetaData(
IFormFile File,
[FromQuery] int ObjectId,
[FromQuery] int ObjectTypeId,
[FromQuery] int DocumentDetailId,
[FromQuery] string? TempRefKey,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
string result = await _FileUploadBLL.FileUploadWebWithMetaData(
File, ObjectId, ObjectTypeId, DocumentDetailId, LoginDTO!, TempRefKey);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── Upload (Base64 JSON — all fields in body) ────────────────
///
/// Accepts file as Base64 JSON body. All TATTACHMENT fields are sent in the POST body.
/// POST /File/UploadFile/TempRefKeyService
///
[HttpPost("UploadFile/TempRefKeyService")]
[DisableRequestSizeLimit]
public async Task> FileUploadBase64WithTempRefKey(
[FromBody] FileUploadBase64ContentDTO Body,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
var request = new FileUploadBase64RequestDTO
{
FileName = Body.FileName,
Base64Content = Body.Base64Content,
ObjectId = Body.ObjectId,
ObjectTypeId = Body.ObjectTypeId,
DocumentDetailId = Body.DocumentDetailId,
Tags = Body.Tags,
ObjectHeaderId = Body.ObjectHeaderId,
ObjectHeaderTypeId = Body.ObjectHeaderTypeId,
ObjectHeaderGuid = Body.ObjectHeaderGuid,
DisplayFileName = Body.DisplayFileName,
Remarks = Body.Remarks,
SlNo = Body.SlNo,
BizTransactionTypeId = Body.BizTransactionTypeId,
AttachmentType = Body.AttachmentType,
DocumentTypeId = Body.DocumentTypeId,
ValidFrom = Body.ValidFrom,
ValidTo = Body.ValidTo,
ClassificationMarkId = Body.ClassificationMarkId,
Status = Body.Status,
LinkUrl = Body.LinkUrl,
Thumbnail = Body.Thumbnail,
RowGuid = Body.RowGuid
};
var result = await _FileUploadBLL.FileUploadBase64Async(request, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── Upload (Base64 JSON — simpler for frontend) ───────────────
///
/// Simplified upload: accepts file as Base64 in a plain JSON body.
/// No multipart/FormData needed — frontend just sends application/json.
///
/// POST /File/Upload/Base64/Generic
/// Body: { "FileName": "doc.pdf", "Base64Content": "...", "ObjectId": 123, "ObjectTypeId": -1399999905, "DocumentDetailId": -1 }
///
[HttpPost("Upload/Base64/Generic")]
[DisableRequestSizeLimit]
public async Task> FileUploadBase64(
[FromBody] FileUploadBase64RequestDTO Request,
[FromQuery] string? TempRefKey,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
var result = await _FileUploadBLL.FileUploadBase64Async(Request, LoginDTO!, TempRefKey);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── Get ───────────────────────────────────────────────────────
/// GET /File/Get/Attachment/{attachmentId}
[HttpGet("Get/Attachment/{AttachmentId:int}")]
public async Task> GetAttachment(
int AttachmentId,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
string result = await _FileUploadBLL.GetAttachment(AttachmentId, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── List ──────────────────────────────────────────────────────
/// GET /File/List/Attachment?ObjectId=&ObjectTypeId=
[HttpGet("List/Attachment")]
public async Task> ListAttachments(
[FromQuery] int ObjectId,
[FromQuery] int ObjectTypeId,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
var result = await _FileUploadBLL.ListAttachments(ObjectId, ObjectTypeId, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponseEnumerable(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── List by ObjectId ──────────────────────────────────────────
/// GET /File/List/Attachment/ByObjectId?ObjectId=
[HttpGet("List/Attachment/ByObjectId")]
public async Task> ListAttachmentsByObjectId(
[FromQuery] int ObjectId,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
var result = await _FileUploadBLL.ListAttachmentsByObjectId(ObjectId, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponseEnumerable(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── List by ObjectHeaderId + ObjectId ─────────────────────────
/// GET /File/List/Attachment/ByHeaderAndObjectId?ObjectHeaderId=&ObjectId=
[HttpGet("List/Attachment/ByHeaderAndObjectId")]
public async Task> ListAttachmentsByHeaderAndObjectId(
[FromQuery] int ObjectHeaderId,
[FromQuery] int ObjectId,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
var result = await _FileUploadBLL.ListAttachmentsByHeaderAndObjectId(ObjectHeaderId, ObjectId, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponseEnumerable(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── Update ────────────────────────────────────────────────────
/// PUT /File/Update/Attachment
[HttpPut("Update/Attachment")]
public async Task> UpdateAttachment(
[FromBody] AttachmentUpdateDTO Dto,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
string result = await _FileUploadBLL.UpdateAttachment(Dto, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── Download as Base64 ────────────────────────────────────────
///
/// Returns the file as Base64 so the front-end can render/download it directly.
/// GET /File/Download/Attachment/{attachmentId}
/// Response body: { AttachmentId, FileName, MimeType, Base64Content, FileSizeBytes }
///
[HttpGet("Download/Attachment/{AttachmentId:int}")]
public async Task> DownloadAttachmentAsBase64(
int AttachmentId,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
var result = await _FileUploadBLL.DownloadAttachmentAsBase64(AttachmentId, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
// ── Delete ────────────────────────────────────────────────────
/// DELETE /File/Delete/Attachment/{attachmentId}
[HttpDelete("Delete/Attachment/{AttachmentId:int}")]
public async Task> DeleteAttachment(
int AttachmentId,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
string result = await _FileUploadBLL.DeleteAttachment(AttachmentId, LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex, CacheKeyLevel.NOT_REQUIRED, LoginDTO!, ex.Message, 500);
}
}
[HttpGet("List/Attachment/ByHeaderAndObjectHeaderTypeId")]
public async Task> ListAttachmentsByHeaderAndObjectHeaderTypeId(
[FromQuery] int ObjectHeaderTypeId,
[FromQuery] int ObjectHeaderId,
[FromHeader(Name = "Login")] string LoginDTOJson)
{
var LoginDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(LoginDTOJson);
try
{
var result = await _FileUploadBLL.ListAttachmentsByHeaderAndObjectHeaderTypeId(
ObjectHeaderTypeId,
ObjectHeaderId,
LoginDTO!);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponseEnumerable(
result,
CacheKeyLevel.NOT_REQUIRED,
LoginDTO!);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(
ex,
CacheKeyLevel.NOT_REQUIRED,
LoginDTO!,
ex.Message,
500);
}
}
}
}