summaryrefslogtreecommitdiff
path: root/ApiController.cs
blob: d37370273e9b7d8f7334c13e4fc2376c1cc51681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// download - the download interface
// Copyright (C) 2024 Ian Cowburn <ianc@noddybox.co.uk>
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using Npgsql;
using System.Net;

namespace download;

public class ApiController : Controller
{
    public IActionResult Get(string? key)
    {
        FileObject? file = GetDownload(key);

        if (file == null)
        {
            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,
                       new string[] {ip_address, user_agent},
                       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[] info, DateTime time)
    {
        try
        {
            using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]);

            conn.Open();

            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("info", NpgsqlTypes.NpgsqlDbType.Array|NpgsqlTypes.NpgsqlDbType.Text, info);
            cmd.Parameters.AddWithValue("time", NpgsqlTypes.NpgsqlDbType.TimestampTz, time);

            cmd.ExecuteNonQuery();
        }
        catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine("RecordDownload: Caught exception {0} - {1}", e.GetType().ToString(), e.Message);
        }
    }
}