|
@@ -30,24 +30,33 @@ namespace MTWorkHR.Application.Services
|
|
_containerClient.CreateIfNotExists();
|
|
_containerClient.CreateIfNotExists();
|
|
_logger = logger;
|
|
_logger = logger;
|
|
}
|
|
}
|
|
|
|
+
|
|
public async Task<AttachmentResponseDto> UploadFile(IFormFile file)
|
|
public async Task<AttachmentResponseDto> UploadFile(IFormFile file)
|
|
{
|
|
{
|
|
AttachmentResponseDto result = new AttachmentResponseDto();
|
|
AttachmentResponseDto result = new AttachmentResponseDto();
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- var blobClient = _containerClient.GetBlobClient(file.FileName);
|
|
|
|
|
|
+ string uniqueFileName = GenerateUniqueFileName(file.FileName);
|
|
|
|
+ var blobClient = _containerClient.GetBlobClient(uniqueFileName);
|
|
|
|
+
|
|
if (blobClient != null)
|
|
if (blobClient != null)
|
|
{
|
|
{
|
|
|
|
+ if (blobClient.ExistsAsync().Result)
|
|
|
|
+ {
|
|
|
|
+ uniqueFileName = GenerateUniqueFileName(file.FileName);
|
|
|
|
+ }
|
|
var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
|
|
var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
|
|
- //if(status.Value == "201")
|
|
|
|
- result.FileName = file.FileName;
|
|
|
|
|
|
+ result.OriginalName = file.FileName;
|
|
|
|
+ result.FileName = uniqueFileName;
|
|
result.FilePath = blobClient.Uri.AbsoluteUri;
|
|
result.FilePath = blobClient.Uri.AbsoluteUri;
|
|
return result;
|
|
return result;
|
|
- }else
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
|
|
+ _logger.LogError(ex.Message);
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -55,34 +64,60 @@ namespace MTWorkHR.Application.Services
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- var blobClient = _containerClient.GetBlobClient(file.FileName);
|
|
|
|
- if(blobClient != null)
|
|
|
|
|
|
+ // Generate a unique file name to avoid overwriting
|
|
|
|
+ string uniqueFileName = GenerateUniqueFileName(file.FileName);
|
|
|
|
+ var blobClient = _containerClient.GetBlobClient(uniqueFileName);
|
|
|
|
+
|
|
|
|
+ if (blobClient != null)
|
|
{
|
|
{
|
|
- //var stream = file.FileData.OpenReadStream();
|
|
|
|
|
|
+ if (blobClient.ExistsAsync().Result)
|
|
|
|
+ {
|
|
|
|
+ uniqueFileName = GenerateUniqueFileName(file.FileName);
|
|
|
|
+ }
|
|
using (Stream fs = file.FileData.OpenReadStream())
|
|
using (Stream fs = file.FileData.OpenReadStream())
|
|
{
|
|
{
|
|
- var status = await blobClient.UploadAsync(fs, true);
|
|
|
|
|
|
+ // Upload the file to blob storage
|
|
|
|
+ await blobClient.UploadAsync(fs, true);
|
|
fs.Position = 0;
|
|
fs.Position = 0;
|
|
|
|
+
|
|
using (BinaryReader br = new BinaryReader(fs))
|
|
using (BinaryReader br = new BinaryReader(fs))
|
|
{
|
|
{
|
|
byte[] bytes = br.ReadBytes((Int32)fs.Length);
|
|
byte[] bytes = br.ReadBytes((Int32)fs.Length);
|
|
- var Headers = file.FileData.Headers;
|
|
|
|
|
|
+ var headers = file.FileData.Headers;
|
|
file.Content = bytes;
|
|
file.Content = bytes;
|
|
file.ContentType = file.FileData.ContentType;
|
|
file.ContentType = file.FileData.ContentType;
|
|
file.FilePath = blobClient.Uri.AbsoluteUri;
|
|
file.FilePath = blobClient.Uri.AbsoluteUri;
|
|
|
|
+ file.OriginalName = file.FileName;
|
|
|
|
+ file.FileName = uniqueFileName;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- //if(status.Value == "201")
|
|
|
|
return blobClient.Uri.AbsoluteUri;
|
|
return blobClient.Uri.AbsoluteUri;
|
|
- }else
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
return "";
|
|
return "";
|
|
-
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
|
|
+ // Log the exception for debugging (e.g., using ILogger)
|
|
return "";
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // Helper method to generate a unique file name
|
|
|
|
+ private string GenerateUniqueFileName(string originalFileName)
|
|
|
|
+ {
|
|
|
|
+ // Extract file name and extension
|
|
|
|
+ string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(originalFileName);
|
|
|
|
+ string extension = Path.GetExtension(originalFileName);
|
|
|
|
+
|
|
|
|
+ // Append a unique identifier (e.g., timestamp or GUID)
|
|
|
|
+ string uniqueIdentifier = DateTime.UtcNow.Ticks.ToString(); // or Guid.NewGuid().ToString()
|
|
|
|
+ return $"{fileNameWithoutExtension}_{uniqueIdentifier}{extension}";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
public async Task<AttachmentResponseDto> UploadFile(byte[] pdfBytes, string fileName)
|
|
public async Task<AttachmentResponseDto> UploadFile(byte[] pdfBytes, string fileName)
|
|
{
|
|
{
|
|
AttachmentResponseDto result = new AttachmentResponseDto();
|
|
AttachmentResponseDto result = new AttachmentResponseDto();
|
|
@@ -157,30 +192,6 @@ namespace MTWorkHR.Application.Services
|
|
}
|
|
}
|
|
return msgs;
|
|
return msgs;
|
|
}
|
|
}
|
|
- public async Task<Tuple<bool, string>> UploadFile2(IFormFile file)
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- var blobClient = _containerClient.GetBlobClient(file.FileName);
|
|
|
|
- var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
|
|
|
|
- return new Tuple<bool, string>(true, "");// = "File uploaded successfully.";
|
|
|
|
- }
|
|
|
|
- catch (Exception ex)
|
|
|
|
- {
|
|
|
|
- return new Tuple<bool, string>(false, ex.Message);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- public async Task<List<Tuple<bool, string>>> UploadFiles2(List<IFormFile> files)
|
|
|
|
- {
|
|
|
|
- List<Tuple<bool, string>> msgs = new List<Tuple<bool, string>>();
|
|
|
|
- foreach (var formFile in files)
|
|
|
|
- {
|
|
|
|
- msgs.Add( await UploadFile2(formFile));
|
|
|
|
- }
|
|
|
|
- return msgs;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|
|
public async Task<bool> Delete(string fileName)
|
|
public async Task<bool> Delete(string fileName)
|
|
{
|
|
{
|