diff options
| author | Ian C <ianc@noddybox.co.uk> | 2024-03-27 17:51:13 +0000 | 
|---|---|---|
| committer | Ian C <ianc@noddybox.co.uk> | 2024-03-27 17:51:13 +0000 | 
| commit | 74b07b3cba17263490d18f9ca2242cf330ea92df (patch) | |
| tree | 874bab7132350b0069d91c484317791d37921671 | |
| parent | fe62f6d46a195ae1f2d3c3b2cb75260a719b0092 (diff) | |
| -rw-r--r-- | ApiController.cs | 25 | 
1 files changed, 13 insertions, 12 deletions
| diff --git a/ApiController.cs b/ApiController.cs index 47a15d9..d373702 100644 --- a/ApiController.cs +++ b/ApiController.cs @@ -32,9 +32,16 @@ public class ApiController : Controller              return StatusCode((int)HttpStatusCode.NotFound, String.Empty);          } +        string ip_address = String.Format +            ("IP Address: {0}", +             Request?.HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? "unknown"); + +        string user_agent = String.Format +            ("User Agent: {0}", +             Request?.Headers?.UserAgent.ToString() ?? "unknown"); +          RecordDownload(key, -                       Request?.HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? "IP address unknown", -                       Request?.Headers?.UserAgent.ToString() ?? "User agent unknown", +                       new string[] {ip_address, user_agent},                         DateTime.UtcNow);          return File(file.Data, file.MimeType, file.FileName); @@ -67,7 +74,7 @@ public class ApiController : Controller          return ret;      } -    private void RecordDownload(string key, string ip_address, string user_agent, DateTime time) +    private void RecordDownload(string key, string[] info, DateTime time)      {          try          { @@ -75,17 +82,11 @@ public class ApiController : Controller              conn.Open(); -            using var cmd = new NpgsqlCommand("INSERT INTO file_object (key, ip_address, user_agent, time) VALUES (@key, @ip_address, @user_agent, @time)", conn); - -            if (user_agent.Length > 256) -            { -                user_agent = user_agent.Substring(0,256); -            } +            using var cmd = new NpgsqlCommand("INSERT INTO download (key, info, time) VALUES (@key, @info, @time)", conn);              cmd.Parameters.AddWithValue("key", NpgsqlTypes.NpgsqlDbType.Varchar, key); -            cmd.Parameters.AddWithValue("ip_address", NpgsqlTypes.NpgsqlDbType.Varchar, ip_address); -            cmd.Parameters.AddWithValue("user_agent", NpgsqlTypes.NpgsqlDbType.Varchar, user_agent); -            cmd.Parameters.AddWithValue("time", NpgsqlTypes.NpgsqlDbType.Timestamp, time); +            cmd.Parameters.AddWithValue("info", NpgsqlTypes.NpgsqlDbType.Array|NpgsqlTypes.NpgsqlDbType.Text, info); +            cmd.Parameters.AddWithValue("time", NpgsqlTypes.NpgsqlDbType.TimestampTz, time);              cmd.ExecuteNonQuery();          } | 
