The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UploadController.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using Teknik.Areas.Upload.Models;
  8. using Teknik.Areas.Upload.ViewModels;
  9. using Teknik.Controllers;
  10. using Teknik.Models;
  11. namespace Teknik.Areas.Upload.Controllers
  12. {
  13. public class UploadController : DefaultController
  14. {
  15. private TeknikEntities db = new TeknikEntities();
  16. // GET: Upload/Upload
  17. [HttpGet]
  18. [AllowAnonymous]
  19. public ActionResult Index()
  20. {
  21. return View(new UploadViewModel());
  22. }
  23. [HttpPost]
  24. [AllowAnonymous]
  25. [ValidateAntiForgeryToken]
  26. public ActionResult Upload(string fileType, string iv, HttpPostedFileWrapper data)
  27. {
  28. if (data.ContentLength <= Config.UploadConfig.MaxUploadSize)
  29. {
  30. Models.Upload upload = Uploader.SaveFile(data, fileType, iv);
  31. if (upload != null)
  32. {
  33. return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }) } }, "text/plain");
  34. }
  35. return Json(new { error = "Unable to upload file" });
  36. }
  37. else
  38. {
  39. return Json(new { error = "File Too Large" });
  40. }
  41. }
  42. // User did not supply key
  43. [HttpGet]
  44. [AllowAnonymous]
  45. public ActionResult Download(string file)
  46. {
  47. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  48. if (upload != null)
  49. {
  50. // We don't have the key, so we need to decrypt it client side
  51. if (upload.Key == null)
  52. {
  53. DownloadViewModel model = new DownloadViewModel();
  54. model.FileName = file;
  55. model.ContentType = upload.ContentType;
  56. model.ContentLength = upload.ContentLength;
  57. model.Key = upload.Key;
  58. model.IV = upload.IV;
  59. return View(model);
  60. }
  61. else
  62. {
  63. // decrypt it server side! Weee
  64. return View();
  65. }
  66. }
  67. else
  68. {
  69. return RedirectToRoute("Error.Http404");
  70. }
  71. }
  72. [HttpPost]
  73. [AllowAnonymous]
  74. [ValidateAntiForgeryToken]
  75. public FileResult DownloadData(string file)
  76. {
  77. Models.Upload upload = db.Uploads.Where(up => up.Url == file).FirstOrDefault();
  78. if (upload != null)
  79. {
  80. string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, upload.FileName);
  81. if (System.IO.File.Exists(filePath))
  82. {
  83. byte[] buffer;
  84. FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  85. try
  86. {
  87. int length = (int)fileStream.Length; // get file length
  88. buffer = new byte[length]; // create buffer
  89. int count; // actual number of bytes read
  90. int sum = 0; // total number of bytes read
  91. // read until Read method returns 0 (end of the stream has been reached)
  92. while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
  93. sum += count; // sum is a buffer offset for next reading
  94. }
  95. finally
  96. {
  97. fileStream.Close();
  98. }
  99. return File(buffer, System.Net.Mime.MediaTypeNames.Application.Octet, file);
  100. }
  101. }
  102. RedirectToAction("Http404", "Error", new { area = "Errors", exception = new Exception("File Not Found") });
  103. return null;
  104. }
  105. [HttpPost]
  106. [AllowAnonymous]
  107. [ValidateAntiForgeryToken]
  108. public ActionResult Delete(string url, string deleteKey)
  109. {
  110. return Json(new { result = true });
  111. }
  112. [HttpPost]
  113. [AllowAnonymous]
  114. [ValidateAntiForgeryToken]
  115. public ActionResult GenerateDeleteKey(string uploadID)
  116. {
  117. return Json(new { result = "temp-delete-key" });
  118. }
  119. }
  120. }