diff options
author | Ian C <ianc@noddybox.co.uk> | 2024-03-22 12:28:07 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2024-03-22 12:28:07 +0000 |
commit | 3f05ee34d209fcb994cba50955cd2e301315d9bd (patch) | |
tree | 5cd375c2e8ecc444786286af4d1afcbcaf95227e /Pages/Index.cshtml.cs | |
parent | d71f080ad276653399c92810c9122bf537e551fe (diff) |
Initial working version.
Diffstat (limited to 'Pages/Index.cshtml.cs')
-rw-r--r-- | Pages/Index.cshtml.cs | 114 |
1 files changed, 111 insertions, 3 deletions
diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 6e9a5ed..90c18db 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -17,11 +17,14 @@ public class IndexModel : PageModel public void OnGet() { + ViewData["Title"] = "Download Repsoitory Editor"; LoadData(); } public void OnPost() { + ViewData["Title"] = "Download Repsoitory Editor"; + if (!String.IsNullOrEmpty(Request.Form["add"])) { DoAdd(); @@ -47,17 +50,122 @@ public class IndexModel : PageModel private void DoAdd() { - SetError("DoAdd"); + try + { + string? key = Request.Form["key"]; + string? mime_type = Request.Form["mime_type"]; + string? file_name = Request.Form["file_name"]; + IFormFile? file = Request.Form.Files.Count > 0 ? Request.Form.Files[0] : null; + + if (String.IsNullOrWhiteSpace(key) || + String.IsNullOrWhiteSpace(mime_type) || + String.IsNullOrEmpty(file_name) || + file == null) + { + SetError("Need key, mime type, file name and file"); + return; + } + + using var file_stream = file.OpenReadStream(); + byte[] file_data = new byte[file.Length]; + file_stream.Read(file_data, 0, (int)file.Length); + + using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); + + conn.Open(); + + using var cmd = new NpgsqlCommand("INSERT INTO file_object (key, mime_type, file_name, data) VALUES (@key, @mime_type, @file_name, @data)", conn); + + cmd.Parameters.AddWithValue("key", NpgsqlTypes.NpgsqlDbType.Varchar, key); + cmd.Parameters.AddWithValue("mime_type", NpgsqlTypes.NpgsqlDbType.Varchar, mime_type); + cmd.Parameters.AddWithValue("file_name", NpgsqlTypes.NpgsqlDbType.Varchar, file_name); + cmd.Parameters.AddWithValue("data", NpgsqlTypes.NpgsqlDbType.Bytea, file_data); + + cmd.ExecuteNonQuery(); + } + catch(Exception e) + { + SetError(e.Message); + } } private void DoDelete() { - SetError("DoDelete"); + try + { + string? key = Request.Form["original_key"]; + + using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); + + conn.Open(); + + using var delete_download = new NpgsqlCommand("DELETE FROM download WHERE key = @original_key", conn); + + delete_download.Parameters.AddWithValue("original_key", NpgsqlTypes.NpgsqlDbType.Varchar, key); + delete_download.ExecuteNonQuery(); + + using var delete_file = new NpgsqlCommand("DELETE FROM file_object WHERE key = @original_key", conn); + + delete_file.Parameters.AddWithValue("original_key", NpgsqlTypes.NpgsqlDbType.Varchar, key); + delete_file.ExecuteNonQuery(); + } + catch(Exception e) + { + SetError(e.Message); + } } private void DoUpdate() { - SetError("DoUpdate"); + try + { + string? original_key = Request.Form["original_key"]; + string? key = Request.Form["key"]; + string? mime_type = Request.Form["mime_type"]; + string? file_name = Request.Form["file_name"]; + IFormFile? file = Request.Form.Files.Count > 0 ? Request.Form.Files[0] : null; + + if (file != null) + { + using var file_stream = file.OpenReadStream(); + byte[] file_data = new byte[file.Length]; + file_stream.Read(file_data, 0, (int)file.Length); + + using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); + + conn.Open(); + + using var cmd = new NpgsqlCommand("UPDATE file_object set key=@key, mime_type=@mime_type, file_name=@file_name, data=@data WHERE key=@original_key", conn); + + cmd.Parameters.AddWithValue("original_key", NpgsqlTypes.NpgsqlDbType.Varchar, original_key); + cmd.Parameters.AddWithValue("key", NpgsqlTypes.NpgsqlDbType.Varchar, key); + cmd.Parameters.AddWithValue("mime_type", NpgsqlTypes.NpgsqlDbType.Varchar, mime_type); + cmd.Parameters.AddWithValue("file_name", NpgsqlTypes.NpgsqlDbType.Varchar, file_name); + cmd.Parameters.AddWithValue("data", NpgsqlTypes.NpgsqlDbType.Bytea, file_data); + + cmd.ExecuteNonQuery(); + } + else + { + using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); + + conn.Open(); + + using var cmd = new NpgsqlCommand("UPDATE file_object set key=@key, mime_type=@mime_type, file_name=@file_name WHERE key=@original_key", conn); + + cmd.Parameters.AddWithValue("original_key", NpgsqlTypes.NpgsqlDbType.Varchar, original_key); + cmd.Parameters.AddWithValue("key", NpgsqlTypes.NpgsqlDbType.Varchar, key); + cmd.Parameters.AddWithValue("mime_type", NpgsqlTypes.NpgsqlDbType.Varchar, mime_type); + cmd.Parameters.AddWithValue("file_name", NpgsqlTypes.NpgsqlDbType.Varchar, file_name); + + cmd.ExecuteNonQuery(); + } + + } + catch(Exception e) + { + SetError(e.Message); + } } private void LoadData() |