103 lines
3.3 KiB
C#
103 lines
3.3 KiB
C#
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);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|