Skip to content

Commit 99fdbb2

Browse files
committed
Get multipart handling to work
1 parent 4ff69c1 commit 99fdbb2

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/lib.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use actix_web_httpauth::middleware::HttpAuthentication;
66
use futures::{future, Future};
77
use rand::distributions::Alphanumeric;
88
use rand::{thread_rng, Rng};
9-
use serde::Deserialize;
109
use std::error::Error;
1110
use std::fs;
1211
use std::fs::OpenOptions;
@@ -77,10 +76,9 @@ fn new_paste(
7776
(mp, form): (Multipart, web::Data<form_data::Form>),
7877
) -> impl Future<Item = HttpResponse, Error = form_data::Error> {
7978
form_data::handle_multipart(mp, form.get_ref().clone()).map(move |form_value| {
80-
// XXX: Can we safely unwrap form_value, thus avoiding this check?
81-
let paste = match form_value.text() {
82-
Some(paste) => paste,
83-
None => return HttpResponse::InternalServerError().into(),
79+
let paste = match form_value {
80+
form_data::Value::Map(mut form_map) => form_map.remove("paste")?.text()?,
81+
_ => return None,
8482
};
8583

8684
let mut rng = thread_rng();
@@ -99,15 +97,18 @@ fn new_paste(
9997
}
10098
};
10199

102-
let paste_url = format!("{}/{}", config.url_base, paste_id);
100+
file.write_all(paste.as_bytes()).ok()?;
103101

104-
match file.write_all(paste.as_bytes()) {
105-
Ok(_) => HttpResponse::Created()
106-
.set_header("Location", paste_url.clone())
107-
.content_type("text/plain")
108-
.body(paste_url),
109-
Err(_) => HttpResponse::InternalServerError().into(),
110-
}
102+
let paste_url = format!("{}/{}", config.url_base, paste_id);
103+
Some(HttpResponse::Created()
104+
.set_header("Location", paste_url.clone())
105+
.content_type("text/plain")
106+
.body(paste_url))
107+
})
108+
.map(|res| match res {
109+
Some(response) => response,
110+
// TODO: Add info to error response.
111+
None => HttpResponse::InternalServerError().finish(),
111112
})
112113
}
113114

0 commit comments

Comments
 (0)