fix 代码合并,minio上传文件
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Dlhis.Common.FileOperation.Services;
|
||||
using Dlhis.Common.Request;
|
||||
using Dlhis.Entity.Common;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Services.Implements;
|
||||
|
||||
public class ApkFIileService : IApkFIileService
|
||||
{
|
||||
private readonly IMinioService _minioService;
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public ApkFIileService(IMinioService minioService, IDbContext dbContext)
|
||||
{
|
||||
_minioService = minioService;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件上传
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApkFileResponse> FormSingleFileUpload(ApkRequest request)
|
||||
{
|
||||
//上传
|
||||
var fileUrl = await _minioService.MinloUploadFileAsync(request);
|
||||
|
||||
string fileName = request.formFile.FileName;
|
||||
|
||||
//更新数据库
|
||||
var entity = await _dbContext.Queryable<ApkFileEntity>().Where(x => x.Name == fileName).FirstAsync();
|
||||
if (entity == null)
|
||||
{
|
||||
var info = new ApkFileEntity
|
||||
{
|
||||
Name = fileName,
|
||||
Url = fileUrl,
|
||||
VersionCode = request.VersionCode,
|
||||
UpdateMessage = request.UpdateMessage,
|
||||
};
|
||||
await _dbContext.InsertAsync(info);
|
||||
}
|
||||
else
|
||||
{
|
||||
entity.Url = fileUrl;
|
||||
entity.VersionCode = request.VersionCode;
|
||||
entity.UpdateMessage = request.UpdateMessage;
|
||||
await _dbContext.UpdateAsync(entity);
|
||||
}
|
||||
var newEntity = await _dbContext.Queryable<ApkFileEntity>().Where(x => x.Name == fileName).FirstAsync();
|
||||
var res = newEntity.Adapt<ApkFileResponse>();
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件下载
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<string> RequestDownloadFile(string fileName)
|
||||
{
|
||||
return await _minioService.MinloDownloadLinkAsync(fileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本地文件上传
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApkFileResponse> FormLocalFileUpload(ApkRequest request)
|
||||
{
|
||||
//上传
|
||||
var completeFilePath = FormSingleFileUpload(request.formFile);
|
||||
if (completeFilePath == "No file uploaded" || completeFilePath.IsEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
//更新数据库
|
||||
var fileName = request.formFile.FileName;
|
||||
var entity = await _dbContext.Queryable<ApkFileEntity>().Where(x => x.Name == fileName).FirstAsync();
|
||||
if (entity == null)
|
||||
{
|
||||
var info = new ApkFileEntity
|
||||
{
|
||||
Name = fileName,
|
||||
Url = completeFilePath,
|
||||
VersionCode = request.VersionCode,
|
||||
UpdateMessage = request.UpdateMessage,
|
||||
};
|
||||
await _dbContext.InsertAsync(info);
|
||||
}
|
||||
else
|
||||
{
|
||||
entity.Url = completeFilePath;
|
||||
entity.VersionCode = request.VersionCode;
|
||||
entity.UpdateMessage = request.UpdateMessage;
|
||||
await _dbContext.UpdateAsync(entity);
|
||||
}
|
||||
var newEntity = await _dbContext.Queryable<ApkFileEntity>().Where(x => x.Name == fileName).FirstAsync();
|
||||
var res = newEntity.Adapt<ApkFileResponse>();
|
||||
return res;
|
||||
|
||||
}
|
||||
public string FormSingleFileUpload(IFormFile formFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filePath = "D:\\apk\\";
|
||||
|
||||
// 创建存储文件夹
|
||||
if (!Directory.Exists(filePath))
|
||||
{
|
||||
Directory.CreateDirectory(filePath);
|
||||
}
|
||||
|
||||
if (formFile != null && formFile.Length > 0)
|
||||
{
|
||||
// 保存的文件名称
|
||||
var saveName = Path.GetFileName(formFile.FileName);
|
||||
|
||||
// 文件保存
|
||||
using (var fs = File.Create(Path.Combine(filePath, saveName)))
|
||||
{
|
||||
formFile.CopyTo(fs);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
// 完整的文件路径
|
||||
var completeFilePath = Path.Combine(filePath, saveName);
|
||||
return completeFilePath;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "No file uploaded";
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 本地下载
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public IActionResult FormLocalDownloadFile(string filePath)
|
||||
{
|
||||
string fileName = Path.GetFileName(filePath);
|
||||
return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
using Dlhis.Common.FileOperation.Configurations;
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.Request;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Minio;
|
||||
using Minio.DataModel.Args;
|
||||
using Minio.Exceptions;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Services.Implements;
|
||||
|
||||
public class MinioService : IMinioService
|
||||
{
|
||||
private readonly IMinioClient _client;
|
||||
private readonly MinIOOptions _minIOOptions;
|
||||
|
||||
public MinioService(IMinioClient client, IOptions<MinIOOptions> options)
|
||||
{
|
||||
_client = client;
|
||||
_minIOOptions = options.Value;
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsBucketAsync(string bucket)
|
||||
{
|
||||
var args = new BucketExistsArgs().WithBucket(bucket);
|
||||
return await _client.BucketExistsAsync(args);
|
||||
}
|
||||
|
||||
|
||||
public async Task CreateBucketIfNotExistsAsync(string bucket)
|
||||
{
|
||||
if (!await ExistsBucketAsync(bucket))
|
||||
{
|
||||
var policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME/*\"]}]}".Replace("BUCKETNAME", bucket);
|
||||
|
||||
await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucket));
|
||||
await _client.SetPolicyAsync(new SetPolicyArgs().WithBucket(bucket).WithPolicy(policy));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> ExistsFileAsync(string bucket, string objectName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var obj = new StatObjectArgs().WithBucket(bucket).WithObject(objectName);
|
||||
var objStat = await _client.StatObjectAsync(obj);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<string> UploadFileAsync(string bucket, IFormFile formFile)
|
||||
{
|
||||
await CreateBucketIfNotExistsAsync(bucket);
|
||||
|
||||
var ms = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||||
var fileName = Path.GetFileNameWithoutExtension(formFile.FileName) + "_" + ms + Path.GetExtension(formFile.FileName);
|
||||
fileName = $"{DateTime.Now.Year}/{DateTime.Now.Month}/{fileName}";
|
||||
|
||||
var response = await _client.PutObjectAsync(new PutObjectArgs().WithBucket(bucket)
|
||||
.WithObject(fileName)
|
||||
.WithObjectSize(formFile.Length)
|
||||
.WithStreamData(formFile.OpenReadStream())
|
||||
.WithContentType(formFile.ContentType)
|
||||
.WithVersionId(bucket)
|
||||
);
|
||||
|
||||
return $"/{bucket}/{response.ObjectName}";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> MinloUploadFileAsync(ApkRequest request)
|
||||
{
|
||||
string fileName = request.formFile.FileName;
|
||||
|
||||
using (MemoryStream filestream = new MemoryStream())
|
||||
{
|
||||
await request.formFile.CopyToAsync(filestream);
|
||||
filestream.Position = 0;
|
||||
|
||||
var res = await _client.PutObjectAsync(new PutObjectArgs()
|
||||
.WithBucket(_minIOOptions.DefaultBucket)
|
||||
.WithObject(fileName)
|
||||
.WithStreamData(filestream)
|
||||
.WithObjectSize(filestream.Length));
|
||||
}
|
||||
var filerl = await MinloDownloadLinkAsync(fileName);
|
||||
return filerl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件下载链接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<string> MinloDownloadLinkAsync(string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = await _client.PresignedGetObjectAsync(new PresignedGetObjectArgs()
|
||||
.WithBucket(_minIOOptions.DefaultBucket)
|
||||
.WithObject(fileName)
|
||||
.WithExpiry(60 * 60 * 24 * 7));//链接时效
|
||||
return url;
|
||||
}
|
||||
catch (MinioException e)
|
||||
{
|
||||
Console.Out.WriteLine("Error occurred: " + e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 单个删除文件
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public async Task MinloRemoveFileAsync(string fileName)
|
||||
{
|
||||
await _client.RemoveObjectAsync(new RemoveObjectArgs()
|
||||
.WithBucket(_minIOOptions.DefaultBucket)
|
||||
.WithObject(fileName));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user