punt/src/lib.rs

26 lines
637 B
Rust

use rand::{Rng, distributions::Alphanumeric};
use tokio::fs::create_dir;
use std::io::ErrorKind;
use rocket::fs::TempFile;
pub fn gen_id() -> String {
rand::thread_rng()
.sample_iter(Alphanumeric)
.map(|c| c as char)
.take(16)
.collect()
}
pub async fn persist_file(file: &mut TempFile<'_>, filename: &str) -> std::io::Result<()> {
let dst = format!("uploads/{filename}");
match file.persist_to(&dst).await {
Err(e) if e.kind() == ErrorKind::NotFound => {
create_dir("uploads").await?;
file.persist_to(&dst).await
},
other => other,
}
}