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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. string fileKey = upload.Key;
  95. // Associate this with the user if they provided an auth key
  96. if (User.Identity.IsAuthenticated)
  97. {
  98. User foundUser = UserHelper.GetUser(db, User.Identity.Name);
  99. if (foundUser != null)
  100. {
  101. upload.UserId = foundUser.UserId;
  102. db.Entry(upload).State = EntityState.Modified;
  103. db.SaveChanges();
  104. }
  105. }
  106. // Generate delete key only if asked to
  107. if (!model.genDeletionKey)
  108. {
  109. upload.DeleteKey = string.Empty;
  110. db.Entry(upload).State = EntityState.Modified;
  111. db.SaveChanges();
  112. }
  113. // remove the key if we don't want to save it
  114. if (!model.saveKey)
  115. {
  116. upload.Key = null;
  117. db.Entry(upload).State = EntityState.Modified;
  118. db.SaveChanges();
  119. }
  120. // Pull all the information together
  121. string fullUrl = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url });
  122. var returnData = new
  123. {
  124. url = (model.saveKey || string.IsNullOrEmpty(fileKey)) ? fullUrl : fullUrl + "#" + fileKey,
  125. fileName = upload.Url,
  126. contentType = upload.ContentType,
  127. contentLength = upload.ContentLength,
  128. key = fileKey,
  129. keySize = upload.KeySize,
  130. iv = upload.IV,
  131. blockSize = upload.BlockSize,
  132. deletionKey = upload.DeleteKey
  133. };
  134. return Json(new { result = returnData });
  135. }
  136. return Json(new { error = new { message = "Unable to save file" } });
  137. }
  138. }
  139. else
  140. {
  141. return Json(new { error = new { message = "File Too Large" } });
  142. }
  143. }
  144. return Json(new { error = new { message = "Invalid Upload Request" } });
  145. }
  146. return Json(new { error = new { message = "Uploads are Disabled" } });
  147. }
  148. catch(Exception ex)
  149. {
  150. return Json(new { error = new { message = "Exception: " + ex.Message } });
  151. }
  152. }
  153. [HttpPost]
  154. [AllowAnonymous]
  155. [TrackPageView]
  156. public ActionResult Paste(APIv1PasteModel model)
  157. {
  158. try
  159. {
  160. if (model != null && model.code != null)
  161. {
  162. using (TeknikEntities db = new TeknikEntities())
  163. {
  164. Paste.Models.Paste paste = PasteHelper.CreatePaste(db, model.code, model.title, model.syntax, model.expireUnit, model.expireLength, model.password, model.hide);
  165. // Associate this with the user if they are logged in
  166. if (User.Identity.IsAuthenticated)
  167. {
  168. User foundUser = UserHelper.GetUser(db, User.Identity.Name);
  169. if (foundUser != null)
  170. {
  171. paste.UserId = foundUser.UserId;
  172. }
  173. }
  174. db.Pastes.Add(paste);
  175. db.SaveChanges();
  176. return Json(new
  177. {
  178. result = new
  179. {
  180. id = paste.Url,
  181. url = Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url, password = model.password }),
  182. title = paste.Title,
  183. syntax = paste.Syntax,
  184. expiration = paste.ExpireDate,
  185. password = model.password
  186. }
  187. });
  188. }
  189. }
  190. return Json(new { error = new { message = "Invalid Paste Request" } });
  191. }
  192. catch (Exception ex)
  193. {
  194. return Json(new { error = new { message = "Exception: " + ex.Message } });
  195. }
  196. }
  197. [HttpPost]
  198. [AllowAnonymous]
  199. [TrackPageView]
  200. public ActionResult Shorten(APIv1ShortenModel model)
  201. {
  202. try
  203. {
  204. if (model.url.IsValidUrl())
  205. {
  206. using (TeknikEntities db = new TeknikEntities())
  207. {
  208. ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(db, model.url, Config.ShortenerConfig.UrlLength);
  209. // Associate this with the user if they are logged in
  210. if (User.Identity.IsAuthenticated)
  211. {
  212. User foundUser = UserHelper.GetUser(db, User.Identity.Name);
  213. if (foundUser != null)
  214. {
  215. newUrl.UserId = foundUser.UserId;
  216. }
  217. }
  218. db.ShortenedUrls.Add(newUrl);
  219. db.SaveChanges();
  220. string shortUrl = string.Format("{0}://{1}/{2}", HttpContext.Request.Url.Scheme, Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
  221. if (Config.DevEnvironment)
  222. {
  223. shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
  224. }
  225. return Json(new
  226. {
  227. result = new
  228. {
  229. shortUrl = shortUrl,
  230. originalUrl = model.url
  231. }
  232. });
  233. }
  234. }
  235. return Json(new { error = new { message = "Must be a valid Url" } });
  236. }
  237. catch (Exception ex)
  238. {
  239. return Json(new { error = new { message = "Exception: " + ex.Message } });
  240. }
  241. }
  242. }
  243. }