45 lines
2.0 KiB
C#
45 lines
2.0 KiB
C#
using Jiaowu.Api.Domain.Academic;
|
|
using Jiaowu.Api.Domain.Identity;
|
|
using Jiaowu.Api.Infrastructure.Persistence;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Jiaowu.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Authorize(Roles = SystemRoles.SuperAdmin)]
|
|
[Route("api/approval-attachment-storage")]
|
|
public sealed class ApprovalAttachmentStorageController(AppDbContext db, IConfiguration configuration) : ControllerBase
|
|
{
|
|
internal static readonly Guid SettingId = Guid.Parse("3b0d4d71-835f-44c7-8af3-e4112913ba66");
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> Get(CancellationToken ct)
|
|
{
|
|
var setting = await db.ApprovalAttachmentStorageSettings.AsNoTracking().SingleOrDefaultAsync(x => x.Id == SettingId, ct);
|
|
return Ok(new { Provider = setting?.Provider ?? "Local", S3Configured = IsS3Configured() });
|
|
}
|
|
|
|
[HttpPut]
|
|
public async Task<IActionResult> Put(StorageProviderRequest request, CancellationToken ct)
|
|
{
|
|
if (request.Provider is not ("Local" or "S3")) return BadRequest(new { message = "无效的存储方式。" });
|
|
if (request.Provider == "S3" && !IsS3Configured()) return BadRequest(new { message = "请先在服务器配置 S3 地址、区域、存储桶和访问凭据。" });
|
|
var setting = await db.ApprovalAttachmentStorageSettings.SingleOrDefaultAsync(x => x.Id == SettingId, ct);
|
|
if (setting is null)
|
|
{
|
|
setting = new ApprovalAttachmentStorageSetting { Id = SettingId, Provider = request.Provider };
|
|
db.ApprovalAttachmentStorageSettings.Add(setting);
|
|
}
|
|
else { setting.Provider = request.Provider; setting.UpdatedAt = DateTime.UtcNow; }
|
|
await db.SaveChangesAsync(ct);
|
|
return NoContent();
|
|
}
|
|
|
|
private bool IsS3Configured() => new[] { "Endpoint", "Region", "Bucket", "AccessKey", "SecretKey" }
|
|
.All(key => !string.IsNullOrWhiteSpace(configuration[$"Attachments:S3:{key}"]));
|
|
}
|
|
|
|
public sealed record StorageProviderRequest(string Provider);
|