DataControllerV1.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.IO;
  3. using jsonjumble.Library;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Configuration;
  8. using Newtonsoft.Json;
  9. using Microsoft.Extensions.Logging;
  10. using Newtonsoft.Json.Linq;
  11. namespace jsonjumble.Controllers
  12. {
  13. [ApiController]
  14. [Route("v1/data")]
  15. [Produces("application/json")]
  16. public class DataControllerV1 : ControllerBase
  17. {
  18. private readonly ILogger Logger;
  19. private IConfiguration Configuration;
  20. private Library.SHA256HashGenerator sha256HashGenerator;
  21. public DataControllerV1(ILogger<DataControllerV1> logger, IConfiguration config)
  22. {
  23. Logger = logger;
  24. Configuration = config;
  25. sha256HashGenerator = Library.SHA256HashGenerator.GetInstance();
  26. }
  27. private string getDatapath(string user, string key)
  28. {
  29. string dataStorePath = Configuration["DataStoreFilePath"];
  30. return $"{dataStorePath}/{user}/";
  31. }
  32. private string getFilepath(string user, string key)
  33. {
  34. return $"{getDatapath(user, key)}/{sha256HashGenerator.Get(key)}.json";
  35. }
  36. [HttpGet("{user}/{key}")]
  37. public IActionResult Get(string user, string key)
  38. {
  39. string filePath = getFilepath(user, key);
  40. if (!System.IO.File.Exists(filePath))
  41. {
  42. dynamic errorObject = new
  43. {
  44. errorCode = StatusCodes.Status404NotFound,
  45. errorMessage = "404 Error: That key has no associated data.",
  46. filePath = filePath
  47. };
  48. return StatusCode(errorObject.errorCode, errorObject);
  49. }
  50. try
  51. {
  52. var json = JObject.Parse(System.IO.File.ReadAllText(filePath));
  53. return Ok(json);
  54. }
  55. catch (Exception)
  56. {
  57. dynamic errorObject = new
  58. {
  59. errorCode = StatusCodes.Status500InternalServerError,
  60. errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance.",
  61. filePath = filePath
  62. };
  63. return StatusCode(errorObject.errorCode, errorObject);
  64. }
  65. }
  66. [Authorize]
  67. [HttpPost("{user}/{key}")]
  68. public IActionResult Save(string user, string key, [FromBody] dynamic body)
  69. {
  70. string filePath = getFilepath(user, key);
  71. dynamic json = body;
  72. string path = new JwtTokenHandler(Configuration)
  73. .GetTokenPath(Request.Headers.Authorization.ToString().Substring(7));
  74. if(user != path) {
  75. dynamic errorObject = new
  76. {
  77. errorCode = StatusCodes.Status401Unauthorized,
  78. errorMessage = "401 Unauthorized: Token is not authorized to alter this key.",
  79. filePath = filePath
  80. };
  81. return StatusCode(errorObject.errorCode, errorObject);
  82. }
  83. string dataStorePath = getDatapath(user, key);
  84. if(!Directory.Exists(dataStorePath)) {
  85. Directory.CreateDirectory(dataStorePath);
  86. }
  87. if (System.IO.File.Exists(filePath))
  88. {
  89. try
  90. {
  91. json = JObject.Parse(System.IO.File.ReadAllText(filePath));
  92. foreach (var prop in body)
  93. {
  94. json[prop.Name] = prop.Value;
  95. }
  96. }
  97. catch (Exception e)
  98. {
  99. Logger.LogError($"Save Error: {e.Message}");
  100. dynamic errorObject = new
  101. {
  102. errorCode = StatusCodes.Status500InternalServerError,
  103. errorMessage = "500 Error: Something went wrong. Please contact the administrator for assistance.",
  104. filePath = filePath
  105. };
  106. return StatusCode(errorObject.errorCode, errorObject);
  107. }
  108. }
  109. System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(json, Formatting.Indented));
  110. return Ok(json);
  111. }
  112. [Authorize]
  113. [HttpPut("{user}/{key}")]
  114. public IActionResult Put(string user, string key, [FromBody] dynamic body)
  115. {
  116. string filePath = getFilepath(user, key);
  117. string path = new JwtTokenHandler(Configuration)
  118. .GetTokenPath(Request.Headers.Authorization.ToString().Substring(7));
  119. if(user != path) {
  120. dynamic errorObject = new
  121. {
  122. errorCode = StatusCodes.Status401Unauthorized,
  123. errorMessage = "401 Unauthorized: Token is not authorized to alter this key.",
  124. filePath = filePath
  125. };
  126. return StatusCode(errorObject.errorCode, errorObject);
  127. }
  128. string dataStorePath = getDatapath(user, key);
  129. if(!Directory.Exists(dataStorePath)) {
  130. Directory.CreateDirectory(dataStorePath);
  131. }
  132. System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(body, Formatting.Indented));
  133. return Ok(body);
  134. }
  135. [Authorize]
  136. [HttpDelete("{user}/{key}")]
  137. public IActionResult Delete(string user, string key)
  138. {
  139. string filePath = getFilepath(user, key);
  140. string path = new JwtTokenHandler(Configuration)
  141. .GetTokenPath(Request.Headers.Authorization.ToString().Substring(7));
  142. if(user != path) {
  143. dynamic errorObject = new
  144. {
  145. errorCode = StatusCodes.Status401Unauthorized,
  146. errorMessage = "401 Unauthorized: Token is not authorized to alter this key.",
  147. filePath = filePath
  148. };
  149. return StatusCode(errorObject.errorCode, errorObject);
  150. }
  151. System.IO.File.Delete(filePath);
  152. return Ok();
  153. }
  154. [HttpGet("hash/{key}")]
  155. public IActionResult GetHash(string key)
  156. {
  157. return Ok(sha256HashGenerator.Get(key));
  158. }
  159. }
  160. }