feat: void-cat redirects

This commit is contained in:
2024-10-15 12:17:03 +01:00
parent c86631423d
commit b90995f07f
9 changed files with 119 additions and 61 deletions

View File

@ -7,6 +7,7 @@ use nostr::bitcoin::base58;
use route96::db::{Database, FileUpload};
use route96::filesystem::FileStore;
use route96::settings::Settings;
use route96::void_db::{VoidCatDb, VoidFile};
use sqlx::FromRow;
use sqlx_postgres::{PgPool, PgPoolOptions};
use std::path::PathBuf;
@ -149,62 +150,3 @@ async fn migrate_file(
db.add_file(&fu, uid).await?;
Ok(())
}
#[derive(FromRow)]
struct VoidFile {
#[sqlx(rename = "Id")]
pub id: Uuid,
#[sqlx(rename = "Name")]
pub name: Option<String>,
#[sqlx(rename = "Size")]
pub size: i64,
#[sqlx(rename = "Uploaded")]
pub uploaded: DateTime<Utc>,
#[sqlx(rename = "Description")]
pub description: Option<String>,
#[sqlx(rename = "MimeType")]
pub mime_type: String,
#[sqlx(rename = "Digest")]
pub digest: String,
#[sqlx(rename = "MediaDimensions")]
pub media_dimensions: Option<String>,
#[sqlx(rename = "Email")]
pub email: String,
}
impl VoidFile {
fn map_to_path(&self) -> PathBuf {
let id_str = self.id.as_hyphenated().to_string();
PathBuf::new()
.join("files-v2/")
.join(&id_str[..2])
.join(&id_str[2..4])
.join(&id_str)
}
}
struct VoidCatDb {
pub pool: PgPool,
}
impl VoidCatDb {
async fn connect(conn: &str) -> Result<Self, sqlx::Error> {
let pool = PgPoolOptions::new()
.max_connections(50)
.connect(conn)
.await?;
Ok(Self { pool })
}
async fn list_files(&self, page: usize) -> Result<Vec<VoidFile>, sqlx::Error> {
let page_size = 100;
sqlx::query_as(format!("select f.\"Id\", f.\"Name\", CAST(f.\"Size\" as BIGINT) \"Size\", f.\"Uploaded\", f.\"Description\", f.\"MimeType\", f.\"Digest\", f.\"MediaDimensions\", u.\"Email\"
from \"Files\" f, \"UserFiles\" uf, \"Users\" u
where f.\"Id\" = uf.\"FileId\"
and uf.\"UserId\" = u.\"Id\"
and u.\"AuthType\" = 4\
offset {} limit {}", page * page_size, page_size).as_str())
.fetch_all(&self.pool)
.await
}
}