use rand distribution for file id generation

This commit is contained in:
neri 2022-11-22 15:56:38 +01:00
parent 756d4b67a0
commit 44843ab222
1 changed files with 6 additions and 7 deletions

View File

@ -7,7 +7,7 @@ use actix_files::NamedFile;
use actix_multipart::Multipart; use actix_multipart::Multipart;
use actix_web::http::header::LOCATION; use actix_web::http::header::LOCATION;
use actix_web::{error, web, Error, HttpRequest, HttpResponse}; use actix_web::{error, web, Error, HttpRequest, HttpResponse};
use rand::prelude::SliceRandom; use rand::{distributions::Slice, Rng};
use sqlx::postgres::PgPool; use sqlx::postgres::PgPool;
use std::path::PathBuf; use std::path::PathBuf;
use tokio::fs::{self, OpenOptions}; use tokio::fs::{self, OpenOptions};
@ -139,12 +139,11 @@ async fn create_unique_file(
} }
fn gen_file_id() -> String { fn gen_file_id() -> String {
let mut rng = rand::thread_rng(); let distribution = Slice::new(ID_CHARS).expect("ID_CHARS is not empty");
let mut id = String::with_capacity(5); rand::thread_rng()
for _ in 0..5 { .sample_iter(distribution)
id.push(*ID_CHARS.choose(&mut rng).expect("ID_CHARS is not empty")); .take(5)
} .collect()
id
} }
fn get_file_url(req: &HttpRequest, id: &str, name: Option<&str>) -> String { fn get_file_url(req: &HttpRequest, id: &str, name: Option<&str>) -> String {