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.

PasteAPIv1Controller.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Logging;
  8. using Teknik.Areas.API.Controllers;
  9. using Teknik.Areas.API.V1.Models;
  10. using Teknik.Areas.Paste;
  11. using Teknik.Areas.Users.Models;
  12. using Teknik.Areas.Users.Utility;
  13. using Teknik.Configuration;
  14. using Teknik.Data;
  15. using Teknik.Logging;
  16. using Teknik.Utilities;
  17. namespace Teknik.Areas.API.V1.Controllers
  18. {
  19. [Authorize(Policy = "WriteAPI")]
  20. public class PasteAPIv1Controller : APIv1Controller
  21. {
  22. public PasteAPIv1Controller(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  23. [HttpPost]
  24. [AllowAnonymous]
  25. public IActionResult Paste(PasteAPIv1Model model)
  26. {
  27. try
  28. {
  29. if (model != null && model.code != null)
  30. {
  31. Paste.Models.Paste paste = PasteHelper.CreatePaste(_config, _dbContext, model.code, model.title, model.syntax, model.expireUnit, model.expireLength, model.password);
  32. // Associate this with the user if they are logged in
  33. if (User.Identity.IsAuthenticated)
  34. {
  35. User foundUser = UserHelper.GetUser(_dbContext, User.Identity.Name);
  36. if (foundUser != null)
  37. {
  38. paste.UserId = foundUser.UserId;
  39. }
  40. }
  41. _dbContext.Pastes.Add(paste);
  42. _dbContext.SaveChanges();
  43. return Json(new
  44. {
  45. result = new
  46. {
  47. id = paste.Url,
  48. url = Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url, password = model.password }),
  49. title = paste.Title,
  50. syntax = paste.Syntax,
  51. expiration = paste.ExpireDate,
  52. password = model.password
  53. }
  54. });
  55. }
  56. return Json(new { error = new { message = "Invalid Paste Request" } });
  57. }
  58. catch (Exception ex)
  59. {
  60. return Json(new { error = new { message = "Exception: " + ex.Message } });
  61. }
  62. }
  63. }
  64. }