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 10.0KB

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