123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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<DataControllerV1> 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));
- }
- }
- }
|