Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
Use multipart crate for upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Brekenfeld committed Aug 14, 2017
1 parent 7af3d5a commit dbc5751
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ fs2 = "^0.4.2"
rust-crypto = "^0.2.36"
zip = "^0.1.0"
lazy_static = "^0.2.5"
params = "^0.6.0"
params = "0.6.0"
multipart = { version = "^0.13.1", features = ["iron"] }
semver = { git = "https://github.com/MPIB/semver.git", rev = "65820b04b0a09d35d12df81a3c45aa7564f4a491" }
quick-error = "^1.2.0"
clap = "=2.26.0"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern crate fs2;
extern crate crypto;
extern crate zip;
extern crate params;
extern crate multipart;
extern crate semver;
extern crate mustache;
extern crate lazysort;
Expand Down
2 changes: 2 additions & 0 deletions src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use mount::Mount;
use router::Router;
use persistent::{Read, Write};
use staticfile::Static;
use multipart::server::iron::Intercept;

use r2d2::Pool;
use diesel::pg::PgConnection;
Expand Down Expand Up @@ -212,6 +213,7 @@ pub fn start(pool: Pool<ConnectionManager<PgConnection>>, storage: Storage) -> L
chain.link_around(SessionManager);
chain.link_before(PathNormalizer);
chain.link_before(Logger);
chain.link_before(Intercept::default().file_size_limit((CONFIG.web.max_upload_filesize_mb * 1024 * 1024) as u64));
chain.link(Read::<ConnectionPoolKey>::both(pool));
chain.link(Read::<StorageKey>::both(storage));
chain.link(Write::<SessionStoreKey>::both(HashMap::new()));
Expand Down
13 changes: 7 additions & 6 deletions src/web/views/api/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
use iron::{Request, Response, IronResult};
use iron::status;
use persistent::Read;
use plugin::Pluggable;
use params::{Params, Value};
use multipart::server::Entries;

use std::fs::File;

use ::web::server::{ConnectionPoolKey, StorageKey};
use ::web::backend::db::{PackageVersion, User};
Expand All @@ -26,7 +27,7 @@ use ::utils::error::BackendError;
header! { (XNugetApiKey, "X-NuGet-ApiKey") => [String] }

pub fn upload(req: &mut Request) -> IronResult<Response> {
let params = req.get_ref::<Params>().unwrap().clone();
let params = req.extensions.get::<Entries>().unwrap().clone();

let apikey_raw = req.headers.get_raw("X-NuGet-ApiKey").expect("No Raw API Key");
info!("Api Key: {:?}", apikey_raw);
Expand All @@ -44,9 +45,9 @@ pub fn upload(req: &mut Request) -> IronResult<Response> {

match User::get_by_apikey(&*connection, &apikey) {
Ok(user) => {
match params.find(&["package"]) {
Some(&Value::File(ref file)) => {
match PackageVersion::new(&*connection, &user, storage, file.open().unwrap()) {
match params.files.get("package").and_then(|x| x.get(0)) {
Some(file) => {
match PackageVersion::new(&*connection, &user, storage, File::open(&file.path).unwrap()) {
Ok(_) => Ok(Response::with(status::Ok)),
Err(BackendError::PermissionDenied) => Ok(Response::with((status::Forbidden, "Only the maintainer or admin is allowed to update a package"))),
Err(err) => {
Expand Down

0 comments on commit dbc5751

Please sign in to comment.