mojo streaming file upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Mojo::UserAgent; | |
use Mojo::Asset::File; | |
# Build a normal transaction | |
my $ua = Mojo::UserAgent->new; | |
my $tx = $ua->build_tx(POST => 'http://example.com'); | |
# Prepare body | |
my $asset = Mojo::Asset::File->new(path => $file_path); | |
$tx->req->headers->content_type('video/x-msvideo'); | |
$tx->req->headers->content_length(my $size = $asset->size); | |
# Start writing directly with a drain callback | |
my $drain; | |
my $start = 0; | |
$drain = sub { | |
my $content = shift; | |
my $chunk = $asset->get_chunk($start); | |
$start += length $chunk; | |
$drain = undef unless $start < $size; | |
$content->write($chunk, $drain); | |
}; | |
$tx->req->content->$drain; | |
# Process transaction | |
$tx = $ua->start($tx); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment