| 1 | package Crop::File::LOCAL; | ||
| 2 | use base qw/ Crop::File /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::File::LOCAL | ||
| 6 | File local stored. | ||
| 7 | =cut | ||
| 8 | |||
| 9 | use v5.14; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use Fcntl qw/ SEEK_SET O_WRONLY O_CREAT /; | ||
| 13 | use File::Basename; | ||
| 14 | use File::Path qw/ make_path /; | ||
| 15 | |||
| 16 | use Crop::Error; | ||
| 17 | use Crop::Util; | ||
| 18 | |||
| 19 | use Crop::Debug; | ||
| 20 | |||
| 21 | =begin nd | ||
| 22 | Constant: BUF_SIZE | ||
| 23 | Read|Write buffer; reading by small chunks to save memory usage. | ||
| 24 | =cut | ||
| 25 | use constant { | ||
| 26 | BUF_SIZE => 1024 * 1024, # 1Mb | ||
| 27 | }; | ||
| 28 | |||
| 29 | =begin nd | ||
| 30 | Constructor: new (@attr) | ||
| 31 | Set file type. | ||
| 32 | |||
| 33 | Specifying explicit type is not allowed. | ||
| 34 | |||
| 35 | Parameters: | ||
| 36 | %attr - class attributes | ||
| 37 | |||
| 38 | Returns: | ||
| 39 | $self - ok | ||
| 40 | undef - error | ||
| 41 | =cut | ||
| 42 | sub new { | ||
| 43 | my ($class, @attr) = @_; | ||
| 44 | my $attr = expose_hashes \@attr; | ||
| 45 | |||
| 46 | $class->SUPER::new( | ||
| 47 | %$attr, | ||
| 48 | warehouse => 'LOCAL', # parent Constructor replace this attribute to integer value | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | |||
| 52 | =begin nd | ||
| 53 | Method: upload ($src, $dst) | ||
| 54 | Upload field from CGI multipart form field. | ||
| 55 | |||
| 56 | Read/Write uses small blocks instead of all-in-one operation. | ||
| 57 | |||
| 58 | Path to store disallowed to use '../' (dir up) element for security reason. | ||
| 59 | |||
| 60 | Make destination directory unless exists. Set file size. | ||
| 61 | |||
| 62 | Only FastCGI is supported now. Refactoring will be required when will new Server interface arrived. | ||
| 63 | |||
| 64 | Parameters: | ||
| 65 | $src - field name in the CGI form | ||
| 66 | $dst - path the created file will be stored; see section 'upload' in the global.xml | ||
| 67 | |||
| 68 | Returns: | ||
| 69 | true - ok | ||
| 70 | undef - error | ||
| 71 | =cut | ||
| 72 | sub upload { | ||
| 73 | my ($self, $src, $dst) = @_; | ||
| 74 | $self->{path} = $dst; | ||
| 75 | my $S = $self->S; | ||
| 76 | |||
| 77 | return warn 'FILE|CRIT: Upload has incomplete params' unless defined $src and defined $dst; | ||
| 78 | |||
| 79 | # change directory to level up is not allowed | ||
| 80 | my $hack = qr{\.\./}; | ||
| 81 | return warn 'FILE|ALERT: Upload try HACK!' if $dst =~ $hack; | ||
| 82 | |||
| 83 | exists $S->I->{$src} and $S->cgi->upload($src) or return warn "FILE: upload not exists"; # FastCGI specific | ||
| 84 | |||
| 85 | my $src_fh = $S->cgi->upload($src) or return warn "FILE|NOTICE: upload can not get cgi-filehandle for $src field"; | ||
| 86 | sysseek $src_fh, 0, SEEK_SET or return warn "FILE: Can't seek file from multipart field $src"; | ||
| 87 | |||
| 88 | my $dir = dirname $dst; | ||
| 89 | unless (-d $dir) { | ||
| 90 | unless (make_path $dir, {error => \my $err}) { | ||
| 91 | return warn "FILE: Can't upload LOCAL: $err"; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | sysopen my $dst_fh, $dst, O_WRONLY|O_CREAT or return warn "FILE: Can't upload file dest '$dst': $!"; | ||
| 95 | |||
| 96 | my ($buf, $nread, $total); | ||
| 97 | while ($nread = sysread $src_fh, $buf, BUF_SIZE) { | ||
| 98 | my $left = $nread; | ||
| 99 | $total += $nread; | ||
| 100 | |||
| 101 | my $rc; | ||
| 102 | while ($left) { | ||
| 103 | unless ($rc = syswrite $dst_fh, $buf, $left, $nread - $left) { | ||
| 104 | close $dst_fh or return warn "FILE|CRIT: Can't close dst file during uploading: $!"; | ||
| 105 | return warn "FILE: Can't write upload: $!; rc=$rc"; | ||
| 106 | } | ||
| 107 | $left -= $rc; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | $self->{size} //= -s $src_fh; | ||
| 111 | return warn 'FILE|CRIT: Upload size mismatch to source size' unless $total == $self->{size}; | ||
| 112 | |||
| 113 | close $dst_fh or return warn "FILE|CRIT: Can't close dst file during uploading: $!"; | ||
| 114 | return warn "FILE|CRIT: Can't read multipart upload: $!" unless defined $nread; | ||
| 115 | |||
| 116 | 1; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; |