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