diff --git a/Src/Dlhis.Common/Dlhis.Common.csproj b/Src/Dlhis.Common/Dlhis.Common.csproj index a2e461e0..bc489a52 100644 --- a/Src/Dlhis.Common/Dlhis.Common.csproj +++ b/Src/Dlhis.Common/Dlhis.Common.csproj @@ -11,9 +11,13 @@ + + + + @@ -25,8 +29,4 @@ - - - - diff --git a/Src/Dlhis.Common/EnumSetting/EnumsController.cs b/Src/Dlhis.Common/EnumSetting/EnumsController.cs index cc940da5..1821b42f 100644 --- a/Src/Dlhis.Common/EnumSetting/EnumsController.cs +++ b/Src/Dlhis.Common/EnumSetting/EnumsController.cs @@ -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 { 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 { 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 { Key = fd, Value = (int)item.GetValue(null) diff --git a/Src/Dlhis.Common/FileOperation/Constants/FileBucketConstants.cs b/Src/Dlhis.Common/FileOperation/Constants/FileBucketConstants.cs new file mode 100644 index 00000000..a26b3e14 --- /dev/null +++ b/Src/Dlhis.Common/FileOperation/Constants/FileBucketConstants.cs @@ -0,0 +1,13 @@ +namespace Dlhis.Common.FileOperation.Constants; + +public static class FileBucketConstants +{ + /// + /// 文件buckets + /// + public static List Buckets { get; } = new() + { + "restaurant", + "apk" + }; +} diff --git a/Src/Dlhis.Common/FileOperation/Controllers/ApkFileController.cs b/Src/Dlhis.Common/FileOperation/Controllers/ApkFileController.cs deleted file mode 100644 index 6036920e..00000000 --- a/Src/Dlhis.Common/FileOperation/Controllers/ApkFileController.cs +++ /dev/null @@ -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; - -/// -/// APK文件操作 -/// -[Obsolete("作废重写")] -[Authorize] -[Route("api/[controller]")] -[ApiController] -public class ApkFileController : ControllerBase -{ - private readonly IApkFIileService _apkFIileService; - - public ApkFileController(IApkFIileService apkFIileService) - { - _apkFIileService = apkFIileService; - } - /// - /// minlo文件上传 - /// - /// - /// - [HttpPost("minio")] - [RequestSizeLimit(300_000_000)] - public async Task FormSingleFileUpload([FromForm] ApkRequest request) - { - return await _apkFIileService.FormSingleFileUpload(request); - } - - /// - /// minlo文件下载 - /// - /// - /// - [HttpGet("url")] - public async Task RequestDownloadFile(string fileName) - { - return await _apkFIileService.RequestDownloadFile(fileName); - } - - /// - /// 获取最新版本 - /// - /// - /// - [HttpGet("lastversion")] - public async Task GetLastVersionAsync(string productCode) - { - return await _apkFIileService.GetLastVersionAsync(productCode); - } -} - - - diff --git a/Src/Dlhis.Common/FileOperation/Controllers/FileController.cs b/Src/Dlhis.Common/FileOperation/Controllers/FileController.cs index 46fb28f3..9d95fc60 100644 --- a/Src/Dlhis.Common/FileOperation/Controllers/FileController.cs +++ b/Src/Dlhis.Common/FileOperation/Controllers/FileController.cs @@ -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; +/// +/// 文件操作 +/// [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; } /// @@ -27,4 +35,50 @@ public class FileController : ControllerBase { return await _minioService.UploadFileAsync(request.Bucket, request.File); } + + + /// + /// 文件上传(版本) + /// + /// + /// + [HttpPost("{Bucket}/version/upload")] + [RequestSizeLimit(300_000_000)] + public async Task UploadFileAsync([FromForm] FileVersionUploadRequest request) + { + return await _fileVersionService.UploadFileAsync(request); + } + + /// + /// 文件版本分页 + /// + /// + /// + [HttpGet("version/page")] + public async Task> GetPageAsync([FromQuery] PageDto request) + { + return await _fileVersionService.GetPageAsync(request); + } + + /// + /// 文件版本列表 + /// + /// + /// + [HttpGet("version/list")] + public async Task> GetListByCodeAsync([Required] string code) + { + return await _fileVersionService.GetListByCodeAsync(code); + } + + /// + /// 文件版本最新 + /// + /// + /// + [HttpGet("version/last")] + public async Task GetLastByCodeAsync([Required] string code) + { + return await _fileVersionService.GetLastByCodeAsync(code); + } } diff --git a/Src/Dlhis.Common/FileOperation/Request/ApkRequest.cs b/Src/Dlhis.Common/FileOperation/Request/ApkRequest.cs deleted file mode 100644 index aca49440..00000000 --- a/Src/Dlhis.Common/FileOperation/Request/ApkRequest.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace Dlhis.Common.FileOperation.Request -{ - public class ApkRequest - { - /// - /// 文件 - /// - public IFormFile formFile { get; set; } - - /// - /// 版本号 - /// - public int VersionCode { get; set; } - - /// - /// 版本名称 - /// - public string VersionName { get; set; } - - /// - /// 修改信息 - /// - public string UpdateMessage { get; set; } - - /// - /// 产品代码 - /// - public string ProductCode { get; set; } - } -} diff --git a/Src/Dlhis.Common/FileOperation/Request/FileVersionUploadRequest.cs b/Src/Dlhis.Common/FileOperation/Request/FileVersionUploadRequest.cs new file mode 100644 index 00000000..04249399 --- /dev/null +++ b/Src/Dlhis.Common/FileOperation/Request/FileVersionUploadRequest.cs @@ -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 +{ + /// + /// bucket + /// + [FromRoute] + public string Bucket { get; set; } + + /// + /// 文件 + /// + [Required] + public IFormFile File { get; set; } + + /// + /// 文件名称 + /// + [Required] + public string Name { get; set; } + + /// + /// code + /// + [Required] + public string Code { get; set; } + + /// + /// 描述信息 + /// + public string Description { get; set; } + + public IEnumerable Validate(ValidationContext validationContext) + { + if (!FileBucketConstants.Buckets.Contains(Bucket)) + { + yield return new ValidationResult($"不支持{Bucket}", new[] { nameof(Bucket) }); + } + } +} diff --git a/Src/Dlhis.Common/FileOperation/Request/UploadFileReuqest.cs b/Src/Dlhis.Common/FileOperation/Request/UploadFileReuqest.cs index ac690a39..767287bf 100644 --- a/Src/Dlhis.Common/FileOperation/Request/UploadFileReuqest.cs +++ b/Src/Dlhis.Common/FileOperation/Request/UploadFileReuqest.cs @@ -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 { /// /// bucket @@ -14,4 +16,12 @@ public class UploadFileReuqest /// 文件 /// public IFormFile File { get; set; } + + public IEnumerable Validate(ValidationContext validationContext) + { + if (!FileBucketConstants.Buckets.Contains(Bucket)) + { + yield return new ValidationResult($"不支持{Bucket}", new[] { nameof(Bucket) }); + } + } } diff --git a/Src/Dlhis.Common/FileOperation/Response/ApkFile1Response.cs b/Src/Dlhis.Common/FileOperation/Response/ApkFile1Response.cs deleted file mode 100644 index 143be5fb..00000000 --- a/Src/Dlhis.Common/FileOperation/Response/ApkFile1Response.cs +++ /dev/null @@ -1,32 +0,0 @@ -using SqlSugar; - -namespace Dlhis.Common.FileOperation.Response -{ - public class ApkFile1Response - { - /// - /// 文件名称 - /// - public string Name { get; set; } - - /// - /// 地址 - /// - public string Url { get; set; } - - /// - /// 版本号 - /// - public int VersionCode { get; set; } - - /// - /// 版本名称 - /// - public int VersionName { get; set; } - - /// - /// 修改信息 - /// - public string UpdateMessage { get; set; } - } -} diff --git a/Src/Dlhis.Common/FileOperation/Response/ApkFileFormLocalResponse.cs b/Src/Dlhis.Common/FileOperation/Response/ApkFileFormLocalResponse.cs deleted file mode 100644 index b4c78bf2..00000000 --- a/Src/Dlhis.Common/FileOperation/Response/ApkFileFormLocalResponse.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Dlhis.Common.FileOperation.Response -{ - public class ApkFileFormLocalResponse - { - /// - /// 文件名称 - /// - public string Name { get; set; } - - /// - /// 版本号 - /// - public int VersionCode { get; set; } - - /// - /// 版本名称 - /// - public int VersionName { get; set; } - - /// - /// 修改信息 - /// - public string UpdateMessage { get; set; } - } -} diff --git a/Src/Dlhis.Common/FileOperation/Response/ApkFileResponse.cs b/Src/Dlhis.Common/FileOperation/Response/ApkFileResponse.cs deleted file mode 100644 index 78a8db3f..00000000 --- a/Src/Dlhis.Common/FileOperation/Response/ApkFileResponse.cs +++ /dev/null @@ -1,37 +0,0 @@ -using SqlSugar; - -namespace Dlhis.Common.FileOperation.Response -{ - public class ApkFileResponse - { - /// - /// 文件名称 - /// - public string Name { get; set; } - - /// - /// 版本号 - /// - public int VersionCode { get; set; } - - /// - /// 版本名称 - /// - public string VersionName { get; set; } - - /// - /// 修改信息 - /// - public string UpdateMessage { get; set; } - - /// - /// 产品代码 - /// - public string ProductCode { get; set; } - - /// - /// url - /// - public string Url { get; set; } - } -} diff --git a/Src/Dlhis.Common/FileOperation/Response/CommonEnumResponse.cs b/Src/Dlhis.Common/FileOperation/Response/CommonEnumResponse.cs index 06a5a66e..3d4ee959 100644 --- a/Src/Dlhis.Common/FileOperation/Response/CommonEnumResponse.cs +++ b/Src/Dlhis.Common/FileOperation/Response/CommonEnumResponse.cs @@ -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 Data { get; set; } - } + public string Code { get; set; } + + public string Name { get; set; } + + public List> Data { get; set; } } diff --git a/Src/Dlhis.Common/FileOperation/Response/FileVersionResponse.cs b/Src/Dlhis.Common/FileOperation/Response/FileVersionResponse.cs new file mode 100644 index 00000000..891f6358 --- /dev/null +++ b/Src/Dlhis.Common/FileOperation/Response/FileVersionResponse.cs @@ -0,0 +1,31 @@ +namespace Dlhis.Common.FileOperation.Response; + +public class FileVersionResponse +{ + public long Id { get; set; } + + /// + /// 文件名称 + /// + public string Name { get; set; } + + /// + /// 文件code + /// + public string Code { get; set; } + + /// + /// 版本号 + /// + public int Version { get; set; } + + /// + /// 描述信息 + /// + public string Description { get; set; } + + /// + /// 文件地址 + /// + public string Url { get; set; } +} diff --git a/Src/Dlhis.Common/FileOperation/Response/KeyValueDto.cs b/Src/Dlhis.Common/FileOperation/Response/KeyValueDto.cs deleted file mode 100644 index f7cc487f..00000000 --- a/Src/Dlhis.Common/FileOperation/Response/KeyValueDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Dlhis.Common.FileOperation.Response; - -public class KeyValueDto -{ - public string Key { get; set; } - - public object Value { get; set; } -} diff --git a/Src/Dlhis.Common/FileOperation/Services/ApkFIileService.cs b/Src/Dlhis.Common/FileOperation/Services/ApkFIileService.cs deleted file mode 100644 index 2ebc1240..00000000 --- a/Src/Dlhis.Common/FileOperation/Services/ApkFIileService.cs +++ /dev/null @@ -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; - } - - /// - /// 文件上传 - /// - /// - /// - public async Task FormSingleFileUpload(ApkRequest request) - { - //上传 - var fileUrl = await _minioService.MinloUploadFileAsync(request); - - string fileName = request.formFile.FileName; - - //更新数据库 - var entity = await _dbContext.Queryable().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().Where(x => x.Name == fileName).FirstAsync(); - var res = newEntity.Adapt(); - return res; - } - - /// - /// 文件下载 - /// - /// - public async Task RequestDownloadFile(string fileName) - { - return await _minioService.MinloDownloadLinkAsync(fileName); - } - - /// - /// 本地文件上传 - /// - /// - /// - public async Task 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().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().Where(x => x.Name == fileName).FirstAsync(); - var res = newEntity.Adapt(); - 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; - } - } - /// - /// 返回最新版本信息 - /// - /// - /// - public async Task LatestFile(string fileName) - { - var info = await _dbContext.Queryable() - .Where(x => x.Name == fileName) - .OrderByDescending(x => x.VersionCode) - .FirstAsync(); - info.ThrowIfNull($"没有{fileName}信息"); - var res = info.Adapt(); - return res; - } - - /// - /// 本地下载 - /// - /// - /// - 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 IApkFIileService.FormSingleFileUpload(ApkRequest request) - { - throw new NotImplementedException(); - } - - public Task GetLastVersionAsync(string productCode) - { - throw new NotImplementedException(); - } -} - - diff --git a/Src/Dlhis.Common/FileOperation/Services/IApkFIileService.cs b/Src/Dlhis.Common/FileOperation/Services/IApkFIileService.cs deleted file mode 100644 index 047b8f6b..00000000 --- a/Src/Dlhis.Common/FileOperation/Services/IApkFIileService.cs +++ /dev/null @@ -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 -{ - /// - /// 文件上传 - /// - /// - /// - Task FormSingleFileUpload(ApkRequest request); - - /// - /// 文件下载 - /// - /// - Task RequestDownloadFile(string fileName); - - - Task GetLastVersionAsync(string productCode); -} - - - diff --git a/Src/Dlhis.Common/FileOperation/Services/IFileVersionService.cs b/Src/Dlhis.Common/FileOperation/Services/IFileVersionService.cs new file mode 100644 index 00000000..69c2bdb7 --- /dev/null +++ b/Src/Dlhis.Common/FileOperation/Services/IFileVersionService.cs @@ -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 UploadFileAsync(FileVersionUploadRequest request); + + Task> GetPageAsync(PageDto request); + + Task> GetListByCodeAsync(string code); + + Task GetLastByCodeAsync(string code); +} diff --git a/Src/Dlhis.Common/FileOperation/Services/IMinioService.cs b/Src/Dlhis.Common/FileOperation/Services/IMinioService.cs index f70c17f0..6a318f01 100644 --- a/Src/Dlhis.Common/FileOperation/Services/IMinioService.cs +++ b/Src/Dlhis.Common/FileOperation/Services/IMinioService.cs @@ -1,21 +1,35 @@ -using Dlhis.Common.FileOperation.Request; - -namespace Dlhis.Common.FileOperation.Services; +namespace Dlhis.Common.FileOperation.Services; public interface IMinioService : ISingletonDependency { - Task MinloUploadFileAsync(ApkRequest request); - - Task MinloDownloadLinkAsync(string fileName); - - Task MinloRemoveFileAsync(string fileName); - - + /// + /// bucket不存在即创建 + /// + /// + /// + Task CreateBucketIfNotExistsAsync(string bucket); + /// + /// 判断是否存在bucket + /// + /// + /// Task ExistsBucketAsync(string bucket); + /// + /// 判断文件是否存在 + /// + /// + /// + /// Task ExistsFileAsync(string bucket, string objectName); + /// + /// 上传文件 + /// + /// + /// + /// Task UploadFileAsync(string bucket, IFormFile formFile); } diff --git a/Src/Dlhis.Common/FileOperation/Services/Implements/ApkFIileService.cs b/Src/Dlhis.Common/FileOperation/Services/Implements/ApkFIileService.cs deleted file mode 100644 index 9b920f5c..00000000 --- a/Src/Dlhis.Common/FileOperation/Services/Implements/ApkFIileService.cs +++ /dev/null @@ -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; - } - - /// - /// 文件上传 - /// - /// - /// - public async Task 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().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().Where(x => x.ProductCode == request.ProductCode).FirstAsync(); - var res = newEntity.Adapt(); - return res; - } - - public async Task GetLastVersionAsync(string productCode) - { - var info = await _dbContext.Queryable() - .Where(x => x.ProductCode == productCode) - .Select() - .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; - } - - /// - /// 文件下载 - /// - /// - public async Task RequestDownloadFile(string fileName) - { - return await _minioService.MinloDownloadLinkAsync(fileName); - } - - - -} - - diff --git a/Src/Dlhis.Common/FileOperation/Services/Implements/FileVersionService.cs b/Src/Dlhis.Common/FileOperation/Services/Implements/FileVersionService.cs new file mode 100644 index 00000000..3d4a4285 --- /dev/null +++ b/Src/Dlhis.Common/FileOperation/Services/Implements/FileVersionService.cs @@ -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 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> GetPageAsync(PageDto request) + { + return await _dbContext.Queryable() + .OrderByDescending(x => x.CreateTime) + .Select() + .ToPageListAsync(request); + } + + public async Task> GetListByCodeAsync(string code) + { + return await _dbContext.Queryable() + .Where(x => x.Code == code) + .OrderByDescending(x => x.CreateTime) + .Select() + .ToListAsync(); + } + + public async Task GetLastByCodeAsync(string code) + { + return await _dbContext.Queryable() + .Where(x => x.Code == code) + .OrderByDescending(x => x.Version) + .Select() + .SingleAsync(); + } + + //************************************************************* help methods *************************************************************** + + private async Task GetLastVersionAsync(string code) + { + return await _dbContext.Queryable() + .Where(x => x.Code == code) + .OrderByDescending(x => x.Version) + .Select(x => x.Version) + .SingleAsync(); + } +} diff --git a/Src/Dlhis.Common/FileOperation/Services/Implements/MinioService.cs b/Src/Dlhis.Common/FileOperation/Services/Implements/MinioService.cs index 70d82c49..811f8a0a 100644 --- a/Src/Dlhis.Common/FileOperation/Services/Implements/MinioService.cs +++ b/Src/Dlhis.Common/FileOperation/Services/Implements/MinioService.cs @@ -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 options) { _client = client; - _minIOOptions = options.Value; } public async Task ExistsBucketAsync(string bucket) @@ -71,64 +66,4 @@ public class MinioService : IMinioService return $"/{bucket}/{response.ObjectName}"; } - - - /// - /// 上传文件 - /// - /// - /// - public async Task 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; - } - - /// - /// 文件下载链接 - /// - /// - public async Task 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; - } - } - - - /// - /// 单个删除文件 - /// - /// - /// - public async Task MinloRemoveFileAsync(string fileName) - { - await _client.RemoveObjectAsync(new RemoveObjectArgs() - .WithBucket(_minIOOptions.DefaultBucket) - .WithObject(fileName)); - } - } diff --git a/Src/Dlhis.Entity/Common/ApkFileEntity.cs b/Src/Dlhis.Entity/Common/ApkFileEntity.cs deleted file mode 100644 index e32c5bf9..00000000 --- a/Src/Dlhis.Entity/Common/ApkFileEntity.cs +++ /dev/null @@ -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; - -/// -/// Apk文件表 -/// -[SugarTable(TableName = "ApkFIile", TableDescription = "Apk文件表")] -public class ApkFileEntity : SugarBaseEntity -{ - /// - /// 文件名称 - /// - [SugarColumn(ColumnDescription = "文件名称")] - public string Name { get; set; } - - /// - /// 版本号 - /// - [SugarColumn(ColumnDescription = "版本号")] - public int VersionCode { get; set; } - - /// - /// 版本名称 - /// - [SugarColumn(ColumnDescription = "版本名称")] - public string VersionName { get; set; } - - /// - /// 修改信息 - /// - [SugarColumn(ColumnDescription = "修改信息")] - public string UpdateMessage { get; set; } - - /// - /// 产品代码 - /// - [SugarColumn(ColumnDescription = "产品代码")] - public string ProductCode { get; set; } -} - - diff --git a/Src/Dlhis.Entity/Common/FileVersionEntity.cs b/Src/Dlhis.Entity/Common/FileVersionEntity.cs new file mode 100644 index 00000000..d0973b26 --- /dev/null +++ b/Src/Dlhis.Entity/Common/FileVersionEntity.cs @@ -0,0 +1,43 @@ +using Kurisu.SqlSugar; +using SqlSugar; + +namespace Dlhis.Entity.Common; + +/// +/// 文件版本信息 +/// +[SugarTable(TableName = "FileVersion", TableDescription = "文件版本信息")] +public class FileVersionEntity : SugarBaseEntity +{ + /// + /// 文件名称 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "文件名称")] + public string Name { get; set; } + + /// + /// 文件code + /// + [SugarColumn(IsNullable = false, ColumnDescription = "文件code", UniqueGroupNameList = new[] { "idx_FileVersion_Code" })] + public string Code { get; set; } + + /// + /// 版本号 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "版本号")] + public int Version { get; set; } + + /// + /// 描述信息 + /// + [SugarColumn(ColumnDescription = "描述信息")] + public string Description { get; set; } + + /// + /// 文件地址 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "文件地址")] + public string Url { get; set; } +} + +