UserAgent: Posting Mojo::Asset::Memory?

138 views
Skip to first unread message

rasta

unread,
Aug 29, 2014, 10:33:53 AM8/29/14
to mojol...@googlegroups.com
Hi guys!

I'm getting a file from the client which I then need to forward to another server.

my $file = $self->req->upload('file');

But if the file is small enough, it's just in memory...

if($file->asset->is_file){
    $post
= $ua->post($url => { 'Content-Type' => $mimetype } => form => { file => { file => $file->asset->path }} );
}else{
    what should I
do here???
}

Do I have to mess with creating a temp file first? Or can I feed the user agent with the asset somehow?

Thanks!
Rasta

Александр Грошев

unread,
Aug 29, 2014, 10:46:12 AM8/29/14
to mojol...@googlegroups.com

$file = $self->req->upload('file')->asset(Mojo::Asset::File->new);

And all assets will be stored to files.

29 авг. 2014 г. 18:33 пользователь "rasta" <hudak.r...@gmail.com> написал:
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

sri

unread,
Aug 29, 2014, 1:08:47 PM8/29/14
to mojol...@googlegroups.com
Do I have to mess with creating a temp file first? Or can I feed the user agent with the asset somehow?

No. Yes.

--
sebastian 

rasta

unread,
Aug 29, 2014, 4:29:42 PM8/29/14
to mojol...@googlegroups.com
But if I do this, the $file (Mojo::Upload) seems to contain an empty asset, as if it rewrites what was there.

my $upload = $self->req->upload('file')->asset(Mojo::Asset::File->new);
 
$self
->app->log->debug($self->app->dumper($upload->asset));

--->

[Fri Aug 29 22:15:09 2014] [debug] bless( {}, 'Mojo::Asset::File' )

rasta

unread,
Aug 29, 2014, 4:29:54 PM8/29/14
to mojol...@googlegroups.com
Example. Please.

sri

unread,
Aug 29, 2014, 4:39:47 PM8/29/14
to mojol...@googlegroups.com
Example. Please.

Where in the documentation have you looked and not found anything?

--
sebastian 

Александр Грошев

unread,
Aug 29, 2014, 4:40:02 PM8/29/14
to mojol...@googlegroups.com
Strange. It works for me.
Try to use $upload->move_to().

use File::Temp 'tempfile';
...
} else {
my $tfile = File::Temp->new;
$file->move_to($tfile->filename);
$post = $ua->post($url => { 'Content-Type' => $mimetype } => form => { file => { file => $tfile->filename }} );
}


--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.



--
С уважением, Александр Грошев.

rasta

unread,
Aug 30, 2014, 4:37:47 AM8/30/14
to mojol...@googlegroups.com
Works like a charm! Thanks Alex!!

rasta

unread,
Aug 30, 2014, 4:38:15 AM8/30/14
to mojol...@googlegroups.com

sri

unread,
Aug 30, 2014, 8:58:47 AM8/30/14
to mojol...@googlegroups.com
Looked here:...

If you look at the description of the actual method you are using and the resources it links to, you still can't figure it out?


--
sebastian 

rasta

unread,
Aug 30, 2014, 12:44:04 PM8/30/14
to mojol...@googlegroups.com
OK you've got your frag man! (:

This is the part, if anyone will be interested:

# POST request with upload streamed from asset
my $asset = Mojo::Asset::Memory->new->add_chunk('lalala');
my $tx    = $t->tx(
  POST => 'http://example.com' => form => {mytext => {file => $asset}});

So I only have to do 

$post = $ua->post($url => { 'Content-Type' => $mimetype } => form => { file => { file => $upload->asset }} );

without the need to check whether the asset is a file or not.

sri

unread,
Aug 30, 2014, 7:39:50 PM8/30/14
to mojol...@googlegroups.com
OK you've got your frag man! (:

I'm usually giving hints instead of direct answers to find weaknesses in the documentation. ;)

--
sebastian 
Reply all
Reply to author
Forward
0 new messages