The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadController.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using Teknik.Areas.Error.ViewModels;
  9. using Teknik.Areas.Upload.Models;
  10. using Teknik.Areas.Upload.ViewModels;
  11. using Teknik.Controllers;
  12. using Teknik.Helpers;
  13. using Teknik.Models;
  14. namespace Teknik.Areas.Upload.Controllers
  15. {
  16. public class UploadController : DefaultController
  17. {
  18. private TeknikEntities db = new TeknikEntities();
  19. // GET: Upload/Upload
  20. [HttpGet]
  21. [AllowAnonymous]
  22. public ActionResult Index()
  23. {
  24. ViewBag.Title = "Teknik Upload - End to End Encryption";
  25. UploadViewModel model = new UploadViewModel();
  26. Areas.Profile.Models.User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
  27. if (user != null)
  28. {
  29. model.SaveKey = user.UploadSettings.SaveKey;
  30. model.ServerSideEncrypt = user.UploadSettings.ServerSideEncrypt;
  31. }
  32. else
  33. {
  34. model.SaveKey = false;
  35. model.ServerSideEncrypt = false;
  36. }
  37. return View(model);
  38. }
  39. [HttpPost]
  40. [AllowAnonymous]
  41. [ValidateAntiForgeryToken]
  42. public ActionResult Upload(string fileType, string fileExt, string iv, int keySize, int blockSize, bool encrypt, HttpPostedFileWrapper data, string key = null)
  43. {
  44. if (Config.UploadConfig.UploadEnabled)
  45. {
  46. if (data.ContentLength <= Config.UploadConfig.MaxUploadSize)
  47. {
  48. // convert file to bytes
  49. byte[] fileData = null;
  50. int contentLength = data.ContentLength;
  51. using (var binaryReader = new BinaryReader(data.InputStream))
  52. {
  53. fileData = binaryReader.ReadBytes(data.ContentLength);
  54. }
  55. // if they want us to encrypt it, we do so here
  56. if (encrypt)
  57. {
  58. // Generate key and iv if empty
  59. if (string.IsNullOrEmpty(key))
  60. {
  61. key = Utility.RandomString(keySize / 8);
  62. }
  63. fileData = AES.Encrypt(fileData, key, iv);
  64. if (fileData == null || fileData.Length <= 0)
  65. {
  66. return Json(new { error = new { message = "Unable to encrypt file" } });
  67. }
  68. }
  69. Models.Upload upload = Uploader.SaveFile(fileData, fileType, contentLength, fileExt, iv, key, keySize, blockSize);
  70. if (upload != null)
  71. {
  72. if (User.Identity.IsAuthenticated)
  73. {
  74. Profile.Models.User user = db.Users.Where(u => u.Username == User.Identity.Name).FirstOrDefault();
  75. if (user != null)
  76. {
  77. upload.UserId = user.UserId;
  78. db.Entry(upload).State = EntityState.Modified;
  79. db.SaveChanges();
  80. }
  81. }
  82. return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }) } }, "text/plain");
  83. }
  84. return Json(new { error = "Unable to upload file" });
  85. }
  86. else
  87. {
  88. return Json(new { error = "File Too Large" });
  89. }
  90. }
  91. return Json(new { error = "Uploads are disabled" });
  92. }
  93. // User did not supply key
  94. [HttpGet]
  95. [AllowAnonymous]
  96. public ActionResult Download(string file)
  97. {
  98. if (Config.UploadConfig.DownloadEnabled)
  99. {
  100. ViewBag.Title = "Teknik Download - " + file;
  101. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  102. if (upload != null)
  103. {
  104. upload.Downloads += 1;
  105. db.Entry(upload).State = EntityState.Modified;
  106. db.SaveChanges();
  107. // We don't have the key, so we need to decrypt it client side
  108. if (string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
  109. {
  110. DownloadViewModel model = new DownloadViewModel();
  111. model.FileName = file;
  112. model.ContentType = upload.ContentType;
  113. model.ContentLength = upload.ContentLength;
  114. model.IV = upload.IV;
  115. return View(model);
  116. }
  117. else // We have the key, so that means server side decryption
  118. {
  119. if (System.IO.File.Exists(upload.FileName))
  120. {
  121. // Read in the file
  122. byte[] data = System.IO.File.ReadAllBytes(upload.FileName);
  123. // If the IV is set, and Key is set, then decrypt it
  124. if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
  125. {
  126. // Decrypt the data
  127. data = AES.Decrypt(data, upload.Key, upload.IV);
  128. }
  129. // Create content disposition
  130. var cd = new System.Net.Mime.ContentDisposition
  131. {
  132. FileName = upload.Url,
  133. Inline = true
  134. };
  135. Response.AppendHeader("Content-Disposition", cd.ToString());
  136. return File(data, upload.ContentType);
  137. }
  138. }
  139. }
  140. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  141. }
  142. return Redirect(Url.SubRouteUrl("error", "Error.Http403"));
  143. }
  144. [HttpPost]
  145. [AllowAnonymous]
  146. [ValidateAntiForgeryToken]
  147. public FileResult DownloadData(string file)
  148. {
  149. if (Config.UploadConfig.DownloadEnabled)
  150. {
  151. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  152. if (upload != null)
  153. {
  154. string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, upload.FileName);
  155. if (System.IO.File.Exists(filePath))
  156. {
  157. byte[] buffer;
  158. FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  159. try
  160. {
  161. int length = (int)fileStream.Length; // get file length
  162. buffer = new byte[length]; // create buffer
  163. int count; // actual number of bytes read
  164. int sum = 0; // total number of bytes read
  165. // read until Read method returns 0 (end of the stream has been reached)
  166. while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
  167. sum += count; // sum is a buffer offset for next reading
  168. }
  169. finally
  170. {
  171. fileStream.Close();
  172. }
  173. return File(buffer, System.Net.Mime.MediaTypeNames.Application.Octet, file);
  174. }
  175. }
  176. Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  177. return null;
  178. }
  179. Redirect(Url.SubRouteUrl("error", "Error.Http403"));
  180. return null;
  181. }
  182. [HttpGet]
  183. [AllowAnonymous]
  184. public ActionResult Delete(string file, string key)
  185. {
  186. ViewBag.Title = "File Delete - " + file + " - " + Config.Title;
  187. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  188. if (upload != null)
  189. {
  190. DeleteViewModel model = new DeleteViewModel();
  191. model.File = file;
  192. if (!string.IsNullOrEmpty(upload.DeleteKey) && upload.DeleteKey == key)
  193. {
  194. string filePath = upload.FileName;
  195. // Delete from the DB
  196. db.Uploads.Remove(upload);
  197. db.SaveChanges();
  198. // Delete the File
  199. if (System.IO.File.Exists(filePath))
  200. {
  201. System.IO.File.Delete(filePath);
  202. }
  203. model.Deleted = true;
  204. }
  205. else
  206. {
  207. model.Deleted = false;
  208. }
  209. return View(model);
  210. }
  211. return RedirectToRoute("Error.Http404");
  212. }
  213. [HttpPost]
  214. [AllowAnonymous]
  215. [ValidateAntiForgeryToken]
  216. public ActionResult GenerateDeleteKey(string file)
  217. {
  218. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  219. if (upload != null)
  220. {
  221. string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength);
  222. upload.DeleteKey = delKey;
  223. db.Entry(upload).State = EntityState.Modified;
  224. db.SaveChanges();
  225. return Json(new { result = Url.SubRouteUrl("upload", "Upload.Delete", new { file = file, key = delKey }) });
  226. }
  227. return Json(new { error = "Invalid URL" });
  228. }
  229. [HttpPost]
  230. [AllowAnonymous]
  231. [ValidateAntiForgeryToken]
  232. public ActionResult SaveFileKey(string file, string key)
  233. {
  234. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  235. if (upload != null)
  236. {
  237. upload.Key = key;
  238. db.Entry(upload).State = EntityState.Modified;
  239. db.SaveChanges();
  240. return Json(new { result = Url.SubRouteUrl("upload", "Upload.Download", new { file = file }) });
  241. }
  242. return Json(new { error = "Invalid URL" });
  243. }
  244. [HttpPost]
  245. [AllowAnonymous]
  246. [ValidateAntiForgeryToken]
  247. public ActionResult RemoveFileKey(string file, string key)
  248. {
  249. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  250. if (upload != null)
  251. {
  252. if (upload.Key == key)
  253. {
  254. upload.Key = null;
  255. db.Entry(upload).State = EntityState.Modified;
  256. db.SaveChanges();
  257. return Json(new { result = Url.SubRouteUrl("upload", "Upload.Download", new { file = file }) });
  258. }
  259. return Json(new { error = "Non-Matching Key" });
  260. }
  261. return Json(new { error = "Invalid URL" });
  262. }
  263. }
  264. }