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.6KB

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