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.

TrackDownload.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using Teknik.Configuration;
  8. using Teknik.Utilities;
  9. using Teknik.Tracking;
  10. using Microsoft.AspNetCore.Mvc.Filters;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Http.Extensions;
  13. using Microsoft.AspNetCore.Mvc;
  14. namespace Teknik.Filters
  15. {
  16. public class TrackDownloadAttribute : TypeFilterAttribute
  17. {
  18. public TrackDownloadAttribute() : base(typeof(TrackDownload))
  19. {
  20. }
  21. public class TrackDownload : ActionFilterAttribute
  22. {
  23. private readonly IBackgroundTaskQueue _queue;
  24. private readonly Config _config;
  25. public TrackDownload(IBackgroundTaskQueue queue, Config config)
  26. {
  27. _queue = queue;
  28. _config = config;
  29. }
  30. public override void OnActionExecuting(ActionExecutingContext filterContext)
  31. {
  32. }
  33. public override void OnActionExecuted(ActionExecutedContext filterContext)
  34. {
  35. HttpRequest request = filterContext.HttpContext.Request;
  36. string doNotTrack = request.Headers["DNT"];
  37. if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
  38. {
  39. string userAgent = request.Headers["User-Agent"].ToString();
  40. string clientIp = request.ClientIPFromRequest(true);
  41. string urlReferrer = request.Headers["Referer"].ToString();
  42. string url = UriHelper.GetEncodedUrl(request);
  43. // Fire and forget. Don't need to wait for it.
  44. _queue.QueueBackgroundWorkItem(async token =>
  45. {
  46. Tracking.Tracking.TrackDownload(filterContext.HttpContext, _config, userAgent, clientIp, url, urlReferrer);
  47. });
  48. }
  49. }
  50. }
  51. }
  52. }