From 61695531dd941918da95170cb3ac49cf01b85035 Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 26 Mar 2024 23:52:34 +0000 Subject: Updates. Not working version. --- ApiController.cs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- Config.cs | 6 +++++ FileObject.cs | 18 +++++++++++++++ Program.cs | 4 ++++ download.csproj | 4 ++++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 Config.cs create mode 100644 FileObject.cs diff --git a/ApiController.cs b/ApiController.cs index 3b75419..4257554 100644 --- a/ApiController.cs +++ b/ApiController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; +using Npgsql; using System.Net; namespace download; @@ -8,6 +9,73 @@ public class ApiController : Controller { public IActionResult Get(string? key) { - return StatusCode((int)HttpStatusCode.OK, "Hello World - " + key); + FileObject? file = GetDownload(key); + + if (file == null) + { + return StatusCode((int)HttpStatusCode.NotFound, String.Empty); + } + + RecordDownload(key, + Request?.HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? "IP address unknown", + Request?.Headers?.UserAgent.ToString() ?? "User agent unknown", + DateTime.UtcNow); + + return File(file.Data, file.MimeType, file.FileName); + } + + private FileObject? GetDownload(string key) + { + FileObject? ret = null; + + try + { + using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); + + conn.Open(); + + using var cmd = new NpgsqlCommand("SELECT mime_type, file_name, data FROM file_object WHERE key = @key", conn); + cmd.Parameters.AddWithValue("key", NpgsqlTypes.NpgsqlDbType.Varchar, key); + using var reader = cmd.ExecuteReader(); + + if(reader.Read()) + { + ret = new FileObject(key, (string)reader["mime_type"], (string)reader["file_name"], (byte[])reader["data"]); + } + } + catch(Exception e) + { + System.Diagnostics.Debug.WriteLine("GetDownload: Caught exception {0} - {1}", e.GetType().ToString(), e.Message); + } + + return ret; + } + + private void RecordDownload(string key, string ip_address, string user_agent, DateTime time) + { + try + { + using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); + + 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); + } + + 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.ExecuteNonQuery(); + } + catch(Exception e) + { + System.Diagnostics.Debug.WriteLine("RecordDownload: Caught exception {0} - {1}", e.GetType().ToString(), e.Message); + } } } diff --git a/Config.cs b/Config.cs new file mode 100644 index 0000000..a361719 --- /dev/null +++ b/Config.cs @@ -0,0 +1,6 @@ +namespace download; + +public static class Config +{ + static public IConfiguration Settings {get; set;} +} diff --git a/FileObject.cs b/FileObject.cs new file mode 100644 index 0000000..00c1784 --- /dev/null +++ b/FileObject.cs @@ -0,0 +1,18 @@ +namespace download; + +public class FileObject +{ + public string Key {get;private set;} + public string MimeType {get;private set;} + public string FileName {get;private set;} + public byte[] Data {get;private set;} + + public FileObject(string key, string mimetype, string filename, byte[] data) + { + Key = key; + MimeType = mimetype; + FileName = filename; + Data = data; + } + +} diff --git a/Program.cs b/Program.cs index 1b97a78..a205fe2 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,5 @@ +using download; + var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); @@ -13,4 +15,6 @@ app.UseAuthorization(); app.MapControllerRoute(name: "default", pattern: "{controller=Api}/{action=Get}/{key}"); +Config.Settings = app.Configuration; + app.Run(); diff --git a/download.csproj b/download.csproj index 13d8127..439e5d7 100644 --- a/download.csproj +++ b/download.csproj @@ -6,4 +6,8 @@ enable + + + + -- cgit v1.2.3