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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 System.Web.Mvc;
  8. using System.Web.UI;
  9. using Teknik.Configuration;
  10. using Teknik.Utilities;
  11. using Teknik.Piwik;
  12. namespace Teknik.Filters
  13. {
  14. public class TrackDownload : ActionFilterAttribute
  15. {
  16. public override void OnActionExecuting(ActionExecutingContext filterContext)
  17. {
  18. base.OnActionExecuting(filterContext);
  19. }
  20. public override void OnActionExecuted(ActionExecutedContext filterContext)
  21. {
  22. HttpRequestBase request = filterContext.HttpContext.Request;
  23. string doNotTrack = request.Headers["DNT"];
  24. if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1")
  25. {
  26. string userAgent = request.UserAgent;
  27. string ipAddress = request.ClientIPFromRequest(true);
  28. string urlReferrer = request.UrlReferrer?.ToString();
  29. string url = string.Empty;
  30. if (request.Url != null)
  31. url = request.Url.ToString();
  32. // Fire and forget. Don't need to wait for it.
  33. Task.Run(() => AsyncTrackDownload(userAgent, ipAddress, url, urlReferrer));
  34. }
  35. base.OnActionExecuted(filterContext);
  36. }
  37. private static void AsyncTrackDownload(string userAgent, string clientIp, string url, string urlReferrer)
  38. {
  39. Tracking.TrackDownload(userAgent, clientIp, url, urlReferrer);
  40. }
  41. }
  42. }