L1SvrController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using LJLib.Client;
  2. using LJLib.Net.SPI.Client;
  3. using LJLib.Net.SPI.Com;
  4. using LJProxy.Models;
  5. using LJProxy.Settings;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using Newtonsoft.Json;
  13. using System.IO;
  14. using System.Text;
  15. using Newtonsoft.Json.Linq;
  16. using LJProxy.Services;
  17. using Microsoft.AspNetCore.Http.Extensions;
  18. using Microsoft.AspNetCore.Http;
  19. namespace LJProxy.Controllers
  20. {
  21. [Route("api/[controller]")]
  22. [ApiController]
  23. public class L1SvrController : Controller
  24. {
  25. private static object _syncRoot = new object();
  26. public static AppSettings _appSettingModel;
  27. private LJClientPoolService _ljClient;
  28. public L1SvrController(LJClientPoolService ljClient)
  29. {
  30. _ljClient = ljClient;
  31. }
  32. [Route("svr/{apiName}")]
  33. [HttpPost]
  34. [HttpGet]
  35. public async Task<IActionResult> Svr(string apiName)
  36. {
  37. string requestBody;
  38. using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
  39. {
  40. requestBody = await reader.ReadToEndAsync();
  41. }
  42. var url = Request.GetDisplayUrl();
  43. var pathStart = "/api/l1svr/svr/";
  44. var idx = url.IndexOf(pathStart, StringComparison.OrdinalIgnoreCase);
  45. string gateway = string.Empty;
  46. if (idx > 0)
  47. gateway = url.Substring(0, idx + 1);
  48. if (!string.IsNullOrEmpty(requestBody))
  49. {
  50. var reqObj = JObject.Parse(requestBody);
  51. reqObj["gateway"] = gateway;
  52. requestBody = JsonConvert.SerializeObject(reqObj);
  53. }
  54. var rslt = _ljClient.Pool.DoExcute(apiName, requestBody);
  55. return Content(rslt, "application/json");
  56. }
  57. [HttpPost]
  58. [Route("uploadfiles")]
  59. [RequestFormLimits(ValueLengthLimit = 20 *1024 * 1024, MultipartBodyLengthLimit = 21*1024*1024)]
  60. public IActionResult UploadFiles([FromForm] IFormCollection formData)
  61. {
  62. string token = string.Empty;
  63. if (Request.Headers.ContainsKey("Authorization"))
  64. {
  65. token = Request.Headers["Authorization"];
  66. }
  67. var files = formData.Files;
  68. if (files != null && files.Count > 0)
  69. {
  70. UploadFilesRequest requestObj = new UploadFilesRequest();
  71. List<FileInfoModel> fileList = new List<FileInfoModel>();
  72. requestObj.token = token;
  73. foreach (var file in files)
  74. {
  75. FileInfoModel fileInfo = new FileInfoModel();
  76. fileInfo.FileName = file.FileName;
  77. using (var fs = file.OpenReadStream())
  78. {
  79. byte[] buffer = new byte[fs.Length];
  80. fs.Read(buffer, 0, buffer.Length);
  81. fileInfo.FileData = Convert.ToBase64String(buffer);
  82. }
  83. fileList.Add(fileInfo);
  84. }
  85. requestObj.FileList = fileList;
  86. var requestBody = JsonConvert.SerializeObject(requestObj);
  87. var rslt = _ljClient.Pool.DoExcute(requestObj.GetApiName(), requestBody);
  88. return Content(rslt, "application/json");
  89. }
  90. else
  91. {
  92. return NotFound();
  93. }
  94. }
  95. }
  96. }