using System; using System.IO; using jsonjumble.Library; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; namespace jsonjumble.Controllers { [ApiController] [Route("v1/data")] [Produces("application/json")] public class DataControllerV1 : ControllerBase { private readonly ILogger Logger; private IConfiguration Configuration; private Library.SHA256HashGenerator sha256HashGenerator; public DataControllerV1(ILogger logger, IConfiguration config) { Logger = logger; Configuration = config; sha256HashGenerator = Library.SHA256HashGenerator.GetInstance(); } private string getDatapath(string user, string key) { string dataStorePath = Configuration["DataStoreFilePath"]; return $"{dataStorePath}/{user}/"; } private string getFilepath(string user, string key) { return $"{getDatapath(user, key)}/{sha256HashGenerator.Get(key)}.json"; } [HttpGet("{user}/{key}")] public IActionResult Get(string user, string key) { string filePath = getFilepath(user, key); if (!System.IO.File.Exists(filePath)) { dynamic errorObject = new { errorCode = StatusCodes.Status404NotFound, errorMessage = "404 Error: That key has no associated data.", filePath = filePath }; return StatusCode(errorObject.errorCode, errorObject); } try { var json = JObject.Parse(System.IO.File.ReadAllText(filePath)); return Ok(json); } catch (Exception) { dynamic errorObject = new { errorCode = StatusCodes.Status500InternalServerError, errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance.", filePath = filePath }; return StatusCode(errorObject.errorCode, errorObject); } } [Authorize] [HttpPost("{user}/{key}")] public IActionResult Save(string user, string key, [FromBody] dynamic body) { string filePath = getFilepath(user, key); dynamic json = body; string path = new JwtTokenHandler(Configuration) .GetTokenPath(Request.Headers.Authorization.ToString().Substring(7)); if(user != path) { dynamic errorObject = new { errorCode = StatusCodes.Status401Unauthorized, errorMessage = "401 Unauthorized: Token is not authorized to alter this key.", filePath = filePath }; return StatusCode(errorObject.errorCode, errorObject); } string dataStorePath = getDatapath(user, key); if(!Directory.Exists(dataStorePath)) { Directory.CreateDirectory(dataStorePath); } if (System.IO.File.Exists(filePath)) { try { json = JObject.Parse(System.IO.File.ReadAllText(filePath)); foreach (var prop in body) { json[prop.Name] = prop.Value; } } catch (Exception e) { Logger.LogError($"Save Error: {e.Message}"); dynamic errorObject = new { errorCode = StatusCodes.Status500InternalServerError, errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance.", filePath = filePath }; return StatusCode(errorObject.errorCode, errorObject); } } System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(json, Formatting.Indented)); return Ok(json); } [Authorize] [HttpPut("{user}/{key}")] public IActionResult Put(string user, string key, [FromBody] dynamic body) { string filePath = getFilepath(user, key); string path = new JwtTokenHandler(Configuration) .GetTokenPath(Request.Headers.Authorization.ToString().Substring(7)); if(user != path) { dynamic errorObject = new { errorCode = StatusCodes.Status401Unauthorized, errorMessage = "401 Unauthorized: Token is not authorized to alter this key.", filePath = filePath }; return StatusCode(errorObject.errorCode, errorObject); } string dataStorePath = getDatapath(user, key); if(!Directory.Exists(dataStorePath)) { Directory.CreateDirectory(dataStorePath); } System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(body, Formatting.Indented)); return Ok(body); } [Authorize] [HttpDelete("{user}/{key}")] public IActionResult Delete(string user, string key) { string filePath = getFilepath(user, key); string path = new JwtTokenHandler(Configuration) .GetTokenPath(Request.Headers.Authorization.ToString().Substring(7)); if(user != path) { dynamic errorObject = new { errorCode = StatusCodes.Status401Unauthorized, errorMessage = "401 Unauthorized: Token is not authorized to alter this key.", filePath = filePath }; return StatusCode(errorObject.errorCode, errorObject); } System.IO.File.Delete(filePath); return Ok(); } [HttpGet("hash/{key}")] public IActionResult GetHash(string key) { return Ok(sha256HashGenerator.Get(key)); } } }