feat(server): announce com validacao e persistencia + files por id

This commit is contained in:
2026-08-05 20:28:09 -03:00
parent 9c7cb1c9b8
commit 2564c33748
9 changed files with 319 additions and 2 deletions
+26 -1
View File
@@ -1,6 +1,7 @@
use rusqlite::Connection;
use rusqlite::{params, Connection};
use crate::config::Config;
use crate::error::AppResult;
use crate::models::{AnnounceRequest, FileRecord};
pub fn open(cfg: &Config) -> AppResult<Connection> {
if let Some(parent) = std::path::Path::new(&cfg.db_path).parent() {
@@ -47,4 +48,28 @@ pub fn migrate(db: &Connection) -> rusqlite::Result<()> {
END;
"#,
)
}
pub fn insert_file(db: &Connection, req: &AnnounceRequest) -> rusqlite::Result<String> {
let id = format!("{}::{}", req.peer_id, &req.hash[..12]);
let tags_json = serde_json::to_string(&req.tags).unwrap_or_else(|_| "[]".into());
db.execute(
"INSERT INTO files (id, peer_id, name, description, ftype, extension, size_bytes, hash, license, tags, created_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
params![
id, req.peer_id, req.name, req.description, req.ftype, req.extension,
req.size_bytes, req.hash, req.license, tags_json,
chrono::Utc::now().timestamp(),
],
)?;
Ok(id)
}
pub fn get_file(db: &Connection, file_id: &str) -> rusqlite::Result<FileRecord> {
db.query_row(
"SELECT id, peer_id, name, description, ftype, extension, size_bytes, hash, license, tags, created_at
FROM files WHERE id = ?1",
[file_id],
FileRecord::from_row,
)
}