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.

VaultController.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using Teknik.Areas.Paste;
  8. using Teknik.Areas.Users.Models;
  9. using Teknik.Areas.Users.Utility;
  10. using Teknik.Areas.Vault.Models;
  11. using Teknik.Areas.Vault.ViewModels;
  12. using Teknik.Attributes;
  13. using Teknik.Configuration;
  14. using Teknik.Controllers;
  15. using Teknik.Models;
  16. using Teknik.Utilities;
  17. namespace Teknik.Areas.Vault.Controllers
  18. {
  19. [TeknikAuthorize]
  20. public class VaultController : DefaultController
  21. {
  22. private TeknikEntities db = new TeknikEntities();
  23. [AllowAnonymous]
  24. public ActionResult ViewVault(string id)
  25. {
  26. Models.Vault foundVault = db.Vaults.Where(v => v.Url == id).FirstOrDefault();
  27. if (foundVault != null)
  28. {
  29. // Update view count
  30. foundVault.Views += 1;
  31. db.Entry(foundVault).State = EntityState.Modified;
  32. db.SaveChanges();
  33. ViewBag.Title = foundVault.Title + " - Teknik Vault";
  34. VaultViewModel model = new VaultViewModel();
  35. model.Url = foundVault.Url;
  36. model.UserId = foundVault.UserId;
  37. model.User = foundVault.User;
  38. model.Title = foundVault.Title;
  39. model.Description = foundVault.Description;
  40. model.DateCreated = foundVault.DateCreated;
  41. model.DateEdited = foundVault.DateEdited;
  42. if (foundVault.VaultItems.Any())
  43. {
  44. foreach (VaultItem item in foundVault.VaultItems)
  45. {
  46. VaultItemViewModel itemModel = new VaultItemViewModel();
  47. itemModel.Title = item.Title;
  48. itemModel.Description = item.Description;
  49. itemModel.DateAdded = item.DateAdded;
  50. if (item.GetType().BaseType == typeof(UploadVaultItem))
  51. {
  52. UploadVaultItem upload = (UploadVaultItem)item;
  53. // Increment Views
  54. upload.Upload.Downloads += 1;
  55. db.Entry(upload.Upload).State = EntityState.Modified;
  56. db.SaveChanges();
  57. UploadItemViewModel uploadModel = new UploadItemViewModel();
  58. uploadModel.Title = item.Title;
  59. uploadModel.Description = item.Description;
  60. uploadModel.DateAdded = item.DateAdded;
  61. uploadModel.Upload = upload.Upload;
  62. model.Items.Add(uploadModel);
  63. }
  64. else if (item.GetType().BaseType == typeof(PasteVaultItem))
  65. {
  66. PasteVaultItem paste = (PasteVaultItem)item;
  67. // Increment Views
  68. paste.Paste.Views += 1;
  69. db.Entry(paste.Paste).State = EntityState.Modified;
  70. db.SaveChanges();
  71. // Check Expiration
  72. if (PasteHelper.CheckExpiration(paste.Paste))
  73. {
  74. db.Pastes.Remove(paste.Paste);
  75. db.SaveChanges();
  76. break;
  77. }
  78. PasteItemViewModel pasteModel = new PasteItemViewModel();
  79. pasteModel.Title = item.Title;
  80. pasteModel.Description = item.Description;
  81. pasteModel.DateAdded = item.DateAdded;
  82. pasteModel.Paste = paste.Paste;
  83. model.Items.Add(pasteModel);
  84. }
  85. }
  86. }
  87. return View(model);
  88. }
  89. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  90. }
  91. [HttpGet]
  92. [AllowAnonymous]
  93. public ActionResult NewVault()
  94. {
  95. ViewBag.Title = "Create Vault";
  96. ModifyVaultViewModel model = new ModifyVaultViewModel();
  97. return View("~/Areas/Vault/Views/Vault/ModifyVault.cshtml", model);
  98. }
  99. [HttpGet]
  100. [AllowAnonymous]
  101. public ActionResult NewVaultFromService(string type, string urls)
  102. {
  103. ViewBag.Title = "Create Vault";
  104. ModifyVaultViewModel model = new ModifyVaultViewModel();
  105. string[] allURLs = urls.Split(',');
  106. int index = 0;
  107. foreach (string url in allURLs)
  108. {
  109. string[] urlInfo = url.Split(':');
  110. string uploadId = urlInfo[0];
  111. string title = string.Empty;
  112. if (urlInfo.GetUpperBound(0) >= 1)
  113. {
  114. // They also passed in the original filename, so let's use it as our title
  115. title = urlInfo[1];
  116. }
  117. if (IsValidItem(type, uploadId))
  118. {
  119. ModifyVaultItemViewModel item = new ModifyVaultItemViewModel();
  120. item.isTemplate = false;
  121. item.index = index;
  122. item.title = title;
  123. item.url = uploadId;
  124. item.type = type;
  125. model.items.Add(item);
  126. index++;
  127. }
  128. }
  129. return View("~/Areas/Vault/Views/Vault/ModifyVault.cshtml", model);
  130. }
  131. [HttpGet]
  132. public ActionResult EditVault(string url)
  133. {
  134. ViewBag.Title = "Edit Vault";
  135. Vault.Models.Vault foundVault = db.Vaults.Where(v => v.Url == url).FirstOrDefault();
  136. if (foundVault != null)
  137. {
  138. if (foundVault.User.Username == User.Identity.Name)
  139. {
  140. ViewBag.Title = "Edit Vault - " + foundVault.Title;
  141. ModifyVaultViewModel model = new ModifyVaultViewModel();
  142. model.isEdit = true;
  143. model.vaultId = foundVault.VaultId;
  144. model.title = foundVault.Title;
  145. model.description = foundVault.Description;
  146. int index = 0;
  147. foreach (VaultItem item in foundVault.VaultItems)
  148. {
  149. ModifyVaultItemViewModel itemModel = new ModifyVaultItemViewModel();
  150. itemModel.index = index;
  151. itemModel.isTemplate = false;
  152. if (item.GetType().BaseType == typeof(UploadVaultItem))
  153. {
  154. UploadVaultItem upload = (UploadVaultItem)item;
  155. itemModel.title = upload.Title;
  156. itemModel.description = upload.Description;
  157. itemModel.type = "Upload";
  158. itemModel.url = upload.Upload.Url;
  159. model.items.Add(itemModel);
  160. index++;
  161. }
  162. else if (item.GetType().BaseType == typeof(PasteVaultItem))
  163. {
  164. PasteVaultItem paste = (PasteVaultItem)item;
  165. itemModel.title = paste.Title;
  166. itemModel.description = paste.Description;
  167. itemModel.type = "Paste";
  168. itemModel.url = paste.Paste.Url;
  169. model.items.Add(itemModel);
  170. index++;
  171. }
  172. }
  173. return View("~/Areas/Vault/Views/Vault/ModifyVault.cshtml", model);
  174. }
  175. return Redirect(Url.SubRouteUrl("error", "Error.Http403"));
  176. }
  177. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  178. }
  179. [HttpPost]
  180. [AllowAnonymous]
  181. [ValidateAntiForgeryToken]
  182. public ActionResult CreateVault(ModifyVaultViewModel model)
  183. {
  184. if (model != null)
  185. {
  186. if (!string.IsNullOrEmpty(model.title))
  187. {
  188. Vault.Models.Vault newVault = db.Vaults.Create();
  189. // Create a new ID
  190. string url = StringHelper.RandomString(Config.VaultConfig.UrlLength);
  191. while (db.Vaults.Where(v => v.Url == url).FirstOrDefault() != null)
  192. {
  193. url = StringHelper.RandomString(Config.VaultConfig.UrlLength);
  194. }
  195. newVault.Url = url;
  196. newVault.DateCreated = DateTime.Now;
  197. newVault.Title = model.title;
  198. newVault.Description = model.description;
  199. if (User.Identity.IsAuthenticated)
  200. {
  201. User user = UserHelper.GetUser(db, User.Identity.Name);
  202. if (user != null)
  203. {
  204. newVault.UserId = user.UserId;
  205. }
  206. }
  207. // Add/Verify items
  208. if (model.items.Any())
  209. {
  210. foreach (ModifyVaultItemViewModel item in model.items)
  211. {
  212. if (IsValidItem(item.type, item.url))
  213. {
  214. switch (item.type.ToLower())
  215. {
  216. case "upload":
  217. UploadVaultItem newUpload = new UploadVaultItem();
  218. newUpload.DateAdded = DateTime.Now;
  219. newUpload.Title = item.title;
  220. newUpload.Description = item.description;
  221. newUpload.UploadId = db.Uploads.Where(u => u.Url == item.url).FirstOrDefault().UploadId;
  222. newVault.VaultItems.Add(newUpload);
  223. break;
  224. case "paste":
  225. PasteVaultItem newPaste = new PasteVaultItem();
  226. newPaste.DateAdded = DateTime.Now;
  227. newPaste.Title = item.title;
  228. newPaste.Description = item.description;
  229. newPaste.PasteId = db.Pastes.Where(p => p.Url == item.url).FirstOrDefault().PasteId;
  230. newVault.VaultItems.Add(newPaste);
  231. break;
  232. default:
  233. return Json(new { error = new { message = "You have an invalid item type: " + item.type } });
  234. }
  235. }
  236. else
  237. {
  238. return Json(new { error = new { message = "You have an invalid item URL: " + item.url } });
  239. }
  240. }
  241. }
  242. // Add and save the new vault
  243. db.Vaults.Add(newVault);
  244. db.SaveChanges();
  245. return Json(new { result = new { url = Url.SubRouteUrl("v", "Vault.ViewVault", new { id = url }) } });
  246. }
  247. return Json(new { error = new { message = "You must supply a Title" } });
  248. }
  249. return Json(new { error = new { message = "Invalid Parameters" } });
  250. }
  251. [HttpPost]
  252. [ValidateAntiForgeryToken]
  253. public ActionResult EditVault(ModifyVaultViewModel model)
  254. {
  255. if (model != null)
  256. {
  257. Vault.Models.Vault foundVault = db.Vaults.Where(v => v.VaultId == model.vaultId).FirstOrDefault();
  258. if (foundVault != null)
  259. {
  260. if (foundVault.User.Username == User.Identity.Name)
  261. {
  262. foundVault.DateEdited = DateTime.Now;
  263. foundVault.Title = model.title;
  264. foundVault.Description = model.description;
  265. // Clear previous items
  266. List<VaultItem> vaultItems = db.VaultItems.Where(v => v.VaultId == foundVault.VaultId).ToList();
  267. if (vaultItems != null)
  268. {
  269. foreach (VaultItem item in vaultItems)
  270. {
  271. db.VaultItems.Remove(item);
  272. }
  273. }
  274. foundVault.VaultItems.Clear();
  275. // Add/Verify items
  276. if (model.items.Any())
  277. {
  278. foreach (ModifyVaultItemViewModel item in model.items)
  279. {
  280. if (IsValidItem(item.type, item.url))
  281. {
  282. switch (item.type.ToLower())
  283. {
  284. case "upload":
  285. UploadVaultItem newUpload = new UploadVaultItem();
  286. newUpload.DateAdded = DateTime.Now;
  287. newUpload.Title = item.title;
  288. newUpload.Description = item.description;
  289. newUpload.UploadId = db.Uploads.Where(u => u.Url == item.url).FirstOrDefault().UploadId;
  290. foundVault.VaultItems.Add(newUpload);
  291. break;
  292. case "paste":
  293. PasteVaultItem newPaste = new PasteVaultItem();
  294. newPaste.DateAdded = DateTime.Now;
  295. newPaste.Title = item.title;
  296. newPaste.Description = item.description;
  297. newPaste.PasteId = db.Pastes.Where(p => p.Url == item.url).FirstOrDefault().PasteId;
  298. foundVault.VaultItems.Add(newPaste);
  299. break;
  300. default:
  301. return Json(new { error = new { message = "You have an invalid item type: " + item.type } });
  302. }
  303. }
  304. else
  305. {
  306. return Json(new { error = new { message = "You have an invalid item URL: " + item.url } });
  307. }
  308. }
  309. }
  310. db.Entry(foundVault).State = EntityState.Modified;
  311. db.SaveChanges();
  312. return Json(new { result = new { url = Url.SubRouteUrl("v", "Vault.ViewVault", new { id = foundVault.Url }) } });
  313. }
  314. return Json(new { error = new { message = "You do not have permission to edit this Vault" } });
  315. }
  316. return Json(new { error = new { message = "That Vault does not exist" } });
  317. }
  318. return Json(new { error = new { message = "Invalid Parameters" } });
  319. }
  320. [HttpPost]
  321. public ActionResult DeleteVault(string url)
  322. {
  323. Vault.Models.Vault foundVault = db.Vaults.Where(v => v.Url == url).FirstOrDefault();
  324. if (foundVault != null)
  325. {
  326. if (foundVault.User.Username == User.Identity.Name)
  327. {
  328. db.Vaults.Remove(foundVault);
  329. db.SaveChanges();
  330. return Json(new { result = new { url = Url.SubRouteUrl("vault", "Vault.CreateVault") } });
  331. }
  332. return Json(new { error = new { message = "You do not have permission to edit this Vault" } });
  333. }
  334. return Json(new { error = new { message = "That Vault does not exist" } });
  335. }
  336. [HttpPost]
  337. [AllowAnonymous]
  338. [ValidateAntiForgeryToken]
  339. public ActionResult ValidateItem(string type, string url)
  340. {
  341. if (IsValidItem(type, url))
  342. {
  343. return Json(new { result = new { valid = true } });
  344. }
  345. else
  346. {
  347. return Json(new { error = new { message = "Invalid URL Id for this Item" } });
  348. }
  349. }
  350. private bool IsValidItem(string type, string url)
  351. {
  352. bool valid = false;
  353. if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(url))
  354. {
  355. switch (type.ToLower())
  356. {
  357. case "upload":
  358. Upload.Models.Upload foundUpload = db.Uploads.Where(u => u.Url == url).FirstOrDefault();
  359. if (foundUpload != null)
  360. {
  361. valid = true;
  362. }
  363. break;
  364. case "paste":
  365. Paste.Models.Paste foundPaste = db.Pastes.Where(p => p.Url == url).FirstOrDefault();
  366. if (foundPaste != null)
  367. {
  368. valid = true;
  369. }
  370. break;
  371. }
  372. }
  373. return valid;
  374. }
  375. }
  376. }