The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.Upload;
  9. using Teknik.Controllers;
  10. using Teknik.Helpers;
  11. using Teknik.Models;
  12. namespace Teknik.Areas.API.Controllers
  13. {
  14. public class APIController : DefaultController
  15. {
  16. private TeknikEntities db = new TeknikEntities();
  17. [AllowAnonymous]
  18. public ActionResult Index()
  19. {
  20. return Redirect(Url.SubRouteUrl("help", "Help.Topic", new { topic = "API" }));
  21. }
  22. #region API v1 Actions
  23. [AllowAnonymous]
  24. public ActionResult Index_v1()
  25. {
  26. return Redirect(Url.SubRouteUrl("help", "Help.Topic", new { topic = "API" }));
  27. }
  28. [HttpPost]
  29. [AllowAnonymous]
  30. public ActionResult Upload_v1(HttpPostedFileWrapper file, string contentType = null, bool encrypt = false, bool saveKey = false, string key = null, int keySize = 0, string iv = null, int blockSize = 0, bool genDeletionKey = false)
  31. {
  32. if (file != null)
  33. {
  34. if (file.ContentLength <= Config.UploadConfig.MaxUploadSize)
  35. {
  36. // convert file to bytes
  37. byte[] fileData = null;
  38. int contentLength = file.ContentLength;
  39. using (var binaryReader = new BinaryReader(file.InputStream))
  40. {
  41. fileData = binaryReader.ReadBytes(file.ContentLength);
  42. }
  43. // Need to grab the contentType if it's empty
  44. if (string.IsNullOrEmpty(contentType))
  45. {
  46. contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType;
  47. }
  48. // Initialize the key size and block size if empty
  49. if (keySize <= 0)
  50. keySize = Config.UploadConfig.KeySize;
  51. if (blockSize <= 0)
  52. blockSize = Config.UploadConfig.BlockSize;
  53. byte[] data = null;
  54. // If they want us to encrypt the file first, do that here
  55. if (encrypt)
  56. {
  57. // Generate key and iv if empty
  58. if (string.IsNullOrEmpty(key))
  59. {
  60. key = Utility.RandomString(keySize);
  61. }
  62. if (string.IsNullOrEmpty(iv))
  63. {
  64. iv = Utility.RandomString(blockSize);
  65. }
  66. data = AES.Encrypt(fileData, key, iv);
  67. if (data == null || data.Length <= 0)
  68. {
  69. return Json(new { error = "Unable to encrypt file" });
  70. }
  71. }
  72. // Save the file data
  73. Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, iv, key, keySize, blockSize);
  74. if (upload != null)
  75. {
  76. // Save the key to the DB if they wanted it to be
  77. if (saveKey)
  78. {
  79. upload.Key = key;
  80. db.Entry(upload).State = EntityState.Modified;
  81. db.SaveChanges();
  82. }
  83. // Generate delete key if asked to
  84. if (genDeletionKey)
  85. {
  86. string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength);
  87. upload.DeleteKey = delKey;
  88. db.Entry(upload).State = EntityState.Modified;
  89. db.SaveChanges();
  90. }
  91. // Pull all the information together
  92. var returnData = new
  93. {
  94. name = upload.Url,
  95. url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }),
  96. contentType = contentType,
  97. contentLength = contentLength,
  98. key = key,
  99. keySize = keySize,
  100. iv = iv,
  101. blockSize = blockSize,
  102. deletionKey = upload.DeleteKey
  103. };
  104. return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }) } }, "text/plain");
  105. }
  106. return Json(new { error = "Unable to save file" });
  107. }
  108. else
  109. {
  110. return Json(new { error = "File Too Large" });
  111. }
  112. }
  113. return Json(new { error = "Invalid Upload Request" });
  114. }
  115. #endregion
  116. }
  117. }