160 lines
4.5 KiB
C#
160 lines
4.5 KiB
C#
|
|
using CloudGaming.Core.AgileConfig;
|
|
using CloudGaming.Core.AgileConfig.AgileConfigApi;
|
|
using CloudGaming.Core.AgileConfig.AgileConfigClient;
|
|
using CloudGaming.Repository.Game.Entities.Ext;
|
|
|
|
using global::CloudGaming.Core.AgileConfig.AgileConfigApi;
|
|
using global::CloudGaming.Core.AgileConfig;
|
|
|
|
using IdGen;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Refit;
|
|
|
|
using AppConfig = CloudGaming.Code.DataBaseModel.AppConfig;
|
|
using CloudGaming.Code.Aliyun;
|
|
|
|
|
|
namespace CloudGaming.Api.Admin.ApplicationServices.Apps.Ext;
|
|
|
|
/// <summary>
|
|
/// 服务 AliyunOssService
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
public class AliyunOssService(IServiceProvider serviceProvider)
|
|
: ApplicationService(serviceProvider)// ApplicationGameService<AppConfig, Guid, AppConfig, AppConfig>(serviceProvider)
|
|
{
|
|
[Autowired]
|
|
public virtual AppConfigTenant AppConfigTenant { get; set; }
|
|
[AppSettings("AgileConfig")]
|
|
public virtual AgileConfigVM AgileConfig { get; set; }
|
|
[Autowired]
|
|
public virtual IAccountService AccountService { get; set; }
|
|
|
|
[Autowired]
|
|
public virtual AppConfig appConfig { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
/// 查询表单数据
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, object?>> FindFormAsync()
|
|
{
|
|
|
|
var res = new Dictionary<string, object?>();
|
|
|
|
AliyunOssConfig? form = appConfig.AliyunConfig;
|
|
form = form.NullSafe();
|
|
|
|
res["id"] = appConfig.TenantId;
|
|
res[nameof(form)] = form;
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="form"></param>
|
|
/// <returns></returns>
|
|
public async Task SaveFormAsync(AliyunOssConfig form)
|
|
{
|
|
|
|
if (AppConfigTenant == null)
|
|
{
|
|
AppConfigTenant = new AppConfigTenant();
|
|
}
|
|
if (AppConfigTenant.Tenants == null)
|
|
{
|
|
AppConfigTenant.Tenants = new List<AppConfig>();
|
|
}
|
|
var t = AppConfigTenant.Tenants.FirstOrDefault(it => it.TenantId == appConfig.TenantId);
|
|
if (t != null)
|
|
{
|
|
t.AliyunConfig = form;
|
|
await SynchronizeData();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="form"></param>
|
|
/// <returns></returns>
|
|
public AliyunOssConfigDto GeneratePresignedUri(string name, int fileType)
|
|
{
|
|
|
|
string fileName = "";
|
|
if (fileType == 1)
|
|
{
|
|
fileName += "game/";
|
|
}
|
|
else
|
|
{
|
|
|
|
fileName += "common/";
|
|
}
|
|
|
|
return GeneratePresignedUri(name, fileName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="form"></param>
|
|
/// <returns></returns>
|
|
public AliyunOssConfigDto GeneratePresignedUri(string name, string path)
|
|
{
|
|
var contentType = AliyunConfigExtend.GetContentType(name);
|
|
string fileName = appConfig.AliyunConfig.UploadPath ?? "";
|
|
if (!fileName.EndsWith("/"))
|
|
{
|
|
fileName += "/";
|
|
}
|
|
fileName += path;
|
|
if (!fileName.EndsWith("/"))
|
|
{
|
|
fileName += "/";
|
|
}
|
|
string extension = System.IO.Path.GetExtension(name);
|
|
|
|
fileName += $"{DateTime.Now.ToString("yyyy-MM/yyyyMMddHHmmss")}{Guid.NewGuid().ToString("D").Substring(0, 5)}{extension}";
|
|
var url = appConfig.AliyunConfig.GeneratePresignedUri(fileName, contentType);
|
|
AliyunOssConfigDto aliyunOssConfigDto = new AliyunOssConfigDto()
|
|
{
|
|
Url = url,
|
|
Path = fileName,
|
|
DomainName = appConfig.AliyunConfig.DomainName
|
|
};
|
|
return aliyunOssConfigDto;
|
|
}
|
|
|
|
|
|
private async Task SynchronizeData()
|
|
{
|
|
var agileConfigServer = ServiceProvider.GetService<AgileConfigServer>();
|
|
var agileConfigApiVM = new CloudGaming.Core.AgileConfig.AgileConfigApi.AgileConfigApiVM()
|
|
{
|
|
IsPatch = false,
|
|
Json = JsonConvert.SerializeObject(AppConfigTenant)
|
|
};
|
|
if (agileConfigServer != null)
|
|
{
|
|
var s = await agileConfigServer.AgileConfigApi.SaveJson(AgileConfig.AppId, AgileConfig.Env, agileConfigApiVM);
|
|
if (s.Success)
|
|
{
|
|
var model = new AgileConfigPublishVM()
|
|
{
|
|
log = $"{AccountService.AccountContext.Name}修改阿里云配置,{DateTime.Now}自动发布",
|
|
appId = AgileConfig.AppId
|
|
};
|
|
await agileConfigServer.AgileConfigApi.Publish(AgileConfig.Env, model);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|