refactor 文件版本重构
This commit is contained in:
@@ -11,9 +11,13 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="log\**" />
|
||||
<Compile Remove="Response\**" />
|
||||
<Content Remove="log\**" />
|
||||
<Content Remove="Response\**" />
|
||||
<EmbeddedResource Remove="log\**" />
|
||||
<EmbeddedResource Remove="Response\**" />
|
||||
<None Remove="log\**" />
|
||||
<None Remove="Response\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -25,8 +29,4 @@
|
||||
<PackageReference Include="Minio" Version="6.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Response\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.CustomClass;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -46,7 +47,7 @@ public class EnumsController : ControllerBase
|
||||
{
|
||||
var e = (Enum)Enum.Parse(item.FieldType, item.GetValue(null).ToString());
|
||||
var fd = e.GetDescription();
|
||||
return new KeyValueDto
|
||||
return new KeyValueDto<string, object>
|
||||
{
|
||||
Key = fd,
|
||||
Value = (int)item.GetValue(null)
|
||||
@@ -66,7 +67,7 @@ public class EnumsController : ControllerBase
|
||||
{
|
||||
Code = type,
|
||||
Name = type,
|
||||
Data = g.AsEnumerable().Select(x => new KeyValueDto
|
||||
Data = g.AsEnumerable().Select(x => new KeyValueDto<string, object>
|
||||
{
|
||||
Key = x.Display,
|
||||
Value = x.Code
|
||||
@@ -94,7 +95,7 @@ public class EnumsController : ControllerBase
|
||||
{
|
||||
var e = (Enum)Enum.Parse(item.FieldType, item.GetValue(null).ToString());
|
||||
var fd = e.GetDescription();
|
||||
return new KeyValueDto
|
||||
return new KeyValueDto<string, object>
|
||||
{
|
||||
Key = fd,
|
||||
Value = (int)item.GetValue(null)
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Dlhis.Common.FileOperation.Constants;
|
||||
|
||||
public static class FileBucketConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件buckets
|
||||
/// </summary>
|
||||
public static List<string> Buckets { get; } = new()
|
||||
{
|
||||
"restaurant",
|
||||
"apk"
|
||||
};
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Dlhis.Common.FileOperation.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// APK文件操作
|
||||
/// </summary>
|
||||
[Obsolete("作废重写")]
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ApkFileController : ControllerBase
|
||||
{
|
||||
private readonly IApkFIileService _apkFIileService;
|
||||
|
||||
public ApkFileController(IApkFIileService apkFIileService)
|
||||
{
|
||||
_apkFIileService = apkFIileService;
|
||||
}
|
||||
/// <summary>
|
||||
/// minlo文件上传
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("minio")]
|
||||
[RequestSizeLimit(300_000_000)]
|
||||
public async Task<ApkFileResponse> FormSingleFileUpload([FromForm] ApkRequest request)
|
||||
{
|
||||
return await _apkFIileService.FormSingleFileUpload(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// minlo文件下载
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("url")]
|
||||
public async Task<string> RequestDownloadFile(string fileName)
|
||||
{
|
||||
return await _apkFIileService.RequestDownloadFile(fileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取最新版本
|
||||
/// </summary>
|
||||
/// <param name="productCode"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("lastversion")]
|
||||
public async Task<ApkFileResponse> GetLastVersionAsync(string productCode)
|
||||
{
|
||||
return await _apkFIileService.GetLastVersionAsync(productCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,20 +1,28 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Dlhis.Common.FileOperation.Services;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 文件操作
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class FileController : ControllerBase
|
||||
{
|
||||
private readonly IMinioService _minioService;
|
||||
private readonly IFileVersionService _fileVersionService;
|
||||
|
||||
public FileController(IMinioService minioService)
|
||||
public FileController(IMinioService minioService, IFileVersionService fileVersionService)
|
||||
{
|
||||
_minioService = minioService;
|
||||
_fileVersionService = fileVersionService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -27,4 +35,50 @@ public class FileController : ControllerBase
|
||||
{
|
||||
return await _minioService.UploadFileAsync(request.Bucket, request.File);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文件上传(版本)
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("{Bucket}/version/upload")]
|
||||
[RequestSizeLimit(300_000_000)]
|
||||
public async Task<string> UploadFileAsync([FromForm] FileVersionUploadRequest request)
|
||||
{
|
||||
return await _fileVersionService.UploadFileAsync(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件版本分页
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("version/page")]
|
||||
public async Task<Pagination<FileVersionResponse>> GetPageAsync([FromQuery] PageDto request)
|
||||
{
|
||||
return await _fileVersionService.GetPageAsync(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件版本列表
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("version/list")]
|
||||
public async Task<List<FileVersionResponse>> GetListByCodeAsync([Required] string code)
|
||||
{
|
||||
return await _fileVersionService.GetListByCodeAsync(code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件版本最新
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("version/last")]
|
||||
public async Task<FileVersionResponse> GetLastByCodeAsync([Required] string code)
|
||||
{
|
||||
return await _fileVersionService.GetLastByCodeAsync(code);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
namespace Dlhis.Common.FileOperation.Request
|
||||
{
|
||||
public class ApkRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件
|
||||
/// </summary>
|
||||
public IFormFile formFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
public int VersionCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本名称
|
||||
/// </summary>
|
||||
public string VersionName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改信息
|
||||
/// </summary>
|
||||
public string UpdateMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 产品代码
|
||||
/// </summary>
|
||||
public string ProductCode { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Dlhis.Common.FileOperation.Constants;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Request;
|
||||
|
||||
public class FileVersionUploadRequest : IValidatableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// bucket
|
||||
/// </summary>
|
||||
[FromRoute]
|
||||
public string Bucket { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件
|
||||
/// </summary>
|
||||
[Required]
|
||||
public IFormFile File { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件名称
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// code
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述信息
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if (!FileBucketConstants.Buckets.Contains(Bucket))
|
||||
{
|
||||
yield return new ValidationResult($"不支持{Bucket}", new[] { nameof(Bucket) });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Dlhis.Common.FileOperation.Constants;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Request;
|
||||
|
||||
public class UploadFileReuqest
|
||||
public class UploadFileReuqest : IValidatableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// bucket
|
||||
@@ -14,4 +16,12 @@ public class UploadFileReuqest
|
||||
/// 文件
|
||||
/// </summary>
|
||||
public IFormFile File { get; set; }
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if (!FileBucketConstants.Buckets.Contains(Bucket))
|
||||
{
|
||||
yield return new ValidationResult($"不支持{Bucket}", new[] { nameof(Bucket) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Response
|
||||
{
|
||||
public class ApkFile1Response
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 地址
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
public int VersionCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本名称
|
||||
/// </summary>
|
||||
public int VersionName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改信息
|
||||
/// </summary>
|
||||
public string UpdateMessage { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace Dlhis.Common.FileOperation.Response
|
||||
{
|
||||
public class ApkFileFormLocalResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
public int VersionCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本名称
|
||||
/// </summary>
|
||||
public int VersionName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改信息
|
||||
/// </summary>
|
||||
public string UpdateMessage { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Response
|
||||
{
|
||||
public class ApkFileResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
public int VersionCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本名称
|
||||
/// </summary>
|
||||
public string VersionName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改信息
|
||||
/// </summary>
|
||||
public string UpdateMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 产品代码
|
||||
/// </summary>
|
||||
public string ProductCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// url
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
namespace Dlhis.Common.FileOperation.Response
|
||||
using Kurisu.Core.CustomClass;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Response;
|
||||
|
||||
public class CommonEnumResponse
|
||||
{
|
||||
public class CommonEnumResponse
|
||||
{
|
||||
public string Code { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public List<KeyValueDto> Data { get; set; }
|
||||
}
|
||||
|
||||
public List<KeyValueDto<string, object>> Data { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace Dlhis.Common.FileOperation.Response;
|
||||
|
||||
public class FileVersionResponse
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件code
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
public int Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述信息
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件地址
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace Dlhis.Common.FileOperation.Response;
|
||||
|
||||
public class KeyValueDto
|
||||
{
|
||||
public string Key { get; set; }
|
||||
|
||||
public object Value { get; set; }
|
||||
}
|
||||
@@ -1,188 +0,0 @@
|
||||
using Dlhis.Common.ApiLog.Response;
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Dlhis.Entity.Common;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Services;
|
||||
|
||||
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<ApkFile1Response> 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<ApkFile1Response>();
|
||||
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<ApkFileFormLocalResponse> 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,
|
||||
VersionName = request.VersionName,
|
||||
UpdateMessage = request.UpdateMessage,
|
||||
};
|
||||
await _dbContext.InsertAsync(info);
|
||||
}
|
||||
else
|
||||
{
|
||||
//entity.Url = completeFilePath;
|
||||
entity.VersionCode = request.VersionCode;
|
||||
entity.VersionName = request.VersionName;
|
||||
entity.UpdateMessage = request.UpdateMessage;
|
||||
await _dbContext.UpdateAsync(entity);
|
||||
}
|
||||
var newEntity = await _dbContext.Queryable<ApkFileEntity>().Where(x => x.Name == fileName).FirstAsync();
|
||||
var res = newEntity.Adapt<ApkFileFormLocalResponse>();
|
||||
return res;
|
||||
|
||||
}
|
||||
public string FormSingleFileUpload(IFormFile formFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filePath = "C:\\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="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ApkFileFormLocalResponse> LatestFile(string fileName)
|
||||
{
|
||||
var info = await _dbContext.Queryable<ApkFileEntity>()
|
||||
.Where(x => x.Name == fileName)
|
||||
.OrderByDescending(x => x.VersionCode)
|
||||
.FirstAsync();
|
||||
info.ThrowIfNull($"没有{fileName}信息");
|
||||
var res = info.Adapt<ApkFileFormLocalResponse>();
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 本地下载
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public IActionResult FormLocalDownloadFile(string fileName)
|
||||
{
|
||||
var filePath = "C:\\apk\\";
|
||||
string path = filePath + fileName;
|
||||
return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
|
||||
}
|
||||
|
||||
Task<ApkFileResponse> IApkFIileService.FormSingleFileUpload(ApkRequest request)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<ApkFileResponse> GetLastVersionAsync(string productCode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Services;
|
||||
|
||||
public interface IApkFIileService : IScopeDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件上传
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
Task<ApkFileResponse> FormSingleFileUpload(ApkRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// 文件下载
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<string> RequestDownloadFile(string fileName);
|
||||
|
||||
|
||||
Task<ApkFileResponse> GetLastVersionAsync(string productCode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Services;
|
||||
|
||||
public interface IFileVersionService : IScopeDependency
|
||||
{
|
||||
Task<string> UploadFileAsync(FileVersionUploadRequest request);
|
||||
|
||||
Task<Pagination<FileVersionResponse>> GetPageAsync(PageDto request);
|
||||
|
||||
Task<List<FileVersionResponse>> GetListByCodeAsync(string code);
|
||||
|
||||
Task<FileVersionResponse> GetLastByCodeAsync(string code);
|
||||
}
|
||||
@@ -1,21 +1,35 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Services;
|
||||
namespace Dlhis.Common.FileOperation.Services;
|
||||
|
||||
public interface IMinioService : ISingletonDependency
|
||||
{
|
||||
Task<string> MinloUploadFileAsync(ApkRequest request);
|
||||
|
||||
Task<string> MinloDownloadLinkAsync(string fileName);
|
||||
|
||||
Task MinloRemoveFileAsync(string fileName);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// bucket不存在即创建
|
||||
/// </summary>
|
||||
/// <param name="bucket"></param>
|
||||
/// <returns></returns>
|
||||
Task CreateBucketIfNotExistsAsync(string bucket);
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否存在bucket
|
||||
/// </summary>
|
||||
/// <param name="bucket"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> ExistsBucketAsync(string bucket);
|
||||
|
||||
/// <summary>
|
||||
/// 判断文件是否存在
|
||||
/// </summary>
|
||||
/// <param name="bucket"></param>
|
||||
/// <param name="objectName"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> ExistsFileAsync(string bucket, string objectName);
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="bucket"></param>
|
||||
/// <param name="formFile"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> UploadFileAsync(string bucket, IFormFile formFile);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Dlhis.Common.FileOperation.Services;
|
||||
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;
|
||||
private readonly IConfiguration Configuration;
|
||||
|
||||
public ApkFIileService(IMinioService minioService, IDbContext dbContext, IConfiguration configuration)
|
||||
{
|
||||
_minioService = minioService;
|
||||
_dbContext = dbContext;
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
/// <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 lenFive = request.formFile.FileName.Length >= 5;
|
||||
|
||||
lenFive.ThrowIfFalse("文件名长度不能小于5");
|
||||
var houzhui = request.formFile.FileName.Substring(request.formFile.FileName.Length - 4, 4).ToLower();
|
||||
var isApk = request.formFile.FileName.Substring(request.formFile.FileName.Length - 4, 4) == ".apk";
|
||||
isApk.ThrowIfFalse("文件不是apk类型");
|
||||
|
||||
//更新数据库
|
||||
var entity = await _dbContext.Queryable<ApkFileEntity>().Where(x => x.ProductCode == request.ProductCode).FirstAsync();
|
||||
if (entity == null)
|
||||
{
|
||||
var info = new ApkFileEntity
|
||||
{
|
||||
Name = fileName,
|
||||
ProductCode = request.ProductCode,
|
||||
VersionCode = request.VersionCode,
|
||||
UpdateMessage = request.UpdateMessage,
|
||||
VersionName = request.VersionName,
|
||||
};
|
||||
await _dbContext.InsertAsync(info);
|
||||
}
|
||||
else
|
||||
{
|
||||
entity.VersionCode = request.VersionCode;
|
||||
entity.UpdateMessage = request.UpdateMessage;
|
||||
entity.VersionName = request.VersionName;
|
||||
entity.Name = fileName;
|
||||
await _dbContext.UpdateAsync(entity);
|
||||
}
|
||||
var newEntity = await _dbContext.Queryable<ApkFileEntity>().Where(x => x.ProductCode == request.ProductCode).FirstAsync();
|
||||
var res = newEntity.Adapt<ApkFileResponse>();
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<ApkFileResponse> GetLastVersionAsync(string productCode)
|
||||
{
|
||||
var info = await _dbContext.Queryable<ApkFileEntity>()
|
||||
.Where(x => x.ProductCode == productCode)
|
||||
.Select<ApkFileResponse>()
|
||||
.FirstAsync();
|
||||
info.ThrowIfNull($"产品{productCode}下没有apk存在");
|
||||
var url =await RequestDownloadFile(info.Name);
|
||||
|
||||
var host = Configuration["MinIO:Endpoint"];
|
||||
|
||||
var modified_url = url.Replace(host, "https://yikatong.isawesome.cn/m");
|
||||
|
||||
info.Url = modified_url;
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件下载
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<string> RequestDownloadFile(string fileName)
|
||||
{
|
||||
return await _minioService.MinloDownloadLinkAsync(fileName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Dlhis.Common.FileOperation.Response;
|
||||
using Dlhis.Entity.Common;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
|
||||
namespace Dlhis.Common.FileOperation.Services.Implements;
|
||||
|
||||
public class FileVersionService : IFileVersionService
|
||||
{
|
||||
private readonly IMinioService _minioService;
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public FileVersionService(IMinioService minioService, IDbContext dbContext)
|
||||
{
|
||||
_minioService = minioService;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<string> UploadFileAsync(FileVersionUploadRequest request)
|
||||
{
|
||||
var url = await _minioService.UploadFileAsync(request.Bucket, request.File);
|
||||
|
||||
var version = (await GetLastVersionAsync(request.Code)) + 1;
|
||||
await _dbContext.InsertAsync(new FileVersionEntity
|
||||
{
|
||||
Name = request.Name,
|
||||
Url = url,
|
||||
Description = request.Description,
|
||||
Code = request.Code,
|
||||
Version = version,
|
||||
});
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
public async Task<Pagination<FileVersionResponse>> GetPageAsync(PageDto request)
|
||||
{
|
||||
return await _dbContext.Queryable<FileVersionEntity>()
|
||||
.OrderByDescending(x => x.CreateTime)
|
||||
.Select<FileVersionResponse>()
|
||||
.ToPageListAsync(request);
|
||||
}
|
||||
|
||||
public async Task<List<FileVersionResponse>> GetListByCodeAsync(string code)
|
||||
{
|
||||
return await _dbContext.Queryable<FileVersionEntity>()
|
||||
.Where(x => x.Code == code)
|
||||
.OrderByDescending(x => x.CreateTime)
|
||||
.Select<FileVersionResponse>()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<FileVersionResponse> GetLastByCodeAsync(string code)
|
||||
{
|
||||
return await _dbContext.Queryable<FileVersionEntity>()
|
||||
.Where(x => x.Code == code)
|
||||
.OrderByDescending(x => x.Version)
|
||||
.Select<FileVersionResponse>()
|
||||
.SingleAsync();
|
||||
}
|
||||
|
||||
//************************************************************* help methods ***************************************************************
|
||||
|
||||
private async Task<int> GetLastVersionAsync(string code)
|
||||
{
|
||||
return await _dbContext.Queryable<FileVersionEntity>()
|
||||
.Where(x => x.Code == code)
|
||||
.OrderByDescending(x => x.Version)
|
||||
.Select(x => x.Version)
|
||||
.SingleAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,17 @@
|
||||
using Dlhis.Common.FileOperation.Configurations;
|
||||
using Dlhis.Common.FileOperation.Request;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
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)
|
||||
@@ -71,64 +66,4 @@ public class MinioService : IMinioService
|
||||
|
||||
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 (var 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
using Kurisu.SqlSugar;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dlhis.Entity.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Apk文件表
|
||||
/// </summary>
|
||||
[SugarTable(TableName = "ApkFIile", TableDescription = "Apk文件表")]
|
||||
public class ApkFileEntity : SugarBaseEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "文件名称")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "版本号")]
|
||||
public int VersionCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "版本名称")]
|
||||
public string VersionName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改信息
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "修改信息")]
|
||||
public string UpdateMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 产品代码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "产品代码")]
|
||||
public string ProductCode { get; set; }
|
||||
}
|
||||
|
||||
|
||||
43
Src/Dlhis.Entity/Common/FileVersionEntity.cs
Normal file
43
Src/Dlhis.Entity/Common/FileVersionEntity.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Kurisu.SqlSugar;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Entity.Common;
|
||||
|
||||
/// <summary>
|
||||
/// 文件版本信息
|
||||
/// </summary>
|
||||
[SugarTable(TableName = "FileVersion", TableDescription = "文件版本信息")]
|
||||
public class FileVersionEntity : SugarBaseEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件名称
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = false, ColumnDescription = "文件名称")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件code
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = false, ColumnDescription = "文件code", UniqueGroupNameList = new[] { "idx_FileVersion_Code" })]
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = false, ColumnDescription = "版本号")]
|
||||
public int Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 描述信息
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "描述信息")]
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件地址
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = false, ColumnDescription = "文件地址")]
|
||||
public string Url { get; set; }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user