<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Catalyst::Request::Upload;

use Moose;
with 'MooseX::Emulate::Class::Accessor::Fast';

use Catalyst::Exception;
use File::Copy ();
use IO::File ();
use File::Spec::Unix;
use namespace::clean -except =&gt; 'meta';

has filename =&gt; (is =&gt; 'rw');
has headers =&gt; (is =&gt; 'rw');
has size =&gt; (is =&gt; 'rw');
has tempname =&gt; (is =&gt; 'rw');
has type =&gt; (is =&gt; 'rw');
has basename =&gt; (is =&gt; 'ro', lazy_build =&gt; 1);

has fh =&gt; (
  is =&gt; 'rw',
  required =&gt; 1,
  lazy =&gt; 1,
  default =&gt; sub {
    my $self = shift;

    my $fh = IO::File-&gt;new($self-&gt;tempname, IO::File::O_RDONLY);
    unless ( defined $fh ) {
      my $filename = $self-&gt;tempname;
      Catalyst::Exception-&gt;throw(
          message =&gt; qq/Can't open '$filename': '$!'/ );
    }

    return $fh;
  },
);

sub _build_basename {
    my $self = shift;
    my $basename = $self-&gt;filename;
    $basename =~ s|\\|/|g;
    $basename = ( File::Spec::Unix-&gt;splitpath($basename) )[2];
    $basename =~ s|[^\w\.-]+|_|g;
    return $basename;
}

no Moose;

=for stopwords uploadtmp

=head1 NAME

Catalyst::Request::Upload - handles file upload requests

=head1 SYNOPSIS

    my $upload = $c-&gt;req-&gt;upload('field');

    $upload-&gt;basename;
    $upload-&gt;copy_to;
    $upload-&gt;fh;
    $upload-&gt;filename;
    $upload-&gt;headers;
    $upload-&gt;link_to;
    $upload-&gt;size;
    $upload-&gt;slurp;
    $upload-&gt;tempname;
    $upload-&gt;type;

To specify where Catalyst should put the temporary files, set the 'uploadtmp'
option in the Catalyst config. If unset, Catalyst will use the system temp dir.

    __PACKAGE__-&gt;config( uploadtmp =&gt; '/path/to/tmpdir' );

See also L&lt;Catalyst&gt;.

=head1 DESCRIPTION

This class provides accessors and methods to handle client upload requests.

=head1 METHODS

=head2 $upload-&gt;new

Simple constructor.

=head2 $upload-&gt;copy_to

Copies the temporary file using L&lt;File::Copy&gt;. Returns true for success,
false for failure.

     $upload-&gt;copy_to('/path/to/target');

=cut

sub copy_to {
    my $self = shift;
    return File::Copy::copy( $self-&gt;tempname, @_ );
}

=head2 $upload-&gt;fh

Opens a temporary file (see tempname below) and returns an L&lt;IO::File&gt; handle.

=head2 $upload-&gt;filename

Returns the client-supplied filename.

=head2 $upload-&gt;headers

Returns an L&lt;HTTP::Headers&gt; object for the request.

=head2 $upload-&gt;link_to

Creates a hard link to the temporary file. Returns true for success,
false for failure.

    $upload-&gt;link_to('/path/to/target');

=cut

sub link_to {
    my ( $self, $target ) = @_;
    return CORE::link( $self-&gt;tempname, $target );
}

=head2 $upload-&gt;size

Returns the size of the uploaded file in bytes.

=head2 $upload-&gt;slurp

Returns a scalar containing the contents of the temporary file.

Note that this will cause the filehandle pointed to by C&lt;&lt; $upload-&gt;fh &gt;&gt; to
be reset to the start of the file using seek and the file handle to be put
into binary mode.

=cut

sub slurp {
    my ( $self, $layer ) = @_;

    unless ($layer) {
        $layer = ':raw';
    }

    my $content = undef;
    my $handle  = $self-&gt;fh;

    binmode( $handle, $layer );

    $handle-&gt;seek(0, IO::File::SEEK_SET);
    while ( $handle-&gt;sysread( my $buffer, 8192 ) ) {
        $content .= $buffer;
    }

    $handle-&gt;seek(0, IO::File::SEEK_SET);
    return $content;
}

=head2 $upload-&gt;basename

Returns basename for C&lt;filename&gt;.

=head2 $upload-&gt;tempname

Returns the path to the temporary file.

=head2 $upload-&gt;type

Returns the client-supplied Content-Type.

=head2 meta

Provided by Moose

=head1 AUTHORS

Catalyst Contributors, see Catalyst.pm

=head1 COPYRIGHT

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__-&gt;meta-&gt;make_immutable;

1;
</pre></body></html>