ImgController.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. namespace molilian.api.Controllers
  6. {
  7. [ApiController]
  8. [Route("[controller]/[action]")]
  9. public class ImgController : ControllerBase
  10. {
  11. protected IHttpContextAccessor _accessor;
  12. public ImgController(IHttpContextAccessor accessor)
  13. {
  14. _accessor = accessor;
  15. }
  16. [HttpPost("{tokenString}")]
  17. public async Task<ActionResult> Upfile(string tokenString, IFormFile file)
  18. {
  19. if (string.IsNullOrEmpty(tokenString) || file == null || file.Length == 0)
  20. return new APIResult(new { msg = "参数提交不正确", status = -1 });
  21. var token = ApiTokenCore.GetOne(tokenString);
  22. if (token == null)
  23. return new APIResult(new { msg = "key不正确", status = -2 });
  24. if (!token.status)
  25. return new APIResult(new { msg = "账号已经禁用", status = -3 });
  26. if (token.last_time < DateTime.Now)
  27. return new APIResult(new { msg = "授权已到期", status = -3 });
  28. string lockKey = $"lock:{tokenString}";
  29. if (RedisHelper.Exists(lockKey))
  30. return new APIResult(new { msg = "请求频率过高", status = -4 });
  31. using MemoryStream stream = new();
  32. file.CopyTo(stream);
  33. var bytes = stream.ToArray();
  34. var result = await ImageHostingPlus.Dianping(file.FileName, bytes);
  35. return new APIResult(new { data = result });
  36. }
  37. }
  38. }