//! Attachments (oxygenhub#350): files people attach to issues, comments and //! proposals, by content in the assets store. use super::{now_ms, Pool}; #[derive(Clone, Debug, sqlx::FromRow)] pub struct Attachment { pub id: i64, pub repo_id: i64, pub name: String, pub size: i64, pub hash: String, pub content_type: String, pub uploader_id: Option, pub created_ms: i64, } pub async fn add( pool: &Pool, repo_id: i64, name: &str, size: i64, hash: &str, content_type: &str, uploader_id: Option, ) -> sqlx::Result { sqlx::query_as( "INSERT INTO attachments (repo_id, name, size, hash, content_type, uploader_id, created_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", ) .bind(repo_id) .bind(name) .bind(size) .bind(hash) .bind(content_type) .bind(uploader_id) .bind(now_ms()) .fetch_one(pool) .await } /// One of a repository's attachments. pub async fn get(pool: &Pool, repo_id: i64, id: i64) -> sqlx::Result> { sqlx::query_as("SELECT * FROM attachments WHERE repo_id = $1 AND id = $2").bind(repo_id).bind(id).fetch_optional(pool).await } /// Whether any attachment keeps the blob `hash`. pub async fn hash_in_use(pool: &Pool, hash: &str) -> sqlx::Result { let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM attachments WHERE hash = $1").bind(hash).fetch_one(pool).await?; Ok(n > 0) }