Add random id generator

This commit is contained in:
Raphaël Thériault 2020-01-15 14:48:02 -05:00
parent f0714b2945
commit 35db5f64d4
3 changed files with 36 additions and 13 deletions

11
Cargo.lock generated
View File

@ -87,7 +87,7 @@ dependencies = [
"mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pin-project 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.16.9 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
@ -370,7 +370,7 @@ dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -708,6 +708,7 @@ dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libsqlite3-sys 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1316,7 +1317,7 @@ dependencies = [
[[package]]
name = "rand"
version = "0.7.2"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1696,7 +1697,7 @@ dependencies = [
"idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2132,7 +2133,7 @@ dependencies = [
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum r2d2 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e48fa64898ef0286b6ee4b4d8f61483f9182acf5e44e62a398b1c7f56f2f861d"
"checksum rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412"
"checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
"checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853"
"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"

View File

@ -30,6 +30,7 @@ env_logger = "0.7.1"
futures = "0.3.1"
lazy_static = "1.4.0"
num_cpus = "1.11.1"
rand = "0.7.3"
toml = "0.5.5"
[dependencies.diesel]
version = "1.4.3"

View File

@ -169,12 +169,33 @@ macro_rules! delete {
}
/// Verify if an entry exists
macro_rules! exists {
macro_rules! random_id {
($m:ident) => {
pub async fn exists(id: i32, pool: actix_web::web::Data<Pool>) -> bool {
match actix_web::web::block(move || crate::queries::$m::find(id, pool)).await {
Ok(_) => true,
Err(_) => false,
use rand::Rng;
pub async fn random_id(pool: &actix_web::web::Data<Pool>) -> Result<i32, actix_web::Error> {
let mut rng = rand::thread_rng();
loop {
let id = rng.gen();
let pool = pool.clone();
match actix_web::web::block(move || crate::queries::$m::find(id, pool)).await {
Ok(_) => continue,
Err(e) => match e {
actix_web::error::BlockingError::Error(e) => match e {
diesel::result::Error::NotFound => return Ok(id),
_ => {
return Err(actix_web::HttpResponse::InternalServerError()
.body("Internal server error")
.into())
}
},
actix_web::error::BlockingError::Canceled => {
return Err(actix_web::HttpResponse::InternalServerError()
.body("Internal server error")
.into())
}
},
}
}
}
};
@ -294,6 +315,8 @@ pub mod files {
};
select!(files);
delete!(files);
random_id!(files);
/// GET a file entry and statically serve it
pub async fn get(
@ -413,7 +436,7 @@ pub mod files {
) -> Result<HttpResponse, Error> {
auth(identity, request, &password_hash).await?;
let id = parse_id(&path)?;
let id = random_id(&pool).await?;
let (mut path, mut relative_path) = setup(&config).await?;
let mut field = match body.next().await {
@ -476,8 +499,6 @@ pub mod files {
query(id, relative_path, pool).await
}
delete!(files);
}
pub mod links {