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.

APIv1Controller.cs 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.Areas.Paste;
  10. using Teknik.Controllers;
  11. using Teknik.Helpers;
  12. using Teknik.Models;
  13. using System.Text;
  14. using Teknik.Areas.Shortener.Models;
  15. using nClam;
  16. using Teknik.Filters;
  17. namespace Teknik.Areas.API.Controllers
  18. {
  19. public class APIv1Controller : DefaultController
  20. {
  21. private TeknikEntities db = new TeknikEntities();
  22. [AllowAnonymous]
  23. public ActionResult Index()
  24. {
  25. return Redirect(Url.SubRouteUrl("help", "Help.Topic", new { topic = "API" }));
  26. }
  27. [HttpPost]
  28. [AllowAnonymous]
  29. [TrackPageView]
  30. public ActionResult Upload(HttpPostedFileWrapper file, string contentType = null, bool encrypt = true, bool saveKey = true, string key = null, int keySize = 0, string iv = null, int blockSize = 0, bool genDeletionKey = false, bool doNotTrack = false)
  31. {
  32. try
  33. {
  34. ViewBag.Title = "Upload";
  35. if (file != null)
  36. {
  37. if (file.ContentLength <= Config.UploadConfig.MaxUploadSize)
  38. {
  39. // convert file to bytes
  40. byte[] fileData = null;
  41. string fileExt = Path.GetExtension(file.FileName);
  42. int contentLength = file.ContentLength;
  43. using (var binaryReader = new BinaryReader(file.InputStream))
  44. {
  45. fileData = binaryReader.ReadBytes(file.ContentLength);
  46. }
  47. // Scan the file to detect a virus
  48. if (Config.UploadConfig.VirusScanEnable)
  49. {
  50. byte[] scanData = fileData;
  51. // If it was encrypted client side, decrypt it
  52. if (!encrypt && key != null)
  53. {
  54. // If the IV is set, and Key is set, then decrypt it
  55. if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(iv))
  56. {
  57. // Decrypt the data
  58. scanData = AES.Decrypt(scanData, key, iv);
  59. }
  60. }
  61. ClamClient clam = new ClamClient(Config.UploadConfig.ClamServer, Config.UploadConfig.ClamPort);
  62. clam.MaxStreamSize = Config.UploadConfig.MaxUploadSize;
  63. ClamScanResult scanResult = clam.SendAndScanFile(scanData);
  64. switch (scanResult.Result)
  65. {
  66. case ClamScanResults.Clean:
  67. break;
  68. case ClamScanResults.VirusDetected:
  69. return Json(new { error = new { message = string.Format("Virus Detected: {0}. As per our <a href=\"{1}\">Terms of Service</a>, Viruses are not permited.", scanResult.InfectedFiles.First().VirusName, Url.SubRouteUrl("tos", "TOS.Index")) } });
  70. case ClamScanResults.Error:
  71. break;
  72. case ClamScanResults.Unknown:
  73. break;
  74. }
  75. }
  76. // Need to grab the contentType if it's empty
  77. if (string.IsNullOrEmpty(contentType))
  78. {
  79. contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType;
  80. }
  81. // Initialize the key size and block size if empty
  82. if (keySize <= 0)
  83. keySize = Config.UploadConfig.KeySize;
  84. if (blockSize <= 0)
  85. blockSize = Config.UploadConfig.BlockSize;
  86. byte[] data = null;
  87. // If they want us to encrypt the file first, do that here
  88. if (encrypt)
  89. {
  90. // Generate key and iv if empty
  91. if (string.IsNullOrEmpty(key))
  92. {
  93. key = Utility.RandomString(keySize / 8);
  94. }
  95. if (string.IsNullOrEmpty(iv))
  96. {
  97. iv = Utility.RandomString(blockSize / 8);
  98. }
  99. data = AES.Encrypt(fileData, key, iv);
  100. if (data == null || data.Length <= 0)
  101. {
  102. return Json(new { error = new { message = "Unable to encrypt file" } });
  103. }
  104. }
  105. // Save the file data
  106. Upload.Models.Upload upload = Uploader.SaveFile(db, Config, (encrypt) ? data : fileData, contentType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
  107. if (upload != null)
  108. {
  109. // Generate delete key if asked to
  110. if (genDeletionKey)
  111. {
  112. string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength);
  113. upload.DeleteKey = delKey;
  114. db.Entry(upload).State = EntityState.Modified;
  115. db.SaveChanges();
  116. }
  117. // Pull all the information together
  118. string fullUrl = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url });
  119. var returnData = new
  120. {
  121. url = (saveKey || string.IsNullOrEmpty(key)) ? fullUrl : fullUrl + "#" + key,
  122. fileName = upload.Url,
  123. contentType = contentType,
  124. contentLength = contentLength,
  125. key = key,
  126. keySize = keySize,
  127. iv = iv,
  128. blockSize = blockSize,
  129. deletionKey = upload.DeleteKey
  130. };
  131. return Json(new { result = returnData });
  132. }
  133. return Json(new { error = new { message = "Unable to save file" } });
  134. }
  135. else
  136. {
  137. return Json(new { error = new { message = "File Too Large" } });
  138. }
  139. }
  140. return Json(new { error = new { message = "Invalid Upload Request" } });
  141. }
  142. catch(Exception ex)
  143. {
  144. return Json(new { error = new { message = "Exception: " + ex.Message } });
  145. }
  146. }
  147. [HttpPost]
  148. [AllowAnonymous]
  149. [TrackPageView]
  150. public ActionResult Paste(string code, string title = "", string syntax = "text", string expireUnit = "never", int expireLength = 1, string password = "", bool hide = false, bool doNotTrack = false)
  151. {
  152. try
  153. {
  154. ViewBag.Title = "Paste";
  155. Paste.Models.Paste paste = PasteHelper.CreatePaste(code, title, syntax, expireUnit, expireLength, password, hide);
  156. db.Pastes.Add(paste);
  157. db.SaveChanges();
  158. return Json(new
  159. {
  160. result = new
  161. {
  162. id = paste.Url,
  163. url = Url.SubRouteUrl("paste", "Paste.View", new { type = "Full", url = paste.Url, password = password }),
  164. title = paste.Title,
  165. syntax = paste.Syntax,
  166. expiration = paste.ExpireDate,
  167. password = password
  168. }
  169. });
  170. }
  171. catch (Exception ex)
  172. {
  173. return Json(new { error = new { message = "Exception: " + ex.Message } });
  174. }
  175. }
  176. [HttpPost]
  177. [AllowAnonymous]
  178. [TrackPageView]
  179. public ActionResult Shorten(string url, bool doNotTrack = false)
  180. {
  181. try
  182. {
  183. ViewBag.Title = "Shorten";
  184. if (url.IsValidUrl())
  185. {
  186. ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength);
  187. db.ShortenedUrls.Add(newUrl);
  188. db.SaveChanges();
  189. string shortUrl = string.Format("{0}://{1}/{2}", HttpContext.Request.Url.Scheme, Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
  190. if (Config.DevEnvironment)
  191. {
  192. shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
  193. }
  194. return Json(new
  195. {
  196. result = new
  197. {
  198. shortUrl = shortUrl,
  199. originalUrl = url
  200. }
  201. });
  202. }
  203. return Json(new { error = new { message = "Must be a valid Url" } });
  204. }
  205. catch (Exception ex)
  206. {
  207. return Json(new { error = new { message = "Exception: " + ex.Message } });
  208. }
  209. }
  210. }
  211. }