Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileParts always empty #12

Closed
Gvstavo opened this issue Feb 18, 2021 · 4 comments
Closed

FileParts always empty #12

Gvstavo opened this issue Feb 18, 2021 · 4 comments

Comments

@Gvstavo
Copy link

Gvstavo commented Feb 18, 2021

I'm trying to upload a file using a form :

<html>
  <head>
    <meta charset="UTF-8" />
    <title>Catdex</title>
    <link rel="stylesheet" href="static/css/index.css" type="text/css">
  </head>
  <body>
    <h1>Add a new cat</h1>
    <form action="add_cat_form" method="post" enctype="multipart/form-data">
      <label for="name">Name:</label>
      <input type="text" name="name" id="name" value="" />
      <label for="image">Image:</label>
      <input type="file" name="image" id="image" value="" />
      <button type="submit">Submit</button>
    </form>
  </body>
</html>

I get normally the text part, but the image part

Parts { texts: TextParts([("name", b"text")]), files: FileParts([]) } 

Code:

use actix_files::Files;
use actix_web::{http , web, App , Error, HttpResponse, HttpServer};
use actix_web::FromRequest;
use handlebars::Handlebars;
use serde::{Serialize, Deserialize};
use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel::r2d2::{self , ConnectionManager};
use awmp::Parts;
use std::collections::HashMap;	

async fn add_cat_form(pool: web::Data<DbPool>, mut parts: Parts) -> Result<HttpResponse, Error>{

	let file_path = parts.files.take("image").pop().and_then(|f| f.persist_in("./static/image").ok()).unwrap_or_default();
	println!("{:?}",parts);      
	let text_fields : HashMap<_,_> = parts.texts.as_pairs().into_iter().collect();
	let connection = pool.get().expect("Can't get db connection from pool");
	let new_cat = NewCat{
		name: text_fields.get("name").unwrap().to_string(),
		image_path: file_path.to_string_lossy().to_string()
	};
	web::block(move || {
		diesel::insert_into(cats)
		.values(&new_cat)
		.execute(&connection)
	})
	.await
	.map_err(|_| {
		HttpResponse::InternalServerError().finish()
	})?;

	Ok(HttpResponse::SeeOther().header(http::header::LOCATION, "/").finish())
}

Rust version : 1.46.0 (04488afe3 2020-08-24
awmp version: 0.6.0

@tengkuizdihar
Copy link
Contributor

Coming from the example, please refer to the line where you need to configure the awmp configuration itself.

#[actix_rt::main]
async fn main() -> Result<(), std::io::Error> {
    actix_web::HttpServer::new(move || {
        actix_web::App::new()
            .data(awmp::Parts::configure(|cfg| cfg.with_file_limit(1_000_000)))
            .route("/", actix_web::web::post().to(upload))
    })
    .bind("0.0.0.0:3000")?
    .run()
    .await
}

.data(awmp::Parts::configure(|cfg| cfg.with_file_limit(1_000_000))) is the line that you're looking for.

@cybertanyellow
Copy link

I also have this similar problem.
So I tried to run v2 example and also have the same issue.

❯ cargo run --no-default-features --features v2 --example v2
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/examples/v2`

And then used curl to upload file

❯ curl -F xxx=yyy -F file=@README.md http://localhost:3000/
Text parts: xxx=yyy, File parts: 

But no such issue in v3 example

❯ cargo run --example v3                                    
   Compiling awmp v0.6.1 (/home/yellow/rust/awmp)
    Finished dev [unoptimized + debuginfo] target(s) in 9.33s
     Running `target/debug/examples/v3`

The same curl command is OK to upload success

❯ curl -F xxx=yyy -F file=@README.md http://localhost:3000/
Text parts: xxx=yyy, File parts: file: /tmp/README.md

@kardeiz
Copy link
Owner

kardeiz commented Jun 24, 2021

Hi @cybertanyellow, sorry, this is just a case of a bad example.

At one point, I was using example v2 to try out the file size limit feature:

App::new()
            .data(awmp::Parts::configure(|cfg| cfg.with_file_limit(1)))
            .route("/", web::post().to(upload))

This means that any non-empty file is going to be filtered out. I've updated the example now to have the same limit here as v3. Sorry for the confusingness.

@cybertanyellow
Copy link

Hi @kardeiz,

All workable.
I am so careless about too limit(1) value in .with_file_limit() method.

Thanks your support.

@kardeiz kardeiz closed this as completed Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants