diff --git a/Src/Dlhis.Common/Controllers/ApkFIileController.cs b/Src/Dlhis.Common/Controllers/ApkFIileController.cs index 15601144..29215940 100644 --- a/Src/Dlhis.Common/Controllers/ApkFIileController.cs +++ b/Src/Dlhis.Common/Controllers/ApkFIileController.cs @@ -2,17 +2,18 @@ using Dlhis.Common.Response; using Dlhis.Common.Services; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; namespace Dlhis.Common.Controllers; - /// - /// 文件上传/下载 - /// - [Authorize] - [Route("api/[controller]")] - [ApiController] - public class ApkFIileController : ControllerBase +/// +/// 文件上传/下载 +/// +[Authorize] +[Route("api/[controller]")] +[ApiController] +public class ApkFIileController : ControllerBase { private IApkFIileService _apkFIileService; @@ -26,10 +27,24 @@ namespace Dlhis.Common.Controllers; /// /// [HttpPost] - public async Task FormSingleFileUpload(ApkRequest request) + public async Task FormSingleFileUpload([FromForm] ApkRequest request) { - await _apkFIileService.FormSingleFileUpload(request); + await _apkFIileService.FormSingleFileUpload(request); } + + /// + /// 文件下载 + /// + /// + /// + [HttpGet("url")] + public async Task RequestDownloadFile() + { + return await _apkFIileService.RequestDownloadFile(); + } + + } + diff --git a/Src/Dlhis.Common/Packs/MinioPack.cs b/Src/Dlhis.Common/Packs/MinioPack.cs index 1421de5e..636a2a7b 100644 --- a/Src/Dlhis.Common/Packs/MinioPack.cs +++ b/Src/Dlhis.Common/Packs/MinioPack.cs @@ -1,5 +1,6 @@ using Kurisu.AspNetCore.Startup; using Kurisu.SqlSugar.Extensions; +using Microsoft.Extensions.DependencyInjection; using Minio; namespace Dlhis.Common.Packs @@ -11,10 +12,9 @@ namespace Dlhis.Common.Packs public override void ConfigureServices(IServiceCollection services) { //注入minioClient - var minioClient = (MinioClient)new MinioClient() - .WithEndpoint(Configuration["MinIO:Endpoint"]) - .WithCredentials(Configuration["MinIO:AccessKey"], Configuration["MinIO:SecretKey"]) - .WithSSL() + IMinioClient minioClient = (MinioClient)new MinioClient() + .WithEndpoint("192.168.199.124:9000") + .WithCredentials("Zbjj05OFtR19e5TJopQU", "UMXtNdZhufhAAyYJ3XND4cEgZNUsgOWtbUzksizK") .Build(); services.AddSingleton(minioClient); } diff --git a/Src/Dlhis.Common/Services/IApkFIileService.cs b/Src/Dlhis.Common/Services/IApkFIileService.cs index 06a39a2f..f9240845 100644 --- a/Src/Dlhis.Common/Services/IApkFIileService.cs +++ b/Src/Dlhis.Common/Services/IApkFIileService.cs @@ -11,6 +11,12 @@ namespace Dlhis.Common.Services; /// /// Task FormSingleFileUpload(ApkRequest request); + + /// + /// 文件下载 + /// + /// + Task RequestDownloadFile(); } diff --git a/Src/Dlhis.Common/Services/IMinioService.cs b/Src/Dlhis.Common/Services/IMinioService.cs new file mode 100644 index 00000000..c09a08ce --- /dev/null +++ b/Src/Dlhis.Common/Services/IMinioService.cs @@ -0,0 +1,11 @@ +using Dlhis.Common.Request; + +namespace Dlhis.Common.Services +{ + public interface IMinioService : IScopeDependency + { + Task MinloUploadFileAsync(ApkRequest request); + Task MinloDownloadLinkAsync(); + Task MinloRemoveFileAsync(ApkRequest request); + } +} diff --git a/Src/Dlhis.Common/Services/Implements/ApkFIileService.cs b/Src/Dlhis.Common/Services/Implements/ApkFIileService.cs index 1cf176d8..3a93e131 100644 --- a/Src/Dlhis.Common/Services/Implements/ApkFIileService.cs +++ b/Src/Dlhis.Common/Services/Implements/ApkFIileService.cs @@ -10,22 +10,23 @@ using Minio.DataModel.Args; using Minio.Exceptions; using System.Xml.Linq; using System; +using Google.Protobuf.WellKnownTypes; +using Minio.DataModel.Encryption; +using MySqlX.XDevAPI; +using Microsoft.AspNetCore.Http; +using System.IO; namespace Dlhis.Common.Services.Implements; public class ApkFIileService : IApkFIileService { + private readonly IMinioService _minioService; - private readonly IDbContext _dbContext; - private readonly MinioClient _client; - - public ApkFIileService(IDbContext dbContext, MinioClient client) + public ApkFIileService( IMinioService minioService) { - _dbContext = dbContext; - _client = client; + _minioService = minioService; } - /// /// 文件上传 /// @@ -33,33 +34,16 @@ public class ApkFIileService : IApkFIileService /// public async Task FormSingleFileUpload(ApkRequest request) { - //上传文件返回地址 - var url = "https://raw.githubusercontent.com/feicien/android-auto-update/develop/extras/android-auto-update-v1.3.apk"; - - var apkInfo = new ApkFIileEntity - { - Url = url, - VersionCode = request.VersionCode, - UpdateMessage = request.UpdateMessage, - }; - await _dbContext.InsertAsync(apkInfo); + await _minioService.MinloUploadFileAsync(request); } - public async Task UploadFile(IFormFile formFile,string bucketName = "test") + /// + /// 文件下载 + /// + /// + public async Task RequestDownloadFile() { - - - } - - - //文件下载 - [HttpGet] - public IActionResult RequestDownloadFile(string filename) - { - - var FilePath =" "; - - return new FileStreamResult(new FileStream(FilePath, FileMode.Open), "application/octet-stream") { FileDownloadName = filename }; + return await _minioService.MinloDownloadLinkAsync(); } } diff --git a/Src/Dlhis.Common/Services/Implements/MinioService.cs b/Src/Dlhis.Common/Services/Implements/MinioService.cs new file mode 100644 index 00000000..78ea94b2 --- /dev/null +++ b/Src/Dlhis.Common/Services/Implements/MinioService.cs @@ -0,0 +1,107 @@ +using Dlhis.Common.Request; +using Dlhis.Entity.Common; +using Google.Protobuf.WellKnownTypes; +using Kurisu.SqlSugar.Services; +using Minio; +using Minio.DataModel.Args; +using Minio.Exceptions; +using Npgsql.Replication.PgOutput.Messages; +using System.IO; + +namespace Dlhis.Common.Services.Implements +{ + public class MinioService : IMinioService + { + + private readonly IDbContext _dbContext; + private readonly IMinioClient _client; + + private static string bucketName = "apk"; + + public MinioService(IDbContext dbContext, IMinioClient client) + { + _dbContext = dbContext; + _client = client; + } + + + /// + /// 上传文件 + /// + /// + /// + public async Task MinloUploadFileAsync(ApkRequest request) + { + + using (MemoryStream filestream = new MemoryStream()) + { + await request.formFile.CopyToAsync(filestream); + filestream.Position = 0; + + string fileName = request.formFile.FileName; + + var res = await _client.PutObjectAsync(new PutObjectArgs() + .WithBucket(bucketName) + .WithObject(fileName) + .WithStreamData(filestream) + .WithObjectSize(filestream.Length)); + //"\"698d51a19d8a121ce581499d7b701668\"" + //更新数据库 + var entity = await _dbContext.Queryable().Where(x => x.Name == fileName).FirstAsync(); + if (entity == null) + { + var info = new ApkFileEntity + { + Name = fileName, + Url = fileName, + VersionCode = request.VersionCode, + UpdateMessage = request.UpdateMessage, + }; + await _dbContext.InsertAsync(info); + } + else + { + entity.Url = fileName; + entity.VersionCode = request.VersionCode; + entity.UpdateMessage = request.UpdateMessage; + } + } + } + + /// + /// 文件下载链接 + /// + /// + public async Task MinloDownloadLinkAsync() + { + try + { + var url = await _client.PresignedGetObjectAsync(new PresignedGetObjectArgs() + .WithBucket(bucketName) + .WithObject("消费API逻辑.txt") + .WithExpiry(60 * 60 * 24));//链接时效 + return url; + } + catch (MinioException e) + { + Console.Out.WriteLine("Error occurred: " + e); + return null; + } + } + + + /// + /// 单个删除文件 + /// + /// + /// + public async Task MinloRemoveFileAsync(ApkRequest request) + { + string fileName = request.formFile.FileName; + await _client.RemoveObjectAsync(new RemoveObjectArgs() + .WithBucket(bucketName) + .WithObject(fileName)); + } + + } +} diff --git a/Src/Dlhis.Common/appsettings.Development.json b/Src/Dlhis.Common/appsettings.Development.json index 78225481..32a011f5 100644 --- a/Src/Dlhis.Common/appsettings.Development.json +++ b/Src/Dlhis.Common/appsettings.Development.json @@ -31,12 +31,12 @@ //minIO配置 "MinIO": { //服务器IP - "Endpoint": "http://192.168.199.124:9001", + "Endpoint": "http://192.168.199.124:9000", //账号 "AccessKey": "Zbjj05OFtR19e5TJopQU", //密码 "SecretKey": "UMXtNdZhufhAAyYJ3XND4cEgZNUsgOWtbUzksizK", //默认存储桶 - "Bucket": "tests" + "Bucket": "apk" } } diff --git a/Src/Dlhis.Entity/Common/ApkFIileEntity.cs b/Src/Dlhis.Entity/Common/ApkFileEntity.cs similarity index 64% rename from Src/Dlhis.Entity/Common/ApkFIileEntity.cs rename to Src/Dlhis.Entity/Common/ApkFileEntity.cs index 80f37c90..0e2b42db 100644 --- a/Src/Dlhis.Entity/Common/ApkFIileEntity.cs +++ b/Src/Dlhis.Entity/Common/ApkFileEntity.cs @@ -6,14 +6,20 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Dlhis.Entity.Common +namespace Dlhis.Entity.Common; + +/// +/// Apk文件表 +/// +[SugarTable(TableName = "ApkFIile", TableDescription = "Apk文件表")] +public class ApkFileEntity : SugarBaseEntity { - /// - /// Apk文件表 - /// - [SugarTable(TableName = "ApkFIile", TableDescription = "Apk文件表")] - public class ApkFIileEntity : SugarBaseEntity - { + /// + /// 文件名称 + /// + [SugarColumn(ColumnDescription = "文件名称")] + public string Name { get; set; } + /// /// 地址 /// @@ -32,4 +38,5 @@ namespace Dlhis.Entity.Common [SugarColumn(ColumnDescription = "修改信息")] public string UpdateMessage { get; set; } } -} + +