2026-10-12 16:22:04 +08:00
|
|
|
using AdminService.ImageUpload;
|
|
|
|
|
using Common;
|
|
|
|
|
using CommonInfrastructure;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace AdminApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("adminapi/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class ImageUploadController : BaseController
|
|
|
|
|
{
|
|
|
|
|
private readonly IImageUploadService _imageUploadService;
|
|
|
|
|
|
|
|
|
|
public ImageUploadController(IImageUploadService imageUploadService)
|
|
|
|
|
{
|
|
|
|
|
_imageUploadService = imageUploadService;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 00:44:00 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 兼容现有前端的通用上传入口。有 name 时按编号覆盖,否则上传到临时目录。
|
|
|
|
|
/// </summary>
|
2026-10-12 16:22:04 +08:00
|
|
|
[HttpPost("upload")]
|
|
|
|
|
public async Task<IActionResult> UploadFile(IFormFile file, [FromQuery] string? name = null)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
|
name = Request.Form["name"].FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
var result = await _imageUploadService.UploadImageAsync(file, name);
|
|
|
|
|
return result.isOk
|
|
|
|
|
? Json(this.ToResult(new { path = result.path, fileName = result.fileName }))
|
|
|
|
|
: Json(this.ToFailResult<object>(result.errorMessage));
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 00:44:00 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 上传公告等临时图片。业务数据保存成功后,由对应业务服务移动到正式目录。
|
|
|
|
|
/// </summary>
|
2026-10-12 16:22:04 +08:00
|
|
|
[HttpPost("upload/temp")]
|
|
|
|
|
public async Task<IActionResult> UploadTempFile(IFormFile file)
|
|
|
|
|
{
|
|
|
|
|
var result = await _imageUploadService.UploadImageAsync(file);
|
|
|
|
|
return result.isOk
|
|
|
|
|
? Json(this.ToResult(new { path = result.path, fileName = result.fileName }))
|
|
|
|
|
: Json(this.ToFailResult<object>(result.errorMessage));
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 00:44:00 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 上传道具、碎片等配置图片,按“编号.png”直接覆盖正式图片。
|
|
|
|
|
/// </summary>
|
2026-10-12 16:22:04 +08:00
|
|
|
[HttpPost("upload/config")]
|
|
|
|
|
public async Task<IActionResult> UploadConfigFile(IFormFile file, [FromQuery] string? number = null)
|
|
|
|
|
{
|
|
|
|
|
if (!string.Equals(Path.GetExtension(file?.FileName), ".png", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return Json(this.ToFailResult<object>("配置图片必须是 PNG 格式"));
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(number))
|
|
|
|
|
number = Request.Form["number"].FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
number = number?.Trim();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(number)
|
|
|
|
|
|| number != Path.GetFileName(number)
|
|
|
|
|
|| number.Any(c => !char.IsLetterOrDigit(c) && c != '_' && c != '-'))
|
|
|
|
|
{
|
|
|
|
|
return Json(this.ToFailResult<object>("图片编号无效"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await _imageUploadService.UploadImageAsync(file, number);
|
|
|
|
|
return result.isOk
|
|
|
|
|
? Json(this.ToResult(new { path = result.path, fileName = result.fileName, number }))
|
|
|
|
|
: Json(this.ToFailResult<object>(result.errorMessage));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|