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; } /// /// 兼容现有前端的通用上传入口。有 name 时按编号覆盖,否则上传到临时目录。 /// [HttpPost("upload")] public async Task 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(result.errorMessage)); } /// /// 上传公告等临时图片。业务数据保存成功后,由对应业务服务移动到正式目录。 /// [HttpPost("upload/temp")] public async Task 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(result.errorMessage)); } /// /// 上传道具、碎片等配置图片,按“编号.png”直接覆盖正式图片。 /// [HttpPost("upload/config")] public async Task UploadConfigFile(IFormFile file, [FromQuery] string? number = null) { if (!string.Equals(Path.GetExtension(file?.FileName), ".png", StringComparison.OrdinalIgnoreCase)) return Json(this.ToFailResult("配置图片必须是 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("图片编号无效")); } var result = await _imageUploadService.UploadImageAsync(file, number); return result.isOk ? Json(this.ToResult(new { path = result.path, fileName = result.fileName, number })) : Json(this.ToFailResult(result.errorMessage)); } } }