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; } /// /// 文件上传 /// /// /// 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, 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().Where(x => x.Name == fileName).FirstAsync(); var res = newEntity.Adapt(); 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; } } /// /// 本地下载 /// /// /// public IActionResult FormLocalDownloadFile(string filePath) { string fileName = Path.GetFileName(filePath); return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName }; } }