// download - the admin console and reports // Copyright (C) 2024 Ian Cowburn // // 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 . // using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Npgsql; using System.Collections.Generic; using download_admin; using Microsoft.AspNetCore.Http.HttpResults; using System.Text; using System.Text.Encodings; namespace download_admin.Pages; public class ReportModel : PageModel { public void OnGet() { ViewData["Title"] = "Download Repsoitory Report"; GetData(); } public IActionResult OnPost() { GetData(); StringBuilder str = new StringBuilder(); foreach (var row in Rows) { str.AppendFormat("{0},{1}", row.Time.ToString("o", System.Globalization.CultureInfo.InvariantCulture), row.Key); foreach (var s in row.Info) { str.AppendFormat(",{0}", s); } str.AppendLine(); } byte[] file_data = UTF8Encoding.UTF8.GetBytes(str.ToString()); return File(file_data, "text/csv","downloads.csv"); } private void GetData() { try { Rows = new List(); using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); conn.Open(); using var cmd = new NpgsqlCommand("SELECT key, info, time FROM download ORDER BY time", conn); using var reader = cmd.ExecuteReader(); while(reader.Read()) { string key = (string)reader["key"]; string[] info = (string[])reader["info"]; DateTime time = (DateTime)reader["time"]; Rows.Add(new Download(key, info, time)); } } catch(Exception e) { SetError(e.Message); } } private void SetError(string error_text) { HasError = true; ErrorText = error_text; } public List Keys { get { var keys = new List(); try { using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); conn.Open(); using var cmd = new NpgsqlCommand("SELECT key FROM file_object ORDER BY key", conn); using var reader = cmd.ExecuteReader(); while(reader.Read()) { string key = (string)reader["key"]; keys.Add(key); } } catch(Exception e) { SetError(e.Message); } return keys; } } public long GetCount(string key) { try { using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); conn.Open(); using var cmd = new NpgsqlCommand("SELECT count(*) FROM download where key = @key", conn); cmd.Parameters.AddWithValue("key", NpgsqlTypes.NpgsqlDbType.Varchar, key); object? scalar = cmd.ExecuteScalar(); if (scalar != null) { return (long)scalar; } } catch(Exception e) { SetError(e.Message); } return 0; } public List? Rows {get;private set;} public bool HasError {get; private set;} public string? ErrorText {get; private set;} }