forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
73 lines (56 loc) · 2.26 KB
/
mod.rs
File metadata and controls
73 lines (56 loc) · 2.26 KB
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
pub mod driver;
pub mod error;
pub mod mysql;
pub mod sqlite;
use async_trait::async_trait;
use self::driver::Driver;
use self::error::Error;
use crate::databases::mysql::Mysql;
use crate::databases::sqlite::Sqlite;
use crate::protocol::info_hash::InfoHash;
use crate::tracker::auth;
/// # Errors
///
/// Will return `r2d2::Error` if `db_path` is not able to create a database.
pub fn connect(db_driver: &Driver, db_path: &str) -> Result<Box<dyn Database>, r2d2::Error> {
let database: Box<dyn Database> = match db_driver {
Driver::Sqlite3 => {
let db = Sqlite::new(db_path)?;
Box::new(db)
}
Driver::MySQL => {
let db = Mysql::new(db_path)?;
Box::new(db)
}
};
database.create_database_tables().expect("Could not create database tables.");
Ok(database)
}
#[async_trait]
pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Error` if unable to create own tables.
fn create_database_tables(&self) -> Result<(), Error>;
async fn load_persistent_torrents(&self) -> Result<Vec<(InfoHash, u32)>, Error>;
async fn load_keys(&self) -> Result<Vec<auth::Key>, Error>;
async fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error>;
async fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error>;
async fn get_info_hash_from_whitelist(&self, info_hash: &str) -> Result<InfoHash, Error>;
async fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;
async fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;
async fn get_key_from_keys(&self, key: &str) -> Result<auth::Key, Error>;
async fn add_key_to_keys(&self, auth_key: &auth::Key) -> Result<usize, Error>;
async fn remove_key_from_keys(&self, key: &str) -> Result<usize, Error>;
async fn is_info_hash_whitelisted(&self, info_hash: &InfoHash) -> Result<bool, Error> {
self.get_info_hash_from_whitelist(&info_hash.clone().to_string())
.await
.map_or_else(
|e| match e {
Error::QueryReturnedNoRows => Ok(false),
e => Err(e),
},
|_| Ok(true),
)
}
}