65 lines
2.5 KiB
C#
65 lines
2.5 KiB
C#
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;
|
|
}
|
|
|
|
[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));
|
|
}
|
|
|
|
[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));
|
|
}
|
|
|
|
[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));
|
|
}
|
|
}
|
|
}
|