Complete upload
This commit is contained in:
parent
5479c136fd
commit
d4fb5601c0
|
@ -37,7 +37,7 @@ impl Form {
|
||||||
impl Html for Form {
|
impl Html for Form {
|
||||||
fn to_html_string(&self) -> String {
|
fn to_html_string(&self) -> String {
|
||||||
let encoding = match self.encoding {
|
let encoding = match self.encoding {
|
||||||
Some(ref encoding) => format!("encoding=\"{encoding}\"", encoding = encoding),
|
Some(ref encoding) => format!("enctype=\"{encoding}\"", encoding = encoding),
|
||||||
None => format!(""),
|
None => format!(""),
|
||||||
};
|
};
|
||||||
format!(
|
format!(
|
||||||
|
@ -141,6 +141,7 @@ impl Html for Label {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Button {
|
pub struct Button {
|
||||||
|
ty: Option<String>,
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
label: String,
|
label: String,
|
||||||
}
|
}
|
||||||
|
@ -148,20 +149,30 @@ pub struct Button {
|
||||||
impl Button {
|
impl Button {
|
||||||
pub fn new(label: &str) -> Self {
|
pub fn new(label: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
ty: None,
|
||||||
name: None,
|
name: None,
|
||||||
label: label.to_owned(),
|
label: label.to_owned(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_type(mut self, ty: &str) -> Self {
|
||||||
|
self.ty = Some(ty.to_owned());
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Html for Button {
|
impl Html for Button {
|
||||||
fn to_html_string(&self) -> String {
|
fn to_html_string(&self) -> String {
|
||||||
|
let ty = match self.ty {
|
||||||
|
Some(ref ty) => format!("type={}", ty),
|
||||||
|
None => "".to_owned(),
|
||||||
|
};
|
||||||
let name = match self.name {
|
let name = match self.name {
|
||||||
Some(ref name) => format!("name={}", name),
|
Some(ref name) => format!("name={}", name),
|
||||||
None => "".to_owned(),
|
None => "".to_owned(),
|
||||||
};
|
};
|
||||||
format!(
|
format!(
|
||||||
"<button {name}>{label}</button>",
|
"<button {ty} {name}>{label}</button>",
|
||||||
name = name,
|
name = name,
|
||||||
label = self.label
|
label = self.label
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,6 +8,7 @@ use build_html::Html;
|
||||||
use bytes::Buf;
|
use bytes::Buf;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
io::Read,
|
io::Read,
|
||||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
@ -218,19 +219,23 @@ fn script(_: &mut Request) -> IronResult<Response> {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn serve_file(
|
fn serve_file<F>(
|
||||||
file: FileHandle,
|
info: FileInfo,
|
||||||
|
file: F,
|
||||||
old_etags: Option<String>,
|
old_etags: Option<String>,
|
||||||
) -> http::Result<http::Response<Vec<u8>>> {
|
) -> http::Result<http::Response<Vec<u8>>>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> Result<Vec<u8>, ReadFileError>,
|
||||||
|
{
|
||||||
match old_etags {
|
match old_etags {
|
||||||
Some(old_etags) if old_etags != file.info.hash => warp::http::Response::builder()
|
Some(old_etags) if old_etags != info.hash => warp::http::Response::builder()
|
||||||
.header("content-type", file.info.file_type)
|
.header("content-type", info.file_type)
|
||||||
.status(StatusCode::NOT_MODIFIED)
|
.status(StatusCode::NOT_MODIFIED)
|
||||||
.body(vec![]),
|
.body(vec![]),
|
||||||
_ => match file.content() {
|
_ => match file() {
|
||||||
Ok(content) => warp::http::Response::builder()
|
Ok(content) => warp::http::Response::builder()
|
||||||
.header("content-type", file.info.file_type)
|
.header("content-type", info.file_type)
|
||||||
.header("etag", file.info.hash)
|
.header("etag", info.hash)
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::OK)
|
||||||
.body(content),
|
.body(content),
|
||||||
Err(_) => warp::http::Response::builder()
|
Err(_) => warp::http::Response::builder()
|
||||||
|
@ -240,7 +245,9 @@ fn serve_file(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn collect_content(mut part: Part) -> Result<(Option<String>, Vec<u8>), String> {
|
async fn collect_content(
|
||||||
|
mut part: Part,
|
||||||
|
) -> Result<(Option<String>, Option<String>, Vec<u8>), String> {
|
||||||
let mut content: Vec<u8> = Vec::new();
|
let mut content: Vec<u8> = Vec::new();
|
||||||
|
|
||||||
while let Some(Ok(data)) = part.data().await {
|
while let Some(Ok(data)) = part.data().await {
|
||||||
|
@ -248,13 +255,17 @@ async fn collect_content(mut part: Part) -> Result<(Option<String>, Vec<u8>), St
|
||||||
reader.read_to_end(&mut content).unwrap();
|
reader.read_to_end(&mut content).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((part.filename().map(|s| s.to_owned()), content))
|
Ok((
|
||||||
|
part.content_type().map(|s| s.to_owned()),
|
||||||
|
part.filename().map(|s| s.to_owned()),
|
||||||
|
content,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn collect_multipart(
|
async fn collect_multipart(
|
||||||
mut stream: warp::filters::multipart::FormData,
|
mut stream: warp::filters::multipart::FormData,
|
||||||
) -> Result<Vec<(Option<String>, Vec<u8>)>, warp::Error> {
|
) -> Result<Vec<(Option<String>, Option<String>, Vec<u8>)>, warp::Error> {
|
||||||
let mut content: Vec<(Option<String>, Vec<u8>)> = Vec::new();
|
let mut content: Vec<(Option<String>, Option<String>, Vec<u8>)> = Vec::new();
|
||||||
|
|
||||||
while let Some(part) = stream.next().await {
|
while let Some(part) = stream.next().await {
|
||||||
match part {
|
match part {
|
||||||
|
@ -266,6 +277,40 @@ async fn collect_multipart(
|
||||||
Ok(content)
|
Ok(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn handle_upload(
|
||||||
|
form: warp::filters::multipart::FormData,
|
||||||
|
app: Arc<RwLock<Store>>,
|
||||||
|
) -> warp::http::Result<warp::http::Response<String>> {
|
||||||
|
let files = collect_multipart(form).await;
|
||||||
|
match files {
|
||||||
|
Ok(files) => {
|
||||||
|
for (_, filename, content) in files {
|
||||||
|
match filename {
|
||||||
|
Some(filename) => {
|
||||||
|
app.write().unwrap().add_file(filename, content).unwrap();
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return warp::http::Response::builder()
|
||||||
|
.status(StatusCode::BAD_REQUEST)
|
||||||
|
.body("".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_err) => {
|
||||||
|
return warp::http::Response::builder()
|
||||||
|
.status(StatusCode::BAD_REQUEST)
|
||||||
|
.body("".to_owned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// println!("file length: {:?}", files.map(|f| f.len()));
|
||||||
|
warp::http::Response::builder()
|
||||||
|
.header("location", "/")
|
||||||
|
.status(StatusCode::SEE_OTHER)
|
||||||
|
.body("".to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn main() {
|
pub async fn main() {
|
||||||
/*
|
/*
|
||||||
|
@ -315,11 +360,15 @@ pub async fn main() {
|
||||||
&std::env::var("FILE_SHARE_DIR").unwrap(),
|
&std::env::var("FILE_SHARE_DIR").unwrap(),
|
||||||
))));
|
))));
|
||||||
|
|
||||||
|
let app_filter = {
|
||||||
|
let app = app.clone();
|
||||||
|
warp::any().map(move || app.clone())
|
||||||
|
};
|
||||||
|
|
||||||
let log = warp::log("file_service");
|
let log = warp::log("file_service");
|
||||||
|
|
||||||
let root = warp::path!().and(warp::get()).map({
|
let root = warp::path!().and(warp::get()).and(app_filter.clone()).map({
|
||||||
let app = app.clone();
|
move |app: Arc<RwLock<Store>>| {
|
||||||
move || {
|
|
||||||
info!("root handler");
|
info!("root handler");
|
||||||
let app = app.read().unwrap();
|
let app = app.read().unwrap();
|
||||||
match app.list_files() {
|
match app.list_files() {
|
||||||
|
@ -341,8 +390,18 @@ pub async fn main() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let post_upload_handler = warp::path!("upload")
|
||||||
|
.and(warp::post())
|
||||||
|
.and(warp::filters::multipart::form().max_length(1024 * 1024 * 32))
|
||||||
|
.and(app_filter.clone())
|
||||||
|
.then(
|
||||||
|
|form: warp::filters::multipart::FormData, app: Arc<RwLock<Store>>| {
|
||||||
|
handle_upload(form, app)
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
let post_handler = warp::path!(String)
|
let post_delete_handler = warp::path!(String)
|
||||||
.and(warp::post())
|
.and(warp::post())
|
||||||
.and(warp::filters::body::form())
|
.and(warp::filters::body::form())
|
||||||
.map(|id: String, form: HashMap<String, String>| {
|
.map(|id: String, form: HashMap<String, String>| {
|
||||||
|
@ -365,7 +424,7 @@ pub async fn main() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get_file(&FileId::from(id))
|
.get_file(&FileId::from(id))
|
||||||
{
|
{
|
||||||
Ok(file) => serve_file(file, old_etags),
|
Ok(file) => serve_file(file.info.clone(), || file.thumbnail(), old_etags),
|
||||||
Err(_err) => warp::http::Response::builder()
|
Err(_err) => warp::http::Response::builder()
|
||||||
.status(StatusCode::NOT_FOUND)
|
.status(StatusCode::NOT_FOUND)
|
||||||
.body(vec![]),
|
.body(vec![]),
|
||||||
|
@ -382,46 +441,19 @@ pub async fn main() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get_file(&FileId::from(id))
|
.get_file(&FileId::from(id))
|
||||||
{
|
{
|
||||||
Ok(file) => serve_file(file, old_etags),
|
Ok(file) => serve_file(file.info.clone(), || file.content(), old_etags),
|
||||||
Err(_err) => warp::http::Response::builder()
|
Err(_err) => warp::http::Response::builder()
|
||||||
.status(StatusCode::NOT_FOUND)
|
.status(StatusCode::NOT_FOUND)
|
||||||
.body(vec![]),
|
.body(vec![]),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let upload = warp::path!()
|
|
||||||
.and(warp::post())
|
|
||||||
.and(warp::filters::multipart::form().max_length(1024 * 1024 * 32))
|
|
||||||
.then(|form: warp::filters::multipart::FormData| async move {
|
|
||||||
let files = collect_multipart(form).await;
|
|
||||||
/*
|
|
||||||
for (filename, content) in files {
|
|
||||||
app.write()
|
|
||||||
.unwrap()
|
|
||||||
.add_file(Some(filename), content)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
println!("file length: {:?}", files.map(|f| f.len()));
|
|
||||||
warp::reply()
|
|
||||||
});
|
|
||||||
|
|
||||||
let delete = warp::path!(String).and(warp::delete()).map(|id: String| {
|
|
||||||
println!("delete {}", id);
|
|
||||||
warp::reply()
|
|
||||||
});
|
|
||||||
|
|
||||||
/*
|
|
||||||
let server = warp::serve(
|
let server = warp::serve(
|
||||||
root.or(post_handler)
|
root.or(post_upload_handler)
|
||||||
.or(file)
|
|
||||||
.or(thumbnail)
|
.or(thumbnail)
|
||||||
.or(upload)
|
.or(file)
|
||||||
.or(delete)
|
|
||||||
.with(log),
|
.with(log),
|
||||||
);
|
);
|
||||||
*/
|
|
||||||
let server = warp::serve(root.or(upload).or(thumbnail).or(file).or(delete).with(log));
|
|
||||||
server
|
server
|
||||||
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
|
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -10,14 +10,15 @@ pub fn index(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::Htm
|
||||||
.with_header(1, "Admin list of files")
|
.with_header(1, "Admin list of files")
|
||||||
.with_html(
|
.with_html(
|
||||||
Form::new()
|
Form::new()
|
||||||
|
.with_path("/upload")
|
||||||
.with_method("post")
|
.with_method("post")
|
||||||
.with_encoding("multipart/form-data")
|
.with_encoding("multipart/form-data")
|
||||||
.with_container(
|
.with_container(
|
||||||
Container::new(ContainerType::Div)
|
Container::new(ContainerType::Div)
|
||||||
.with_html(Input::new("file", "file"))
|
.with_html(Input::new("file", "file").with_id("for-selector-input"))
|
||||||
.with_html(Label::new("for-selector-input", "Select a file")),
|
.with_html(Label::new("for-selector-input", "Select a file")),
|
||||||
)
|
)
|
||||||
.with_html(Button::new("Upload file")),
|
.with_html(Button::new("Upload file").with_type("submit")),
|
||||||
);
|
);
|
||||||
|
|
||||||
for handle in handles {
|
for handle in handles {
|
||||||
|
|
Loading…
Reference in New Issue