memorable download codes

This commit is contained in:
xenofem 2022-05-01 03:28:25 -04:00
parent 511bd741dd
commit 86bdac20af
7 changed files with 31 additions and 10 deletions

View file

@ -1,6 +1,7 @@
use std::{collections::HashMap, io::ErrorKind, path::PathBuf, str::FromStr};
use log::{debug, error, info, warn};
use rand::{distributions::{Alphanumeric, DistString}, thread_rng, Rng};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use tokio::{
@ -15,6 +16,17 @@ const GIGA: u64 = 1024*1024*1024;
const DEFAULT_MAX_UPLOAD_SIZE: u64 = 16*GIGA;
const DEFAULT_MAX_STORAGE_SIZE: u64 = 64*GIGA;
pub fn gen_storage_code() -> String {
if std::env::var("TRANSBEAM_MNEMONIC_CODES").as_deref() == Ok("false") {
Alphanumeric.sample_string(&mut thread_rng(), 8)
} else {
mnemonic::to_string(thread_rng().gen::<[u8; 4]>())
}
}
pub fn is_valid_storage_code(s: &str) -> bool {
s.as_bytes().iter().all(|c| c.is_ascii_alphanumeric() || c == &b'-')
}
pub(crate) fn storage_dir() -> PathBuf {
PathBuf::from(std::env::var("TRANSBEAM_STORAGE_DIR").unwrap_or_else(|_| String::from(DEFAULT_STORAGE_DIR)))
@ -133,7 +145,7 @@ impl FileStore {
// Handle this case separately, because we don't
// want to try to delete it if it's not the sort
// of path we're expecting
if !crate::util::is_ascii_alphanumeric(&key) {
if !is_valid_storage_code(&key) {
error!("Invalid key in persistent storage: {}", key);
continue;
}