The next generation of the Teknik Services. Written in ASP.NET.
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 10KB

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