apu upload file
This commit is contained in:
parent
08ff366b4c
commit
2f49a5b87e
13 changed files with 301 additions and 254 deletions
|
|
@ -7,6 +7,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using BMA.EHR.Application.Common.Interfaces;
|
||||
using BMA.EHR.Application.Repositories;
|
||||
using BMA.EHR.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
|
@ -10,8 +11,10 @@ namespace BMA.EHR.Infrastructure
|
|||
{
|
||||
public static IServiceCollection AddPersistence(this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||
{
|
||||
services.AddTransient<MinIOService>();
|
||||
|
||||
var connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||
|
||||
services.AddDbContext<ApplicationDBContext>(options =>
|
||||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString),
|
||||
|
|
|
|||
214
BMA.EHR.Infrastructure/Storage/MinIOService.cs
Normal file
214
BMA.EHR.Infrastructure/Storage/MinIOService.cs
Normal file
|
|
@ -0,0 +1,214 @@
|
|||
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.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Net.Http.Headers;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
namespace BMA.EHR.Application.Repositories
|
||||
{
|
||||
public class MinIOService
|
||||
{
|
||||
#region " Fields "
|
||||
|
||||
private readonly ApplicationDBContext _context;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly AmazonS3Client _s3Client;
|
||||
private string _bucketName = string.Empty;
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Constructors "
|
||||
|
||||
public MinIOService(ApplicationDBContext context,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_context = context;
|
||||
_configuration = configuration;
|
||||
|
||||
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 _context.Documents.AddAsync(doc);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
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);
|
||||
|
||||
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 _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")
|
||||
};
|
||||
|
||||
// delete from minio
|
||||
await _s3Client.DeleteObjectAsync(request);
|
||||
|
||||
|
||||
_context.Documents.Remove(doc);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> ImagesPath(Guid? fileId)
|
||||
{
|
||||
if (fileId == null)
|
||||
return "";
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue