1
0
Fork 0
mirror of https://codeberg.org/icewind/shelve.git synced 2026-06-03 12:04:09 +02:00

fix put upload

This commit is contained in:
Robin Appelman 2021-07-30 15:28:47 +02:00
commit 00cd3fe490
2 changed files with 25 additions and 8 deletions

View file

@ -2,7 +2,7 @@ use err_derive::Error;
use priority_queue::PriorityQueue;
use std::cmp::Ordering;
use std::convert::TryInto;
use std::fmt::Debug;
use std::fmt::{Debug, Display, Formatter};
use std::sync::{Arc, Mutex};
use uuid::{Builder, Uuid, Variant, Version};
@ -69,6 +69,12 @@ impl UploadId {
}
}
impl Display for UploadId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0.to_simple(), f)
}
}
#[test]
fn test_generate_upload_id() {
let id = UploadId::generate(12345);

View file

@ -57,20 +57,22 @@ const THOUSAND_YEARS: u64 = 1000 * 356 * 24 * 60 * 60;
async fn put_upload(
data: Data<'_>,
expire: Option<u64>,
name: String,
name: &str,
_token: UploadToken,
basedir: &State<PathBuf>,
expire_queue: &State<ExpireQueue>,
) -> io::Result<String> {
let name = <&FileName>::from(name);
let id = UploadId::generate(now() + expire.unwrap_or(THOUSAND_YEARS));
expire_queue.push(id);
let mut path = basedir.join(id.as_string());
let name = format_filename(name);
create_dir(&path)?;
path.push(name);
path.push(&name);
data.open(2.gibibytes()).into_file(path).await?;
Ok(id.as_string())
Ok(format!("{}/{}", id, name))
}
#[derive(Debug, Responder)]
@ -124,10 +126,11 @@ async fn post_upload(
|mut file| async move {
let id = UploadId::generate(now() + expire.unwrap_or(THOUSAND_YEARS));
expire_queue.push(id);
let name = file.name().unwrap_or("upload");
let ext = file.raw_name().and_then(filename_ext).unwrap_or_default();
let name = format!("{}.{}", name, ext);
let url = format!("{}/{}", id.as_string(), &name);
let name = file
.raw_name()
.map(format_filename)
.unwrap_or_else(|| "upload.bin".into());
let url = format!("{}/{}", id, &name);
let mut path: PathBuf = basedir.join(id.as_string());
create_dir(&path)?;
@ -254,3 +257,11 @@ fn test_ext() {
assert_eq!(None, filename_ext("../foo.png".into()));
assert_eq!(None, filename_ext("tmp/../foo.png".into()));
}
fn format_filename(name: &FileName) -> String {
let name_no_ext = name.as_str().unwrap_or("upload");
match filename_ext(name) {
Some(ext) => format!("{}.{}", name_no_ext, ext),
None => name_no_ext.into(),
}
}