L1SvrController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. private string getIpAddress()
  33. {
  34. var ipAddress = Request.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR");
  35. if (!string.IsNullOrEmpty(ipAddress))
  36. {
  37. string[] addresses = ipAddress.Split(',');
  38. if (addresses.Length > 0)
  39. {
  40. return addresses[0];
  41. }
  42. }
  43. return Request.HttpContext.GetServerVariable("REMOTE_ADDR");
  44. }
  45. [Route("svr/{apiName}")]
  46. [HttpPost]
  47. [HttpGet]
  48. public async Task<IActionResult> Svr(string apiName)
  49. {
  50. var ipAddress = getIpAddress();
  51. string requestBody;
  52. using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
  53. {
  54. requestBody = await reader.ReadToEndAsync();
  55. }
  56. var url = Request.GetDisplayUrl();
  57. var pathStart = "/api/l1svr/svr/";
  58. var idx = url.IndexOf(pathStart, StringComparison.OrdinalIgnoreCase);
  59. string gateway = string.Empty;
  60. if (idx > 0)
  61. gateway = url.Substring(0, idx + 1);
  62. if (!string.IsNullOrEmpty(requestBody))
  63. {
  64. var reqObj = JObject.Parse(requestBody);
  65. reqObj["gateway"] = gateway;
  66. if (!string.IsNullOrEmpty(ipAddress))
  67. {
  68. reqObj["ip"] = ipAddress;
  69. }
  70. requestBody = JsonConvert.SerializeObject(reqObj);
  71. }
  72. var rslt = _ljClient.Pool.DoExcute(apiName, requestBody);
  73. return Content(rslt, "application/json");
  74. }
  75. [HttpPost]
  76. [Route("uploadfiles")]
  77. [RequestFormLimits(ValueLengthLimit = 20 *1024 * 1024, MultipartBodyLengthLimit = 21*1024*1024)]
  78. public IActionResult UploadFiles([FromForm] IFormCollection formData)
  79. {
  80. string token = string.Empty;
  81. if (Request.Headers.ContainsKey("Authorization"))
  82. {
  83. token = Request.Headers["Authorization"];
  84. }
  85. var files = formData.Files;
  86. if (files != null && files.Count > 0)
  87. {
  88. UploadFilesRequest requestObj = new UploadFilesRequest();
  89. List<FileInfoModel> fileList = new List<FileInfoModel>();
  90. requestObj.token = token;
  91. foreach (var file in files)
  92. {
  93. FileInfoModel fileInfo = new FileInfoModel();
  94. fileInfo.FileName = file.FileName;
  95. using (var fs = file.OpenReadStream())
  96. {
  97. byte[] buffer = new byte[fs.Length];
  98. fs.Read(buffer, 0, buffer.Length);
  99. fileInfo.FileData = Convert.ToBase64String(buffer);
  100. }
  101. fileList.Add(fileInfo);
  102. }
  103. requestObj.FileList = fileList;
  104. var requestBody = JsonConvert.SerializeObject(requestObj);
  105. var rslt = _ljClient.Pool.DoExcute(requestObj.GetApiName(), requestBody);
  106. return Content(rslt, "application/json");
  107. }
  108. else
  109. {
  110. return NotFound();
  111. }
  112. }
  113. }
  114. }