Google::Code::Upload - uploading files to a Google Code project.


Google-Code-Upload documentation Contained in the Google-Code-Upload distribution.

Index


Code Index:

NAME

Top

Google::Code::Upload - uploading files to a Google Code project.

SYNOPSIS

Top

    use Google::Code::Upload qw/upload/;

    upload( $file, $project_name, $username, $password, $summary, $labels );

DESCRIPTION

Top

It's an incomplete Perl port of http://support.googlecode.com/svn/trunk/scripts/googlecode_upload.py

basically you need googlecode_upload script instead.

METHODS

Top

upload

    upload( $file, $project_name, $username, $password, $summary, $labels );

AUTHOR

Top

Fayland Lam, <fayland at gmail.com>

COPYRIGHT & LICENSE

Top


Google-Code-Upload documentation Contained in the Google-Code-Upload distribution.

package Google::Code::Upload;

use warnings;
use strict;
use File::Spec ();
use File::Basename ();
use List::MoreUtils qw/natatime/;
use MIME::Base64;
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Request;

use base 'Exporter';
use vars qw/@EXPORT_OK/;
@EXPORT_OK = qw/ upload /;

our $VERSION = '0.05';
our $AUTHORITY = 'cpan:FAYLAND';

sub upload {
    my ( $file, $project_name, $username, $password, $summary, $labels ) = @_;
    
    $labels ||= [];
    if ( $username =~ /^(.*?)\@gmail\.com$/ ) {
        $username = $1;
    }

    my @form_fields = (
        summary => $summary,
    );
    push @form_fields, ( label => $_ ) foreach (@$labels);
    
    my ( $content_type, $body ) = encode_upload_request(\@form_fields, $file);
    
    my $upload_uri  = "https://$project_name.googlecode.com/files";
    my $auth_token  = encode_base64("$username:$password", '');

    my $header = HTTP::Headers->new;
    $header->header('Authorization' => "Basic $auth_token");
    $header->header('User-Agent' => 'Googlecode.com uploader v0.9.4');
    $header->header('Content-Type' => $content_type);
  
    my $ua = LWP::UserAgent->new(
        agent => 'Googlecode.com uploader v0.9.4',
    );
    my $request = HTTP::Request->new(POST =>$upload_uri, $header, $body);
    my $response = $ua->request($request);

    if ($response->code == 201) {
        return ( $response->code, $response->status_line, $response->header('Location') );
    } else {
        return ( $response->code, $response->status_line, undef );
    }
}

sub encode_upload_request {
    my ($form_fields, $file) = @_;
    
    my $BOUNDARY = '----------Googlecode_boundary_reindeer_flotilla';
    my $CRLF = "\r\n";

    my @body;
    
    my $it = natatime 2, @$form_fields;
    while (my ( $key, $val ) = $it->()) {
        push @body, (
            "--$BOUNDARY",
            qq~Content-Disposition: form-data; name="$key"~,
            '',
            $val
        );
    }
    
    my $filename = File::Basename::basename($file);
    open(my $fh, '<', $file) or die $!;
    binmode($fh);
    my $content = do {
        local $/;
        <$fh>;
    };
    close($fh);
    
    push @body, (
        "--$BOUNDARY",
        qq~Content-Disposition: form-data; name="filename"; filename="$filename"~,
        # The upload server determines the mime-type, no need to set it.
        'Content-Type: application/octet-stream',
        '',
        $content,
    );

    # Finalize the form body
    push @body, ("--$BOUNDARY--", '');

    return ("multipart/form-data; boundary=$BOUNDARY", join( $CRLF, @body ) );
}

1;
__END__