This commit is contained in:
2022-09-25 06:24:45 +00:00
committed by GitHub
parent cea025482f
commit 89e08ab968
8 changed files with 95 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
pub mod config;
#[cfg(feature = "db_sqlite")]
pub mod sqlite;
#[cfg(feature = "server")]
pub mod token;
pub mod traits;
#[cfg(feature = "server")]
pub mod user;
@@ -11,6 +13,8 @@ pub use config::PixivDownloaderDbConfig;
pub use config::PixivDownloaderSqliteConfig;
#[cfg(feature = "db_sqlite")]
pub use sqlite::{PixivDownloaderSqlite, SqliteError};
#[cfg(feature = "server")]
pub use token::Token;
pub use traits::PixivDownloaderDb;
#[cfg(feature = "server")]
pub use user::User;

View File

@@ -1,8 +1,8 @@
#[cfg(feature = "server")]
use super::super::User;
use super::super::{
PixivDownloaderDb, PixivDownloaderDbConfig, PixivDownloaderDbError, PixivDownloaderSqliteConfig,
};
#[cfg(feature = "server")]
use super::super::{Token, User};
use super::SqliteError;
use bytes::BytesMut;
use futures_util::lock::Mutex;
@@ -56,10 +56,12 @@ lang TEXT,
translated TEXT
);";
const TOKEN_TABLE: &'static str = "CREATE TABLE token (
id INT,
user_id INT,
token TEXT,
created_at DATETIME,
expired_at DATETIME
expired_at DATETIME,
PRIMARY KEY (id)
);";
const USERS_TABLE: &'static str = "CREATE TABLE users (
id INT,
@@ -77,13 +79,14 @@ v3 INT,
v4 INT,
PRIMARY KEY (id)
);";
const VERSION: [u8; 4] = [1, 0, 0, 2];
const VERSION: [u8; 4] = [1, 0, 0, 3];
pub struct PixivDownloaderSqlite {
db: Mutex<Connection>,
}
impl PixivDownloaderSqlite {
#[cfg(feature = "server")]
async fn _add_root_user(
&self,
name: &str,
@@ -127,6 +130,10 @@ impl PixivDownloaderSqlite {
if db_version < [1, 0, 0, 2] {
tx.execute("ALTER TABLE users ADD is_admin BOOLEAN;", [])?;
}
if db_version < [1, 0, 0, 3] {
tx.execute("DROP TABLE token;", [])?;
tx.execute(TOKEN_TABLE, [])?;
}
self._write_version(&tx)?;
tx.commit()?;
}
@@ -187,6 +194,22 @@ impl PixivDownloaderSqlite {
Ok(tables)
}
#[cfg(feature = "server")]
async fn get_token(&self, id: u64) -> Result<Option<Token>, SqliteError> {
let con = self.db.lock().await;
Ok(con
.query_row("SELECT * FROM token WHERE id = ?;", [id], |row| {
Ok(Token {
id: row.get(0)?,
user_id: row.get(1)?,
token: row.get(2)?,
created_at: row.get(3)?,
expired_at: row.get(4)?,
})
})
.optional()?)
}
#[cfg(feature = "server")]
async fn _get_user(&self, id: u64) -> Result<Option<User>, SqliteError> {
let con = self.db.lock().await;
@@ -267,6 +290,11 @@ impl PixivDownloaderDb for PixivDownloaderSqlite {
Ok(self.get_user(0).await?.expect("Root user not found:"))
}
#[cfg(feature = "server")]
async fn get_token(&self, id: u64) -> Result<Option<Token>, PixivDownloaderDbError> {
Ok(self.get_token(id).await?)
}
#[cfg(feature = "server")]
async fn get_user(&self, id: u64) -> Result<Option<User>, PixivDownloaderDbError> {
Ok(self._get_user(id).await?)

15
src/db/token.rs Normal file
View File

@@ -0,0 +1,15 @@
use chrono::{DateTime, Utc};
/// A token in the database
pub struct Token {
/// The token ID
pub id: u64,
/// The user ID of the token
pub user_id: u64,
/// The token
pub token: String,
/// The token's creation time
pub created_at: DateTime<Utc>,
/// The token's expiration time
pub expired_at: DateTime<Utc>,
}

View File

@@ -1,7 +1,7 @@
use super::PixivDownloaderDbConfig;
use super::PixivDownloaderDbError;
#[cfg(feature = "server")]
use super::User;
use super::{Token, User};
#[async_trait]
pub trait PixivDownloaderDb {
@@ -24,6 +24,10 @@ pub trait PixivDownloaderDb {
password: &[u8],
) -> Result<User, PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// Get token by ID
/// * `id` - The token ID
async fn get_token(&self, id: u64) -> Result<Option<Token>, PixivDownloaderDbError>;
#[cfg(feature = "server")]
/// Get a user by ID
/// * `id`: The user's ID
async fn get_user(&self, id: u64) -> Result<Option<User>, PixivDownloaderDbError>;