ย้าย minioไป appcation

This commit is contained in:
Kittapath 2023-09-14 12:39:16 +07:00
parent fef7af3f87
commit eb09447626
4 changed files with 693 additions and 312 deletions

View file

@ -10,6 +10,7 @@
<PackageReference Include="AWSSDK.S3" Version="3.7.107.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="MimeTypeMapOfficial" Version="1.0.17" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

View file

@ -0,0 +1,366 @@
using Amazon.S3;
using Amazon.S3.Model;
using BMA.EHR.Domain.Models.Documents;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Net.Http.Headers;
using BMA.EHR.Application.Common.Interfaces;
using Amazon;
using BMA.EHR.Domain.Models.Organizations;
using MimeTypes;
using Profile = BMA.EHR.Domain.Models.HR.Profile;
namespace BMA.EHR.Application.Repositories
{
public class MinIOService
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IConfiguration _configuration;
private readonly AmazonS3Client _s3Client;
private readonly IWebHostEnvironment _hostingEnvironment;
private string _bucketName = string.Empty;
#endregion
#region " Constructors "
public MinIOService(IApplicationDBContext dbContext,
IConfiguration configuration,
IWebHostEnvironment hostingEnvironment)
{
_dbContext = dbContext;
_configuration = configuration;
_hostingEnvironment = hostingEnvironment;
var config = new AmazonS3Config
{
ServiceURL = _configuration["MinIO:Endpoint"],
ForcePathStyle = true
};
_s3Client = new AmazonS3Client(_configuration["MinIO:AccessKey"], _configuration["MinIO:SecretKey"], config);
this._bucketName = _configuration["MinIO:BucketName"] ?? "bma-recruit";
}
#endregion
#region " Methods "
public async Task<Document> UploadFileAsync(IFormFile file, string newFileName = "")
{
var fileName = "";
var fileExt = Path.GetExtension(file.FileName);
if (newFileName != "")
fileName = $"{newFileName}";
else
fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var tmpDir = Path.Combine("tmp");
if (!Directory.Exists(tmpDir))
Directory.CreateDirectory(tmpDir);
var tmpFile = Path.Combine(tmpDir, $"tmp_{DateTime.Now.ToString("ddMMyyyyHHmmss")}{fileExt}");
try
{
using (var ms = new MemoryStream())
{
var id = Guid.NewGuid();
file.CopyTo(ms);
var fileBytes = ms.ToArray();
System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileBytes);
var request = new PutObjectRequest
{
BucketName = _bucketName,
Key = id.ToString("D"),
InputStream = filestream,
ContentType = file.ContentType,
CannedACL = S3CannedACL.PublicRead
};
await _s3Client.PutObjectAsync(request);
// create document object
var doc = new Document()
{
FileName = fileName,
FileType = file.ContentType,
FileSize = Convert.ToInt32(file.Length),
ObjectRefId = id,
CreatedDate = DateTime.Now
};
await _dbContext.Set<Document>().AddAsync(doc);
await _dbContext.SaveChangesAsync();
return doc;
}
}
catch
{
throw;
}
finally
{
File.Delete(tmpFile);
}
}
public async Task<FileDownloadResponse> DownloadFileAsync(Guid fileId)
{
try
{
var doc = await _dbContext.Set<Document>().AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
using (var memoryStream = new MemoryStream())
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = _bucketName,
Key = doc.ObjectRefId.ToString("D")
};
using (GetObjectResponse response = await _s3Client.GetObjectAsync(request))
{
using (Stream responseStream = response.ResponseStream)
{
responseStream.CopyTo(memoryStream);
}
}
var fileContent = memoryStream.ToArray();
return new FileDownloadResponse
{
FileName = doc.FileName,
FileType = doc.FileType,
FileContent = fileContent
};
};
}
catch
{
throw;
}
}
public async Task DeleteFileAsync(Guid fileId)
{
try
{
var doc = await _dbContext.Set<Document>().AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
else
{
DeleteObjectRequest request = new DeleteObjectRequest
{
BucketName = _bucketName,
Key = doc?.ObjectRefId.ToString("D")
};
// delete from minio
await _s3Client.DeleteObjectAsync(request);
_dbContext.Set<Document>().Remove(doc);
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
public async Task<string> ImagesPath(Guid? fileId)
{
if (fileId == null)
return "";
var doc = await _dbContext.Set<Document>().AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
var config = new AmazonS3Config
{
ServiceURL = _configuration["MinIO:Endpoint"],
ForcePathStyle = true
};
DateTime expires = DateTime.UtcNow.AddHours(6);
var _protocol = _configuration["Protocol"];
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = _bucketName,
Key = doc?.ObjectRefId.ToString("D"),
Expires = expires,
Protocol = _protocol == "HTTPS" ? Protocol.HTTPS : Protocol.HTTP
};
string path = _s3Client.GetPreSignedURL(request);
return path;
}
public async Task<string> ImagesPathByName(string fileName)
{
var config = new AmazonS3Config
{
ServiceURL = _configuration["MinIO:Endpoint"],
ForcePathStyle = true
};
DateTime expires = DateTime.UtcNow.AddHours(6);
var _protocol = _configuration["Protocol"];
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = _bucketName,
Key = fileName,
Expires = expires,
Protocol = _protocol == "HTTPS" ? Protocol.HTTPS : Protocol.HTTP
};
string path = _s3Client.GetPreSignedURL(request);
return path;
}
#endregion
public List<Guid> GetAllIdByRoot(Guid? id)
{
try
{
var ret = new List<Guid>();
if (id == null)
return ret;
var oc = _dbContext.Set<OrganizationEntity>().FirstOrDefault(x => x.Id == id);
if (oc != null)
ret.Add(oc.Id);
var child = _dbContext.Set<OrganizationEntity>().AsQueryable().Where(x => x.Parent != null && x.Parent.Id == id).ToList();
if (child.Any())
{
foreach (var item in child)
{
ret.AddRange(GetAllIdByRoot(item.Id));
}
}
return ret;
}
catch
{
throw;
}
}
public async Task<string?> CheckBmaOfficer(string CitizenId)
{
var data = await _dbContext.Set<Profile>().FirstOrDefaultAsync(x => x.CitizenId == CitizenId);
if (data == null)
return null;
if (data.ProfileType.Trim().ToUpper() == "OFFICER")
return "OFFICER";
if (data.EmployeeClass.Trim().ToUpper() == "PERM")
return "EMPLOYEE_PERM";
if (data.EmployeeClass.Trim().ToUpper() == "TEMP")
return "EMPLOYEE_TEMP";
return "EMPLOYEE";
}
public async Task UploadFileAsyncTemp(string fileName, string subFolder)
{
try
{
var fileContents = File.ReadAllBytes(fileName);
System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileContents);
//var fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
var fileExt = Path.GetExtension(fileName);
var fileType = MimeTypeMap.GetMimeType(fileExt);
var file_name = Path.GetFileName(fileName);
Console.WriteLine($"{_bucketName}{subFolder}");
Console.WriteLine(fileName);
Console.WriteLine(file_name);
Console.WriteLine(filestream);
Console.WriteLine(fileType);
var request = new PutObjectRequest
{
BucketName = $"{_bucketName}",
// BucketName = $"{_bucketName}{subFolder}",
Key = file_name,
InputStream = filestream,
ContentType = fileType,
CannedACL = S3CannedACL.PublicRead
};
await _s3Client.PutObjectAsync(request);
}
catch
{
throw;
}
}
public async Task GenerateJsonFile(string json, string path, string fileName)
{
var tmpDir = Path.Combine("tmp");
if (!Directory.Exists(tmpDir))
Directory.CreateDirectory(tmpDir);
var tmpFile = Path.Combine(tmpDir, $"{fileName}.json");
try
{
SaveToJsonFile(tmpFile, json);
await UploadFileAsyncTemp(tmpFile, path);
}
catch
{
throw;
}
finally
{
if (tmpFile != "")
{
if (System.IO.File.Exists(tmpFile))
System.IO.File.Delete(tmpFile);
}
}
}
private void SaveToJsonFile(string fileName, string data)
{
TextWriter writer = null;
try
{
writer = new StreamWriter(fileName);
writer.Write(data);
}
catch
{
throw;
}
finally
{
if (writer != null)
writer.Close();
}
}
}
}

View file

@ -1,365 +1,365 @@
using Amazon.S3;
using Amazon.S3.Model;
using BMA.EHR.Domain.Models.Documents;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MimeTypes;
using System.Net.Http.Headers;
// using Amazon.S3;
// using Amazon.S3.Model;
// using BMA.EHR.Domain.Models.Documents;
// using BMA.EHR.Domain.Shared;
// using BMA.EHR.Infrastructure.Persistence;
// using Microsoft.AspNetCore.Hosting;
// using Microsoft.AspNetCore.Http;
// using Microsoft.EntityFrameworkCore;
// using Microsoft.Extensions.Configuration;
// using MimeTypes;
// using System.Net.Http.Headers;
namespace BMA.EHR.Application.Repositories
{
public class MinIOService
{
#region " Fields "
// namespace BMA.EHR.Application.Repositories
// {
// public class MinIOService
// {
// #region " Fields "
private readonly ApplicationDBContext _context;
private readonly IConfiguration _configuration;
private readonly AmazonS3Client _s3Client;
private readonly IWebHostEnvironment _hostingEnvironment;
private string _bucketName = string.Empty;
// private readonly ApplicationDBContext _context;
// private readonly IConfiguration _configuration;
// private readonly AmazonS3Client _s3Client;
// private readonly IWebHostEnvironment _hostingEnvironment;
// private string _bucketName = string.Empty;
#endregion
// #endregion
#region " Constructors "
// #region " Constructors "
public MinIOService(ApplicationDBContext context,
IConfiguration configuration,
IWebHostEnvironment hostingEnvironment)
{
_context = context;
_configuration = configuration;
_hostingEnvironment = hostingEnvironment;
// public MinIOService(ApplicationDBContext context,
// IConfiguration configuration,
// IWebHostEnvironment hostingEnvironment)
// {
// _context = context;
// _configuration = configuration;
// _hostingEnvironment = hostingEnvironment;
var config = new AmazonS3Config
{
ServiceURL = _configuration["MinIO:Endpoint"],
ForcePathStyle = true
};
// var config = new AmazonS3Config
// {
// ServiceURL = _configuration["MinIO:Endpoint"],
// ForcePathStyle = true
// };
_s3Client = new AmazonS3Client(_configuration["MinIO:AccessKey"], _configuration["MinIO:SecretKey"], config);
this._bucketName = _configuration["MinIO:BucketName"] ?? "bma-recruit";
}
// _s3Client = new AmazonS3Client(_configuration["MinIO:AccessKey"], _configuration["MinIO:SecretKey"], config);
// this._bucketName = _configuration["MinIO:BucketName"] ?? "bma-recruit";
// }
#endregion
// #endregion
#region " Methods "
// #region " Methods "
public async Task<Document> UploadFileAsync(IFormFile file, string newFileName = "")
{
var fileName = "";
var fileExt = Path.GetExtension(file.FileName);
if (newFileName != "")
fileName = $"{newFileName}";
else
fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
// public async Task<Document> UploadFileAsync(IFormFile file, string newFileName = "")
// {
// var fileName = "";
// var fileExt = Path.GetExtension(file.FileName);
// if (newFileName != "")
// fileName = $"{newFileName}";
// else
// fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var tmpDir = Path.Combine("tmp");
if (!Directory.Exists(tmpDir))
Directory.CreateDirectory(tmpDir);
// var tmpDir = Path.Combine("tmp");
// if (!Directory.Exists(tmpDir))
// Directory.CreateDirectory(tmpDir);
var tmpFile = Path.Combine(tmpDir, $"tmp_{DateTime.Now.ToString("ddMMyyyyHHmmss")}{fileExt}");
// var tmpFile = Path.Combine(tmpDir, $"tmp_{DateTime.Now.ToString("ddMMyyyyHHmmss")}{fileExt}");
try
{
using (var ms = new MemoryStream())
{
var id = Guid.NewGuid();
file.CopyTo(ms);
var fileBytes = ms.ToArray();
System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileBytes);
// try
// {
// using (var ms = new MemoryStream())
// {
// var id = Guid.NewGuid();
// file.CopyTo(ms);
// var fileBytes = ms.ToArray();
// System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileBytes);
var request = new PutObjectRequest
{
BucketName = _bucketName,
Key = id.ToString("D"),
InputStream = filestream,
ContentType = file.ContentType,
CannedACL = S3CannedACL.PublicRead
};
// var request = new PutObjectRequest
// {
// BucketName = _bucketName,
// Key = id.ToString("D"),
// InputStream = filestream,
// ContentType = file.ContentType,
// CannedACL = S3CannedACL.PublicRead
// };
await _s3Client.PutObjectAsync(request);
// await _s3Client.PutObjectAsync(request);
// create document object
var doc = new Document()
{
FileName = fileName,
FileType = file.ContentType,
FileSize = Convert.ToInt32(file.Length),
ObjectRefId = id,
CreatedDate = DateTime.Now
};
// // create document object
// var doc = new Document()
// {
// FileName = fileName,
// FileType = file.ContentType,
// FileSize = Convert.ToInt32(file.Length),
// ObjectRefId = id,
// CreatedDate = DateTime.Now
// };
await _context.Documents.AddAsync(doc);
await _context.SaveChangesAsync();
// await _context.Documents.AddAsync(doc);
// await _context.SaveChangesAsync();
return doc;
}
}
catch
{
throw;
}
finally
{
File.Delete(tmpFile);
}
}
// return doc;
// }
// }
// catch
// {
// throw;
// }
// finally
// {
// File.Delete(tmpFile);
// }
// }
public async Task<FileDownloadResponse> DownloadFileAsync(Guid fileId)
{
try
{
var doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
// public async Task<FileDownloadResponse> DownloadFileAsync(Guid fileId)
// {
// try
// {
// var doc = await _context.Documents.AsQueryable()
// .FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
// if (doc == null)
// throw new Exception(GlobalMessages.FileNotFoundOnServer);
using (var memoryStream = new MemoryStream())
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = _bucketName,
Key = doc.ObjectRefId.ToString("D")
};
// using (var memoryStream = new MemoryStream())
// {
// GetObjectRequest request = new GetObjectRequest
// {
// BucketName = _bucketName,
// Key = doc.ObjectRefId.ToString("D")
// };
using (GetObjectResponse response = await _s3Client.GetObjectAsync(request))
{
using (Stream responseStream = response.ResponseStream)
{
responseStream.CopyTo(memoryStream);
}
}
// using (GetObjectResponse response = await _s3Client.GetObjectAsync(request))
// {
// using (Stream responseStream = response.ResponseStream)
// {
// responseStream.CopyTo(memoryStream);
// }
// }
var fileContent = memoryStream.ToArray();
// var fileContent = memoryStream.ToArray();
return new FileDownloadResponse
{
FileName = doc.FileName,
FileType = doc.FileType,
FileContent = fileContent
};
};
}
catch
{
throw;
}
}
// return new FileDownloadResponse
// {
// FileName = doc.FileName,
// FileType = doc.FileType,
// FileContent = fileContent
// };
// };
// }
// catch
// {
// throw;
// }
// }
public async Task DeleteFileAsync(Guid fileId)
{
try
{
var doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
// public async Task DeleteFileAsync(Guid fileId)
// {
// try
// {
// var doc = await _context.Documents.AsQueryable()
// .FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
else
{
DeleteObjectRequest request = new DeleteObjectRequest
{
BucketName = _bucketName,
Key = doc?.ObjectRefId.ToString("D")
};
// if (doc == null)
// throw new Exception(GlobalMessages.FileNotFoundOnServer);
// else
// {
// DeleteObjectRequest request = new DeleteObjectRequest
// {
// BucketName = _bucketName,
// Key = doc?.ObjectRefId.ToString("D")
// };
// delete from minio
await _s3Client.DeleteObjectAsync(request);
// // delete from minio
// await _s3Client.DeleteObjectAsync(request);
_context.Documents.Remove(doc);
await _context.SaveChangesAsync();
}
}
catch
{
throw;
}
}
// _context.Documents.Remove(doc);
// await _context.SaveChangesAsync();
// }
// }
// catch
// {
// throw;
// }
// }
public async Task<string> ImagesPath(Guid? fileId)
{
if (fileId == null)
return "";
// public async Task<string> ImagesPath(Guid? fileId)
// {
// if (fileId == null)
// return "";
var doc = await _context.Documents.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == fileId);
// var doc = await _context.Documents.AsQueryable()
// .FirstOrDefaultAsync(x => x.Id == fileId);
if (doc == null)
throw new Exception(GlobalMessages.FileNotFoundOnServer);
var config = new AmazonS3Config
{
ServiceURL = _configuration["MinIO:Endpoint"],
ForcePathStyle = true
};
// if (doc == null)
// throw new Exception(GlobalMessages.FileNotFoundOnServer);
// var config = new AmazonS3Config
// {
// ServiceURL = _configuration["MinIO:Endpoint"],
// ForcePathStyle = true
// };
DateTime expires = DateTime.UtcNow.AddHours(6);
var _protocol = _configuration["Protocol"];
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = _bucketName,
Key = doc?.ObjectRefId.ToString("D"),
Expires = expires,
Protocol = _protocol == "HTTPS" ? Protocol.HTTPS : Protocol.HTTP
};
string path = _s3Client.GetPreSignedURL(request);
// DateTime expires = DateTime.UtcNow.AddHours(6);
// var _protocol = _configuration["Protocol"];
// GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
// {
// BucketName = _bucketName,
// Key = doc?.ObjectRefId.ToString("D"),
// Expires = expires,
// Protocol = _protocol == "HTTPS" ? Protocol.HTTPS : Protocol.HTTP
// };
// string path = _s3Client.GetPreSignedURL(request);
return path;
}
// return path;
// }
public async Task<string> ImagesPathByName(string fileName)
{
var config = new AmazonS3Config
{
ServiceURL = _configuration["MinIO:Endpoint"],
ForcePathStyle = true
};
// public async Task<string> ImagesPathByName(string fileName)
// {
// var config = new AmazonS3Config
// {
// ServiceURL = _configuration["MinIO:Endpoint"],
// ForcePathStyle = true
// };
DateTime expires = DateTime.UtcNow.AddHours(6);
var _protocol = _configuration["Protocol"];
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = _bucketName,
Key = fileName,
Expires = expires,
Protocol = _protocol == "HTTPS" ? Protocol.HTTPS : Protocol.HTTP
};
string path = _s3Client.GetPreSignedURL(request);
// DateTime expires = DateTime.UtcNow.AddHours(6);
// var _protocol = _configuration["Protocol"];
// GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
// {
// BucketName = _bucketName,
// Key = fileName,
// Expires = expires,
// Protocol = _protocol == "HTTPS" ? Protocol.HTTPS : Protocol.HTTP
// };
// string path = _s3Client.GetPreSignedURL(request);
return path;
}
// return path;
// }
#endregion
// #endregion
public List<Guid> GetAllIdByRoot(Guid? id)
{
try
{
var ret = new List<Guid>();
if (id == null)
return ret;
// public List<Guid> GetAllIdByRoot(Guid? id)
// {
// try
// {
// var ret = new List<Guid>();
// if (id == null)
// return ret;
var oc = _context.Organizations.FirstOrDefault(x => x.Id == id);
if (oc != null)
ret.Add(oc.Id);
// var oc = _context.Organizations.FirstOrDefault(x => x.Id == id);
// if (oc != null)
// ret.Add(oc.Id);
var child = _context.Organizations.AsQueryable().Where(x => x.Parent != null && x.Parent.Id == id).ToList();
if (child.Any())
{
foreach (var item in child)
{
ret.AddRange(GetAllIdByRoot(item.Id));
}
}
// var child = _context.Organizations.AsQueryable().Where(x => x.Parent != null && x.Parent.Id == id).ToList();
// if (child.Any())
// {
// foreach (var item in child)
// {
// ret.AddRange(GetAllIdByRoot(item.Id));
// }
// }
return ret;
}
catch
{
throw;
}
}
// return ret;
// }
// catch
// {
// throw;
// }
// }
public async Task<string?> CheckBmaOfficer(string CitizenId)
{
var data = await _context.Profiles.FirstOrDefaultAsync(x => x.CitizenId == CitizenId);
if (data == null)
return null;
if (data.ProfileType.Trim().ToUpper() == "OFFICER")
return "OFFICER";
if (data.EmployeeClass.Trim().ToUpper() == "PERM")
return "EMPLOYEE_PERM";
if (data.EmployeeClass.Trim().ToUpper() == "TEMP")
return "EMPLOYEE_TEMP";
return "EMPLOYEE";
}
// public async Task<string?> CheckBmaOfficer(string CitizenId)
// {
// var data = await _context.Profiles.FirstOrDefaultAsync(x => x.CitizenId == CitizenId);
// if (data == null)
// return null;
// if (data.ProfileType.Trim().ToUpper() == "OFFICER")
// return "OFFICER";
// if (data.EmployeeClass.Trim().ToUpper() == "PERM")
// return "EMPLOYEE_PERM";
// if (data.EmployeeClass.Trim().ToUpper() == "TEMP")
// return "EMPLOYEE_TEMP";
// return "EMPLOYEE";
// }
public async Task UploadFileAsyncTemp(string fileName, string subFolder)
{
try
{
var fileContents = File.ReadAllBytes(fileName);
System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileContents);
//var fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
var fileExt = Path.GetExtension(fileName);
var fileType = MimeTypeMap.GetMimeType(fileExt);
var file_name = Path.GetFileName(fileName);
Console.WriteLine($"{_bucketName}{subFolder}");
Console.WriteLine(fileName);
Console.WriteLine(file_name);
Console.WriteLine(filestream);
Console.WriteLine(fileType);
// public async Task UploadFileAsyncTemp(string fileName, string subFolder)
// {
// try
// {
// var fileContents = File.ReadAllBytes(fileName);
// System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileContents);
// //var fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
// var fileExt = Path.GetExtension(fileName);
// var fileType = MimeTypeMap.GetMimeType(fileExt);
// var file_name = Path.GetFileName(fileName);
// Console.WriteLine($"{_bucketName}{subFolder}");
// Console.WriteLine(fileName);
// Console.WriteLine(file_name);
// Console.WriteLine(filestream);
// Console.WriteLine(fileType);
var request = new PutObjectRequest
{
BucketName = $"{_bucketName}",
// BucketName = $"{_bucketName}{subFolder}",
Key = file_name,
InputStream = filestream,
ContentType = fileType,
CannedACL = S3CannedACL.PublicRead
};
// var request = new PutObjectRequest
// {
// BucketName = $"{_bucketName}",
// // BucketName = $"{_bucketName}{subFolder}",
// Key = file_name,
// InputStream = filestream,
// ContentType = fileType,
// CannedACL = S3CannedACL.PublicRead
// };
await _s3Client.PutObjectAsync(request);
}
catch
{
throw;
}
}
// await _s3Client.PutObjectAsync(request);
// }
// catch
// {
// throw;
// }
// }
public async Task GenerateJsonFile(string json, string path, string fileName)
{
var tmpDir = Path.Combine("tmp");
if (!Directory.Exists(tmpDir))
Directory.CreateDirectory(tmpDir);
// public async Task GenerateJsonFile(string json, string path, string fileName)
// {
// var tmpDir = Path.Combine("tmp");
// if (!Directory.Exists(tmpDir))
// Directory.CreateDirectory(tmpDir);
var tmpFile = Path.Combine(tmpDir, $"{fileName}.json");
// var tmpFile = Path.Combine(tmpDir, $"{fileName}.json");
try
{
SaveToJsonFile(tmpFile, json);
await UploadFileAsyncTemp(tmpFile, path);
}
catch
{
throw;
}
finally
{
if (tmpFile != "")
{
if (System.IO.File.Exists(tmpFile))
System.IO.File.Delete(tmpFile);
}
// try
// {
// SaveToJsonFile(tmpFile, json);
// await UploadFileAsyncTemp(tmpFile, path);
// }
// catch
// {
// throw;
// }
// finally
// {
// if (tmpFile != "")
// {
// if (System.IO.File.Exists(tmpFile))
// System.IO.File.Delete(tmpFile);
// }
}
}
// }
// }
private void SaveToJsonFile(string fileName, string data)
{
TextWriter writer = null;
try
{
writer = new StreamWriter(fileName);
writer.Write(data);
}
catch
{
throw;
}
finally
{
if (writer != null)
writer.Close();
}
}
// private void SaveToJsonFile(string fileName, string data)
// {
// TextWriter writer = null;
// try
// {
// writer = new StreamWriter(fileName);
// writer.Write(data);
// }
// catch
// {
// throw;
// }
// finally
// {
// if (writer != null)
// writer.Close();
// }
// }
}
}
// }
// }

View file

@ -508,6 +508,13 @@ namespace BMA.EHR.Retirement.Service.Controllers
"",
true
);
await _repositoryNoti.PushNotificationAsync(
Guid.Parse("08db721d-ae22-424d-8f4a-87ba30cc3ee7"),
$"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ได้รับการอนุมัติจากผู้บังคับบัญชา",
$"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ได้รับการอนุมัติจากผู้บังคับบัญชา",
"",
true
);
await _context.SaveChangesAsync();
return Success();
@ -547,6 +554,13 @@ namespace BMA.EHR.Retirement.Service.Controllers
"",
true
);
await _repositoryNoti.PushNotificationAsync(
Guid.Parse("08db721d-ae22-424d-8f4a-87ba30cc3ee7"),
$"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ถูกยับยั้งจากผู้บังคับบัญชา",
$"คำขอลาออกขอ {updated.Profile.Prefix?.Name}{updated.Profile.FirstName} {updated.Profile.LastName} ถูกยับยั้งจากผู้บังคับบัญชา",
"",
true
);
await _context.SaveChangesAsync();
return Success();