The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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 11KB

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. [AllowAnonymous]
  27. public ActionResult Index()
  28. {
  29. return Redirect(Url.SubRouteUrl("help", "Help.API"));
  30. }
  31. [HttpPost]
  32. [AllowAnonymous]
  33. [TrackPageView]
  34. public ActionResult Upload(APIv1UploadModel model)
  35. {
  36. try
  37. {
  38. if (Config.UploadConfig.UploadEnabled)
  39. {
  40. if (model.file != null)
  41. {
  42. if (model.file.ContentLength <= Config.UploadConfig.MaxUploadSize)
  43. {
  44. // convert file to bytes
  45. string fileExt = Path.GetExtension(model.file.FileName);
  46. int contentLength = model.file.ContentLength;
  47. // Scan the file to detect a virus
  48. if (Config.UploadConfig.VirusScanEnable)
  49. {
  50. ClamClient clam = new ClamClient(Config.UploadConfig.ClamServer, Config.UploadConfig.ClamPort);
  51. clam.MaxStreamSize = Config.UploadConfig.MaxUploadSize;
  52. ClamScanResult scanResult = clam.SendAndScanFile(model.file.InputStream);
  53. switch (scanResult.Result)
  54. {
  55. case ClamScanResults.Clean:
  56. break;
  57. case ClamScanResults.VirusDetected:
  58. 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")) } });
  59. case ClamScanResults.Error:
  60. break;
  61. case ClamScanResults.Unknown:
  62. break;
  63. }
  64. }
  65. // Need to grab the contentType if it's empty
  66. if (string.IsNullOrEmpty(model.contentType))
  67. {
  68. model.contentType = (string.IsNullOrEmpty(model.file.ContentType)) ? "application/octet-stream" : model.file.ContentType;
  69. }
  70. // Initialize the key size and block size if empty
  71. if (model.keySize <= 0)
  72. model.keySize = Config.UploadConfig.KeySize;
  73. if (model.blockSize <= 0)
  74. model.blockSize = Config.UploadConfig.BlockSize;
  75. using (TeknikEntities db = new TeknikEntities())
  76. {
  77. // Save the file data
  78. Upload.Models.Upload upload = Uploader.SaveFile(db, Config, model.file.InputStream, model.contentType, contentLength, model.encrypt, fileExt, model.iv, model.key, model.keySize, model.blockSize);
  79. if (upload != null)
  80. {
  81. // Associate this with the user if they provided an auth key
  82. if (User.Identity.IsAuthenticated)
  83. {
  84. User foundUser = UserHelper.GetUser(db, User.Identity.Name);
  85. if (foundUser != null)
  86. {
  87. upload.UserId = foundUser.UserId;
  88. db.Entry(upload).State = EntityState.Modified;
  89. db.SaveChanges();
  90. }
  91. }
  92. // Generate delete key only if asked to
  93. if (!model.genDeletionKey)
  94. {
  95. upload.DeleteKey = string.Empty;
  96. db.Entry(upload).State = EntityState.Modified;
  97. db.SaveChanges();
  98. }
  99. // Pull all the information together
  100. string fullUrl = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url });
  101. var returnData = new
  102. {
  103. url = (model.saveKey || string.IsNullOrEmpty(model.key)) ? fullUrl : fullUrl + "#" + model.key,
  104. fileName = upload.Url,
  105. contentType = model.contentType,
  106. contentLength = contentLength,
  107. key = model.key,
  108. keySize = model.keySize,
  109. iv = model.iv,
  110. blockSize = model.blockSize,
  111. deletionKey = upload.DeleteKey
  112. };
  113. return Json(new { result = returnData });
  114. }
  115. return Json(new { error = new { message = "Unable to save file" } });
  116. }
  117. }
  118. else
  119. {
  120. return Json(new { error = new { message = "File Too Large" } });
  121. }
  122. }
  123. return Json(new { error = new { message = "Invalid Upload Request" } });
  124. }
  125. return Json(new { error = new { message = "Uploads are Disabled" } });
  126. }
  127. catch(Exception ex)
  128. {
  129. return Json(new { error = new { message = "Exception: " + ex.Message } });
  130. }
  131. }
  132. [HttpPost]
  133. [AllowAnonymous]
  134. [TrackPageView]
  135. public ActionResult Paste(APIv1PasteModel model)
  136. {
  137. try
  138. {
  139. if (model != null && model.code != null)
  140. {
  141. using (TeknikEntities db = new TeknikEntities())
  142. {
  143. Paste.Models.Paste paste = PasteHelper.CreatePaste(db, model.code, model.title, model.syntax, model.expireUnit, model.expireLength, model.password, model.hide);
  144. // Associate this with the user if they are logged in
  145. if (User.Identity.IsAuthenticated)
  146. {
  147. User foundUser = UserHelper.GetUser(db, User.Identity.Name);
  148. if (foundUser != null)
  149. {
  150. paste.UserId = foundUser.UserId;
  151. }
  152. }
  153. db.Pastes.Add(paste);
  154. db.SaveChanges();
  155. return Json(new
  156. {
  157. result = new
  158. {
  159. id = paste.Url,
  160. url = Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url, password = model.password }),
  161. title = paste.Title,
  162. syntax = paste.Syntax,
  163. expiration = paste.ExpireDate,
  164. password = model.password
  165. }
  166. });
  167. }
  168. }
  169. return Json(new { error = new { message = "Invalid Paste Request" } });
  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(APIv1ShortenModel model)
  180. {
  181. try
  182. {
  183. if (model.url.IsValidUrl())
  184. {
  185. using (TeknikEntities db = new TeknikEntities())
  186. {
  187. ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(db, 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. }
  214. return Json(new { error = new { message = "Must be a valid Url" } });
  215. }
  216. catch (Exception ex)
  217. {
  218. return Json(new { error = new { message = "Exception: " + ex.Message } });
  219. }
  220. }
  221. }
  222. }