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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.Helpers;
  12. using Teknik.Models;
  13. using System.Text;
  14. using Teknik.Areas.Shortener.Models;
  15. namespace Teknik.Areas.API.Controllers
  16. {
  17. public class APIv1Controller : DefaultController
  18. {
  19. private TeknikEntities db = new TeknikEntities();
  20. [AllowAnonymous]
  21. public ActionResult Index()
  22. {
  23. return Redirect(Url.SubRouteUrl("help", "Help.Topic", new { topic = "API" }));
  24. }
  25. [HttpPost]
  26. [AllowAnonymous]
  27. public ActionResult Upload(HttpPostedFileWrapper file, string contentType = null, bool encrypt = false, bool saveKey = false, string key = null, int keySize = 0, string iv = null, int blockSize = 0, bool genDeletionKey = false)
  28. {
  29. try
  30. {
  31. Tracking.TrackPageView(Request, "Upload", Subdomain);
  32. if (file != null)
  33. {
  34. if (file.ContentLength <= Config.UploadConfig.MaxUploadSize)
  35. {
  36. // convert file to bytes
  37. byte[] fileData = null;
  38. string fileExt = Path.GetExtension(file.FileName);
  39. int contentLength = file.ContentLength;
  40. using (var binaryReader = new BinaryReader(file.InputStream))
  41. {
  42. fileData = binaryReader.ReadBytes(file.ContentLength);
  43. }
  44. // Need to grab the contentType if it's empty
  45. if (string.IsNullOrEmpty(contentType))
  46. {
  47. contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType;
  48. }
  49. // Initialize the key size and block size if empty
  50. if (keySize <= 0)
  51. keySize = Config.UploadConfig.KeySize;
  52. if (blockSize <= 0)
  53. blockSize = Config.UploadConfig.BlockSize;
  54. byte[] data = null;
  55. // If they want us to encrypt the file first, do that here
  56. if (encrypt)
  57. {
  58. // Generate key and iv if empty
  59. if (string.IsNullOrEmpty(key))
  60. {
  61. key = Utility.RandomString(keySize / 8);
  62. }
  63. if (string.IsNullOrEmpty(iv))
  64. {
  65. iv = Utility.RandomString(blockSize / 8);
  66. }
  67. data = AES.Encrypt(fileData, key, iv);
  68. if (data == null || data.Length <= 0)
  69. {
  70. return Json(new { error = new { message = "Unable to encrypt file" } });
  71. }
  72. }
  73. // Save the file data
  74. Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, fileExt, iv, (saveKey) ? key : null, keySize, blockSize);
  75. if (upload != null)
  76. {
  77. // Generate delete key if asked to
  78. if (genDeletionKey)
  79. {
  80. string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength);
  81. upload.DeleteKey = delKey;
  82. db.Entry(upload).State = EntityState.Modified;
  83. db.SaveChanges();
  84. }
  85. // Pull all the information together
  86. string fullUrl = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url });
  87. var returnData = new
  88. {
  89. url = (saveKey || string.IsNullOrEmpty(key)) ? fullUrl : fullUrl + "#" + key,
  90. fileName = upload.Url,
  91. contentType = contentType,
  92. contentLength = contentLength,
  93. key = key,
  94. keySize = keySize,
  95. iv = iv,
  96. blockSize = blockSize,
  97. deletionKey = upload.DeleteKey
  98. };
  99. return Json(new { result = returnData });
  100. }
  101. return Json(new { error = new { message = "Unable to save file" } });
  102. }
  103. else
  104. {
  105. return Json(new { error = new { message = "File Too Large" } });
  106. }
  107. }
  108. return Json(new { error = new { message = "Invalid Upload Request" } });
  109. }
  110. catch(Exception ex)
  111. {
  112. return Json(new { error = new { message = "Exception: " + ex.Message } });
  113. }
  114. }
  115. [HttpPost]
  116. [AllowAnonymous]
  117. public ActionResult Paste(string code, string title = "", string syntax = "auto", string expireUnit = "never", int expireLength = 1, string password = "", bool hide = false)
  118. {
  119. try
  120. {
  121. Tracking.TrackPageView(Request, "Paste", Subdomain);
  122. Paste.Models.Paste paste = PasteHelper.CreatePaste(code, title, syntax, expireUnit, expireLength, password, hide);
  123. db.Pastes.Add(paste);
  124. db.SaveChanges();
  125. return Json(new
  126. {
  127. result = new
  128. {
  129. id = paste.Url,
  130. url = Url.SubRouteUrl("paste", "Paste.View", new { type = "Full", url = paste.Url, password = password }),
  131. title = paste.Title,
  132. syntax = paste.Syntax,
  133. expiration = paste.ExpireDate,
  134. password = password
  135. }
  136. });
  137. }
  138. catch (Exception ex)
  139. {
  140. return Json(new { error = new { message = "Exception: " + ex.Message } });
  141. }
  142. }
  143. public ActionResult Shorten(string url)
  144. {
  145. try
  146. {
  147. Tracking.TrackPageView(Request, "Shorten", Subdomain);
  148. if (url.IsValidUrl())
  149. {
  150. ShortenedUrl newUrl = Shortener.Shortener.ShortenUrl(url, Config.ShortenerConfig.UrlLength);
  151. db.ShortenedUrls.Add(newUrl);
  152. db.SaveChanges();
  153. string shortUrl = string.Format("{0}://{1}/{2}", HttpContext.Request.Url.Scheme, Config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
  154. if (Config.DevEnvironment)
  155. {
  156. shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
  157. }
  158. return Json(new
  159. {
  160. result = new
  161. {
  162. shortUrl = shortUrl,
  163. originalUrl = url
  164. }
  165. });
  166. }
  167. return Json(new { error = new { message = "Must be a valid Url" } });
  168. }
  169. catch (Exception ex)
  170. {
  171. return Json(new { error = new { message = "Exception: " + ex.Message } });
  172. }
  173. }
  174. }
  175. }