refactor(paste): use timestamp for expiring one shot files

This commit is contained in:
orhun 2021-08-27 16:05:40 +03:00
parent 92c35fe6df
commit 13126e79ce
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 7 additions and 10 deletions

View file

@ -15,7 +15,7 @@ async fn main() -> IoResult<()> {
.expect("failed to parse config"); .expect("failed to parse config");
let server_config = config.server.clone(); let server_config = config.server.clone();
fs::create_dir_all(&server_config.upload_path)?; fs::create_dir_all(&server_config.upload_path)?;
for paste_type in &[PasteType::Url, PasteType::Oneshot, PasteType::Trash] { for paste_type in &[PasteType::Url, PasteType::Oneshot] {
fs::create_dir_all(paste_type.get_path(&server_config.upload_path))?; fs::create_dir_all(paste_type.get_path(&server_config.upload_path))?;
} }
let mut http_server = HttpServer::new(move || { let mut http_server = HttpServer::new(move || {

View file

@ -16,8 +16,6 @@ pub enum PasteType {
Oneshot, Oneshot,
/// A file that only contains an URL. /// A file that only contains an URL.
Url, Url,
/// A file that is expired or deleted.
Trash,
} }
impl<'a> TryFrom<&'a ContentDisposition> for PasteType { impl<'a> TryFrom<&'a ContentDisposition> for PasteType {
@ -42,7 +40,6 @@ impl PasteType {
Self::File => String::new(), Self::File => String::new(),
Self::Oneshot => String::from("oneshot"), Self::Oneshot => String::from("oneshot"),
Self::Url => String::from("url"), Self::Url => String::from("url"),
Self::Trash => String::from("trash"),
} }
} }

View file

@ -57,10 +57,12 @@ async fn serve(
.into_response(&request)?; .into_response(&request)?;
if paste_type.is_oneshot() { if paste_type.is_oneshot() {
fs::rename( fs::rename(
path, &path,
PasteType::Trash path.with_file_name(format!(
.get_path(&config.server.upload_path) "{}.{}",
.join(&*file), file,
util::get_system_time()?.as_millis()
)),
)?; )?;
} }
Ok(response) Ok(response)
@ -68,7 +70,6 @@ async fn serve(
PasteType::Url => Ok(HttpResponse::Found() PasteType::Url => Ok(HttpResponse::Found()
.header("Location", fs::read_to_string(&path)?) .header("Location", fs::read_to_string(&path)?)
.finish()), .finish()),
PasteType::Trash => unreachable!(),
} }
} }
@ -110,7 +111,6 @@ async fn upload(
paste.store_file(content.get_file_name()?, expiry_date, &config)? paste.store_file(content.get_file_name()?, expiry_date, &config)?
} }
PasteType::Url => paste.store_url(expiry_date, &config)?, PasteType::Url => paste.store_url(expiry_date, &config)?,
PasteType::Trash => unreachable!(),
}; };
log::info!("{} ({}) is uploaded from {}", file_name, bytes_unit, host); log::info!("{} ({}) is uploaded from {}", file_name, bytes_unit, host);
urls.push(format!( urls.push(format!(