mirror of
https://codeberg.org/icewind/shelve.git
synced 2026-06-03 20:14:08 +02:00
fix put upload
This commit is contained in:
parent
4d4108c83b
commit
00cd3fe490
2 changed files with 25 additions and 8 deletions
|
|
@ -2,7 +2,7 @@ use err_derive::Error;
|
||||||
use priority_queue::PriorityQueue;
|
use priority_queue::PriorityQueue;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::fmt::Debug;
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use uuid::{Builder, Uuid, Variant, Version};
|
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]
|
#[test]
|
||||||
fn test_generate_upload_id() {
|
fn test_generate_upload_id() {
|
||||||
let id = UploadId::generate(12345);
|
let id = UploadId::generate(12345);
|
||||||
|
|
|
||||||
25
src/main.rs
25
src/main.rs
|
|
@ -57,20 +57,22 @@ const THOUSAND_YEARS: u64 = 1000 * 356 * 24 * 60 * 60;
|
||||||
async fn put_upload(
|
async fn put_upload(
|
||||||
data: Data<'_>,
|
data: Data<'_>,
|
||||||
expire: Option<u64>,
|
expire: Option<u64>,
|
||||||
name: String,
|
name: &str,
|
||||||
_token: UploadToken,
|
_token: UploadToken,
|
||||||
basedir: &State<PathBuf>,
|
basedir: &State<PathBuf>,
|
||||||
expire_queue: &State<ExpireQueue>,
|
expire_queue: &State<ExpireQueue>,
|
||||||
) -> io::Result<String> {
|
) -> io::Result<String> {
|
||||||
|
let name = <&FileName>::from(name);
|
||||||
let id = UploadId::generate(now() + expire.unwrap_or(THOUSAND_YEARS));
|
let id = UploadId::generate(now() + expire.unwrap_or(THOUSAND_YEARS));
|
||||||
expire_queue.push(id);
|
expire_queue.push(id);
|
||||||
|
|
||||||
let mut path = basedir.join(id.as_string());
|
let mut path = basedir.join(id.as_string());
|
||||||
|
let name = format_filename(name);
|
||||||
create_dir(&path)?;
|
create_dir(&path)?;
|
||||||
path.push(name);
|
path.push(&name);
|
||||||
|
|
||||||
data.open(2.gibibytes()).into_file(path).await?;
|
data.open(2.gibibytes()).into_file(path).await?;
|
||||||
Ok(id.as_string())
|
Ok(format!("{}/{}", id, name))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Responder)]
|
#[derive(Debug, Responder)]
|
||||||
|
|
@ -124,10 +126,11 @@ async fn post_upload(
|
||||||
|mut file| async move {
|
|mut file| async move {
|
||||||
let id = UploadId::generate(now() + expire.unwrap_or(THOUSAND_YEARS));
|
let id = UploadId::generate(now() + expire.unwrap_or(THOUSAND_YEARS));
|
||||||
expire_queue.push(id);
|
expire_queue.push(id);
|
||||||
let name = file.name().unwrap_or("upload");
|
let name = file
|
||||||
let ext = file.raw_name().and_then(filename_ext).unwrap_or_default();
|
.raw_name()
|
||||||
let name = format!("{}.{}", name, ext);
|
.map(format_filename)
|
||||||
let url = format!("{}/{}", id.as_string(), &name);
|
.unwrap_or_else(|| "upload.bin".into());
|
||||||
|
let url = format!("{}/{}", id, &name);
|
||||||
|
|
||||||
let mut path: PathBuf = basedir.join(id.as_string());
|
let mut path: PathBuf = basedir.join(id.as_string());
|
||||||
create_dir(&path)?;
|
create_dir(&path)?;
|
||||||
|
|
@ -254,3 +257,11 @@ fn test_ext() {
|
||||||
assert_eq!(None, filename_ext("../foo.png".into()));
|
assert_eq!(None, filename_ext("../foo.png".into()));
|
||||||
assert_eq!(None, filename_ext("tmp/../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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue