From 44843ab222754659c661f305af3dcb4add56ea48 Mon Sep 17 00:00:00 2001 From: neri Date: Tue, 22 Nov 2022 15:56:38 +0100 Subject: [PATCH] use rand distribution for file id generation --- src/upload.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index 4b2e912..ba14219 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -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 {