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");
let server_config = config.server.clone();
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))?;
}
let mut http_server = HttpServer::new(move || {

View File

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

View File

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