87 lines
2.5 KiB
C#
87 lines
2.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using ApiServer.ApiStruct;
|
|
using Newtonsoft.Json;
|
|
using System.IO;
|
|
|
|
namespace ApiServer.Controllers
|
|
{
|
|
[ApiController, Route("[controller]")]
|
|
public class DataController : ControllerBase
|
|
{
|
|
private Logger log = LogManager.GetCurrentClassLogger();
|
|
[HttpPost, Route("check")]
|
|
public DataCheckClient Check([FromBody] object value)
|
|
{
|
|
try
|
|
{
|
|
var getResult = JsonConvert.DeserializeObject<DataCheckApi>(value.ToString());
|
|
if (!getResult.Status || string.IsNullOrEmpty(getResult.Name))
|
|
{
|
|
log.Warn("Wrong answer.");
|
|
return new DataCheckClient();
|
|
}
|
|
var dataDir = Path.Combine(
|
|
Directory.GetCurrentDirectory(),
|
|
"data", "data",
|
|
getResult.DateAndTime.Year.ToString("D4"),
|
|
getResult.DateAndTime.Month.ToString("D2"),
|
|
getResult.DateAndTime.Day.ToString("D2"),
|
|
getResult.Name);
|
|
log.Info("Search data: " + dataDir);
|
|
if (!System.IO.File.Exists(dataDir))
|
|
{
|
|
log.Info("Data not exist: " + dataDir);
|
|
return new DataCheckClient { Status = true, Exist = false, DataSize = 0 };
|
|
}
|
|
var dataArr = System.IO.File.ReadAllBytes(dataDir);
|
|
log.Info("Send data size: " + dataArr.Length + " | " + dataDir);
|
|
return new DataCheckClient { Status = true, Exist = true, DataSize = (ulong)dataArr.Length };
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.Warn(e);
|
|
return new DataCheckClient();
|
|
}
|
|
}
|
|
[DisableRequestSizeLimit]
|
|
[HttpPost, Route("create")]
|
|
public DataCreateClient Create([FromBody] object value)
|
|
{
|
|
try
|
|
{
|
|
var getResult = JsonConvert.DeserializeObject<DataCreateApi>(value.ToString());
|
|
if (!getResult.Status ||
|
|
string.IsNullOrEmpty(getResult.Name) ||
|
|
getResult.Struct.Length == 0)
|
|
{
|
|
log.Warn("Wrong answer.");
|
|
return new DataCreateClient();
|
|
}
|
|
var dataDir = Path.Combine(
|
|
Directory.GetCurrentDirectory(),
|
|
"data", "data",
|
|
getResult.DateAndTime.Year.ToString("D4"),
|
|
getResult.DateAndTime.Month.ToString("D2"),
|
|
getResult.DateAndTime.Day.ToString("D2"));
|
|
if (!Directory.Exists(dataDir))
|
|
Directory.CreateDirectory(dataDir);
|
|
dataDir = Path.Combine(dataDir, getResult.Name);
|
|
System.IO.File.WriteAllBytes(dataDir, getResult.Struct);
|
|
log.Info("Save data: " + dataDir);
|
|
return new DataCreateClient { Status = true };
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.Warn(e);
|
|
return new DataCreateClient();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|