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

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