123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using LJLib.Client;
- using LJLib.Net.SPI.Client;
- using LJLib.Net.SPI.Com;
- using LJProxy.Models;
- using LJProxy.Settings;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using System.IO;
- using System.Text;
- using Newtonsoft.Json.Linq;
- using LJProxy.Services;
- using Microsoft.AspNetCore.Http.Extensions;
- using Microsoft.AspNetCore.Http;
- namespace LJProxy.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class L1SvrController : Controller
- {
- private static object _syncRoot = new object();
- public static AppSettings _appSettingModel;
- private LJClientPoolService _ljClient;
- public L1SvrController(LJClientPoolService ljClient)
- {
- _ljClient = ljClient;
- }
- private string getIpAddress()
- {
- var ipAddress = Request.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR");
- if (!string.IsNullOrEmpty(ipAddress))
- {
- string[] addresses = ipAddress.Split(',');
- if (addresses.Length > 0)
- {
- return addresses[0];
- }
- }
- return Request.HttpContext.GetServerVariable("REMOTE_ADDR");
- }
- [Route("svr/{apiName}")]
- [HttpPost]
- [HttpGet]
- public async Task<IActionResult> Svr(string apiName)
- {
- var ipAddress = getIpAddress();
- string requestBody;
- using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
- {
- requestBody = await reader.ReadToEndAsync();
- }
- var url = Request.GetDisplayUrl();
- var pathStart = "/api/l1svr/svr/";
- var idx = url.IndexOf(pathStart, StringComparison.OrdinalIgnoreCase);
- string gateway = string.Empty;
- if (idx > 0)
- gateway = url.Substring(0, idx + 1);
- if (!string.IsNullOrEmpty(requestBody))
- {
- var reqObj = JObject.Parse(requestBody);
- reqObj["gateway"] = gateway;
- if (!string.IsNullOrEmpty(ipAddress))
- {
- reqObj["ip"] = ipAddress;
- }
- requestBody = JsonConvert.SerializeObject(reqObj);
- }
- var rslt = _ljClient.Pool.DoExcute(apiName, requestBody);
- return Content(rslt, "application/json");
- }
- [HttpPost]
- [Route("uploadfiles")]
- [RequestFormLimits(ValueLengthLimit = 20 *1024 * 1024, MultipartBodyLengthLimit = 21*1024*1024)]
- public IActionResult UploadFiles([FromForm] IFormCollection formData)
- {
- string token = string.Empty;
- if (Request.Headers.ContainsKey("Authorization"))
- {
- token = Request.Headers["Authorization"];
- }
- var files = formData.Files;
- if (files != null && files.Count > 0)
- {
- UploadFilesRequest requestObj = new UploadFilesRequest();
- List<FileInfoModel> fileList = new List<FileInfoModel>();
- requestObj.token = token;
- foreach (var file in files)
- {
- FileInfoModel fileInfo = new FileInfoModel();
- fileInfo.FileName = file.FileName;
- using (var fs = file.OpenReadStream())
- {
- byte[] buffer = new byte[fs.Length];
- fs.Read(buffer, 0, buffer.Length);
- fileInfo.FileData = Convert.ToBase64String(buffer);
- }
- fileList.Add(fileInfo);
- }
- requestObj.FileList = fileList;
- var requestBody = JsonConvert.SerializeObject(requestObj);
- var rslt = _ljClient.Pool.DoExcute(requestObj.GetApiName(), requestBody);
- return Content(rslt, "application/json");
- }
- else
- {
- return NotFound();
- }
-
- }
- }
- }
|