using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using Newtonsoft; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json; using NLog; using DataClient.Struct; using System.IO; using ApiServer.ApiStruct; namespace ApiServer.Controllers { [ApiController, Route("[controller]")] public class PasportController : ControllerBase { private Logger log = LogManager.GetCurrentClassLogger(); [HttpPost, Route("check")] public PasportCheckClient Check([FromBody] object value) { try { var getResult = JsonConvert.DeserializeObject(value.ToString()); if (!getResult.Status || string.IsNullOrEmpty(getResult.Name)) { log.Warn("Wrong answer."); return new PasportCheckClient(); } var paspName = getResult.DateAndTime.Hour.ToString("D2") + getResult.DateAndTime.Minute.ToString("D2") + getResult.DateAndTime.Second.ToString("D2") + "-" + getResult.Name; var paspDir = Path.Combine( Directory.GetCurrentDirectory(), "data", "pasport", getResult.DateAndTime.Year.ToString("D4"), getResult.DateAndTime.Month.ToString("D2"), getResult.DateAndTime.Day.ToString("D2"), paspName); log.Info("Search pasport: " + paspDir); if (!System.IO.File.Exists(paspDir)) { log.Info("Psport not exist: " + paspDir); return new PasportCheckClient { Status = true, Exist = false, PaspSum = 0 }; } var pasport = System.IO.File.ReadAllBytes(paspDir); ulong paspResult = 0; foreach (var b in pasport) paspResult += b; log.Info("Send pasport size: " + paspResult + " | " + paspDir); return new PasportCheckClient { Status = true, Exist = true, PaspSum = paspResult }; } catch(Exception e) { log.Warn(e); return new PasportCheckClient(); } } [HttpPost, Route("create")] public PasportCreateClient Create([FromBody] object value) { try { var getResult = JsonConvert.DeserializeObject(value.ToString()); if (!getResult.Status || getResult.Pasp == null || !getResult.Pasp.HasData) { log.Warn("Wrong answer."); return new PasportCreateClient(); } var paspName = getResult.Pasp.dEnd.Value.Hour.ToString("D2") + getResult.Pasp.dEnd.Value.Minute.ToString("D2") + getResult.Pasp.dEnd.Value.Second.ToString("D2") + "-" + (string.IsNullOrEmpty(getResult.Pasp.nplav) ? getResult.Pasp.numVDP.Value.ToString("D2") : getResult.Pasp.nplav); var dir = Path.Combine( Directory.GetCurrentDirectory(), "data", "pasport", getResult.Pasp.dEnd.Value.Year.ToString("D4"), getResult.Pasp.dEnd.Value.Month.ToString("D2"), getResult.Pasp.dEnd.Value.Day.ToString("D2")); var paspDir = Path.Combine(dir, paspName); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); System.IO.File.WriteAllBytes(paspDir, getResult.Pasp.PaspByte); log.Info("Save pasport: " + paspDir); return new PasportCreateClient { Status = true }; } catch (Exception e) { log.Warn(e); return new PasportCreateClient(); } } [HttpPost, Route("getlist")] public PasportGetListClient GetList([FromBody] object value) { try { var pasportNameList = new List(); var getResult = JsonConvert.DeserializeObject(value.ToString()); var pasportDir = Path.Combine( Directory.GetCurrentDirectory(), "data", "pasport", getResult.Date.Year.ToString("D4"), getResult.Date.ToString("D2"), getResult.Date.Day.ToString("D2") ); if (!Directory.Exists(pasportDir)) return new PasportGetListClient(); var allPasportFiles = Directory.GetFiles(pasportDir); foreach (var fullPasportName in allPasportFiles) { var name = Path.GetFileNameWithoutExtension(fullPasportName); var pasportParts = name.Split('-'); var pasport = ""; for (var i = 1; i < pasportParts.Length; i++) { if (pasport.Length != 0) pasport += "-"; pasport += pasportParts[i]; } pasportNameList.Add(pasport); } log.Info("Send pasport list: " + pasportNameList); return new PasportGetListClient() { Name = pasportNameList.ToArray() }; } catch (Exception e) { log.Warn(e); return new PasportGetListClient(); } } } }