| 1 | package Amazon::S3::FastUploader::File; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use base qw( Class::Accessor ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | __PACKAGE__->mk_accessors( qw(local_path target_dir bucket config s3) ); | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # stolen from Plack::MIME which is stolen from rack.mime.rb | ||||||||||||||||||||||||||||
| 11 | 3 | our $MIME_TYPES = { | |||||||||||||||||||||||||||
| 12 | ".3gp" => "video/3gpp", | ||||||||||||||||||||||||||||
| 13 | ".a" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 14 | ".ai" => "application/postscript", | ||||||||||||||||||||||||||||
| 15 | ".aif" => "audio/x-aiff", | ||||||||||||||||||||||||||||
| 16 | ".aiff" => "audio/x-aiff", | ||||||||||||||||||||||||||||
| 17 | ".asc" => "application/pgp-signature", | ||||||||||||||||||||||||||||
| 18 | ".asf" => "video/x-ms-asf", | ||||||||||||||||||||||||||||
| 19 | ".asm" => "text/x-asm", | ||||||||||||||||||||||||||||
| 20 | ".asx" => "video/x-ms-asf", | ||||||||||||||||||||||||||||
| 21 | ".atom" => "application/atom+xml", | ||||||||||||||||||||||||||||
| 22 | ".au" => "audio/basic", | ||||||||||||||||||||||||||||
| 23 | ".avi" => "video/x-msvideo", | ||||||||||||||||||||||||||||
| 24 | ".bat" => "application/x-msdownload", | ||||||||||||||||||||||||||||
| 25 | ".bin" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 26 | ".bmp" => "image/bmp", | ||||||||||||||||||||||||||||
| 27 | ".bz2" => "application/x-bzip2", | ||||||||||||||||||||||||||||
| 28 | ".c" => "text/x-c", | ||||||||||||||||||||||||||||
| 29 | ".cab" => "application/vnd.ms-cab-compressed", | ||||||||||||||||||||||||||||
| 30 | ".cc" => "text/x-c", | ||||||||||||||||||||||||||||
| 31 | ".chm" => "application/vnd.ms-htmlhelp", | ||||||||||||||||||||||||||||
| 32 | ".class" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 33 | ".com" => "application/x-msdownload", | ||||||||||||||||||||||||||||
| 34 | ".conf" => "text/plain", | ||||||||||||||||||||||||||||
| 35 | ".cpp" => "text/x-c", | ||||||||||||||||||||||||||||
| 36 | ".crt" => "application/x-x509-ca-cert", | ||||||||||||||||||||||||||||
| 37 | ".css" => "text/css", | ||||||||||||||||||||||||||||
| 38 | ".csv" => "text/csv", | ||||||||||||||||||||||||||||
| 39 | ".cxx" => "text/x-c", | ||||||||||||||||||||||||||||
| 40 | ".deb" => "application/x-debian-package", | ||||||||||||||||||||||||||||
| 41 | ".der" => "application/x-x509-ca-cert", | ||||||||||||||||||||||||||||
| 42 | ".diff" => "text/x-diff", | ||||||||||||||||||||||||||||
| 43 | ".djv" => "image/vnd.djvu", | ||||||||||||||||||||||||||||
| 44 | ".djvu" => "image/vnd.djvu", | ||||||||||||||||||||||||||||
| 45 | ".dll" => "application/x-msdownload", | ||||||||||||||||||||||||||||
| 46 | ".dmg" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 47 | ".doc" => "application/msword", | ||||||||||||||||||||||||||||
| 48 | ".dot" => "application/msword", | ||||||||||||||||||||||||||||
| 49 | ".dtd" => "application/xml-dtd", | ||||||||||||||||||||||||||||
| 50 | ".dvi" => "application/x-dvi", | ||||||||||||||||||||||||||||
| 51 | ".ear" => "application/java-archive", | ||||||||||||||||||||||||||||
| 52 | ".eml" => "message/rfc822", | ||||||||||||||||||||||||||||
| 53 | ".eps" => "application/postscript", | ||||||||||||||||||||||||||||
| 54 | ".exe" => "application/x-msdownload", | ||||||||||||||||||||||||||||
| 55 | ".f" => "text/x-fortran", | ||||||||||||||||||||||||||||
| 56 | ".f77" => "text/x-fortran", | ||||||||||||||||||||||||||||
| 57 | ".f90" => "text/x-fortran", | ||||||||||||||||||||||||||||
| 58 | ".flv" => "video/x-flv", | ||||||||||||||||||||||||||||
| 59 | ".for" => "text/x-fortran", | ||||||||||||||||||||||||||||
| 60 | ".gem" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 61 | ".gemspec" => "text/x-script.ruby", | ||||||||||||||||||||||||||||
| 62 | ".gif" => "image/gif", | ||||||||||||||||||||||||||||
| 63 | ".gz" => "application/x-gzip", | ||||||||||||||||||||||||||||
| 64 | ".h" => "text/x-c", | ||||||||||||||||||||||||||||
| 65 | ".hh" => "text/x-c", | ||||||||||||||||||||||||||||
| 66 | ".htm" => "text/html", | ||||||||||||||||||||||||||||
| 67 | ".html" => "text/html", | ||||||||||||||||||||||||||||
| 68 | ".ico" => "image/vnd.microsoft.icon", | ||||||||||||||||||||||||||||
| 69 | ".ics" => "text/calendar", | ||||||||||||||||||||||||||||
| 70 | ".ifb" => "text/calendar", | ||||||||||||||||||||||||||||
| 71 | ".iso" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 72 | ".jar" => "application/java-archive", | ||||||||||||||||||||||||||||
| 73 | ".java" => "text/x-java-source", | ||||||||||||||||||||||||||||
| 74 | ".jnlp" => "application/x-java-jnlp-file", | ||||||||||||||||||||||||||||
| 75 | ".jpeg" => "image/jpeg", | ||||||||||||||||||||||||||||
| 76 | ".jpg" => "image/jpeg", | ||||||||||||||||||||||||||||
| 77 | ".js" => "application/javascript", | ||||||||||||||||||||||||||||
| 78 | ".json" => "application/json", | ||||||||||||||||||||||||||||
| 79 | ".log" => "text/plain", | ||||||||||||||||||||||||||||
| 80 | ".m3u" => "audio/x-mpegurl", | ||||||||||||||||||||||||||||
| 81 | ".m4v" => "video/mp4", | ||||||||||||||||||||||||||||
| 82 | ".man" => "text/troff", | ||||||||||||||||||||||||||||
| 83 | ".manifest"=> "text/cache-manifest", | ||||||||||||||||||||||||||||
| 84 | ".mathml" => "application/mathml+xml", | ||||||||||||||||||||||||||||
| 85 | ".mbox" => "application/mbox", | ||||||||||||||||||||||||||||
| 86 | ".mdoc" => "text/troff", | ||||||||||||||||||||||||||||
| 87 | ".me" => "text/troff", | ||||||||||||||||||||||||||||
| 88 | ".mid" => "audio/midi", | ||||||||||||||||||||||||||||
| 89 | ".midi" => "audio/midi", | ||||||||||||||||||||||||||||
| 90 | ".mime" => "message/rfc822", | ||||||||||||||||||||||||||||
| 91 | ".mml" => "application/mathml+xml", | ||||||||||||||||||||||||||||
| 92 | ".mng" => "video/x-mng", | ||||||||||||||||||||||||||||
| 93 | ".mov" => "video/quicktime", | ||||||||||||||||||||||||||||
| 94 | ".mp3" => "audio/mpeg", | ||||||||||||||||||||||||||||
| 95 | ".mp4" => "video/mp4", | ||||||||||||||||||||||||||||
| 96 | ".mp4v" => "video/mp4", | ||||||||||||||||||||||||||||
| 97 | ".mpeg" => "video/mpeg", | ||||||||||||||||||||||||||||
| 98 | ".mpg" => "video/mpeg", | ||||||||||||||||||||||||||||
| 99 | ".ms" => "text/troff", | ||||||||||||||||||||||||||||
| 100 | ".msi" => "application/x-msdownload", | ||||||||||||||||||||||||||||
| 101 | ".odp" => "application/vnd.oasis.opendocument.presentation", | ||||||||||||||||||||||||||||
| 102 | ".ods" => "application/vnd.oasis.opendocument.spreadsheet", | ||||||||||||||||||||||||||||
| 103 | ".odt" => "application/vnd.oasis.opendocument.text", | ||||||||||||||||||||||||||||
| 104 | ".ogg" => "application/ogg", | ||||||||||||||||||||||||||||
| 105 | ".ogv" => "video/ogg", | ||||||||||||||||||||||||||||
| 106 | ".p" => "text/x-pascal", | ||||||||||||||||||||||||||||
| 107 | ".pas" => "text/x-pascal", | ||||||||||||||||||||||||||||
| 108 | ".pbm" => "image/x-portable-bitmap", | ||||||||||||||||||||||||||||
| 109 | ".pdf" => "application/pdf", | ||||||||||||||||||||||||||||
| 110 | ".pem" => "application/x-x509-ca-cert", | ||||||||||||||||||||||||||||
| 111 | ".pgm" => "image/x-portable-graymap", | ||||||||||||||||||||||||||||
| 112 | ".pgp" => "application/pgp-encrypted", | ||||||||||||||||||||||||||||
| 113 | ".pkg" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 114 | ".pl" => "text/x-script.perl", | ||||||||||||||||||||||||||||
| 115 | ".pm" => "text/x-script.perl-module", | ||||||||||||||||||||||||||||
| 116 | ".png" => "image/png", | ||||||||||||||||||||||||||||
| 117 | ".pnm" => "image/x-portable-anymap", | ||||||||||||||||||||||||||||
| 118 | ".ppm" => "image/x-portable-pixmap", | ||||||||||||||||||||||||||||
| 119 | ".pps" => "application/vnd.ms-powerpoint", | ||||||||||||||||||||||||||||
| 120 | ".ppt" => "application/vnd.ms-powerpoint", | ||||||||||||||||||||||||||||
| 121 | ".ps" => "application/postscript", | ||||||||||||||||||||||||||||
| 122 | ".psd" => "image/vnd.adobe.photoshop", | ||||||||||||||||||||||||||||
| 123 | ".py" => "text/x-script.python", | ||||||||||||||||||||||||||||
| 124 | ".qt" => "video/quicktime", | ||||||||||||||||||||||||||||
| 125 | ".ra" => "audio/x-pn-realaudio", | ||||||||||||||||||||||||||||
| 126 | ".rake" => "text/x-script.ruby", | ||||||||||||||||||||||||||||
| 127 | ".ram" => "audio/x-pn-realaudio", | ||||||||||||||||||||||||||||
| 128 | ".rar" => "application/x-rar-compressed", | ||||||||||||||||||||||||||||
| 129 | ".rb" => "text/x-script.ruby", | ||||||||||||||||||||||||||||
| 130 | ".rdf" => "application/rdf+xml", | ||||||||||||||||||||||||||||
| 131 | ".roff" => "text/troff", | ||||||||||||||||||||||||||||
| 132 | ".rpm" => "application/x-redhat-package-manager", | ||||||||||||||||||||||||||||
| 133 | ".rss" => "application/rss+xml", | ||||||||||||||||||||||||||||
| 134 | ".rtf" => "application/rtf", | ||||||||||||||||||||||||||||
| 135 | ".ru" => "text/x-script.ruby", | ||||||||||||||||||||||||||||
| 136 | ".s" => "text/x-asm", | ||||||||||||||||||||||||||||
| 137 | ".sgm" => "text/sgml", | ||||||||||||||||||||||||||||
| 138 | ".sgml" => "text/sgml", | ||||||||||||||||||||||||||||
| 139 | ".sh" => "application/x-sh", | ||||||||||||||||||||||||||||
| 140 | ".sig" => "application/pgp-signature", | ||||||||||||||||||||||||||||
| 141 | ".snd" => "audio/basic", | ||||||||||||||||||||||||||||
| 142 | ".so" => "application/octet-stream", | ||||||||||||||||||||||||||||
| 143 | ".svg" => "image/svg+xml", | ||||||||||||||||||||||||||||
| 144 | ".svgz" => "image/svg+xml", | ||||||||||||||||||||||||||||
| 145 | ".swf" => "application/x-shockwave-flash", | ||||||||||||||||||||||||||||
| 146 | ".t" => "text/troff", | ||||||||||||||||||||||||||||
| 147 | ".tar" => "application/x-tar", | ||||||||||||||||||||||||||||
| 148 | ".tbz" => "application/x-bzip-compressed-tar", | ||||||||||||||||||||||||||||
| 149 | ".tcl" => "application/x-tcl", | ||||||||||||||||||||||||||||
| 150 | ".tex" => "application/x-tex", | ||||||||||||||||||||||||||||
| 151 | ".texi" => "application/x-texinfo", | ||||||||||||||||||||||||||||
| 152 | ".texinfo" => "application/x-texinfo", | ||||||||||||||||||||||||||||
| 153 | ".text" => "text/plain", | ||||||||||||||||||||||||||||
| 154 | ".tif" => "image/tiff", | ||||||||||||||||||||||||||||
| 155 | ".tiff" => "image/tiff", | ||||||||||||||||||||||||||||
| 156 | ".torrent" => "application/x-bittorrent", | ||||||||||||||||||||||||||||
| 157 | ".tr" => "text/troff", | ||||||||||||||||||||||||||||
| 158 | ".txt" => "text/plain", | ||||||||||||||||||||||||||||
| 159 | ".vcf" => "text/x-vcard", | ||||||||||||||||||||||||||||
| 160 | ".vcs" => "text/x-vcalendar", | ||||||||||||||||||||||||||||
| 161 | ".vrml" => "model/vrml", | ||||||||||||||||||||||||||||
| 162 | ".war" => "application/java-archive", | ||||||||||||||||||||||||||||
| 163 | ".wav" => "audio/x-wav", | ||||||||||||||||||||||||||||
| 164 | ".wma" => "audio/x-ms-wma", | ||||||||||||||||||||||||||||
| 165 | ".wmv" => "video/x-ms-wmv", | ||||||||||||||||||||||||||||
| 166 | ".wmx" => "video/x-ms-wmx", | ||||||||||||||||||||||||||||
| 167 | ".wrl" => "model/vrml", | ||||||||||||||||||||||||||||
| 168 | ".wsdl" => "application/wsdl+xml", | ||||||||||||||||||||||||||||
| 169 | ".xbm" => "image/x-xbitmap", | ||||||||||||||||||||||||||||
| 170 | ".xhtml" => "application/xhtml+xml", | ||||||||||||||||||||||||||||
| 171 | ".xls" => "application/vnd.ms-excel", | ||||||||||||||||||||||||||||
| 172 | ".xml" => "application/xml", | ||||||||||||||||||||||||||||
| 173 | ".xpm" => "image/x-xpixmap", | ||||||||||||||||||||||||||||
| 174 | ".xsl" => "application/xml", | ||||||||||||||||||||||||||||
| 175 | ".xslt" => "application/xslt+xml", | ||||||||||||||||||||||||||||
| 176 | ".yaml" => "text/yaml", | ||||||||||||||||||||||||||||
| 177 | ".yml" => "text/yaml", | ||||||||||||||||||||||||||||
| 178 | ".zip" => "application/zip", | ||||||||||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub upload { | ||||||||||||||||||||||||||
|
Subroutine
upload: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | my $bucket = $self->bucket; | |||||||||||||||||||||||||||
| 185 | 0 | my $s3 = $self->s3; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | my $opt = {}; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ($self->config->{encrypt}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | $opt->{"x-amz-server-side-encryption"} = 'AES256'; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if ($self->config->{acl_short}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | $opt->{acl_short} = $self->config->{acl_short}; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # http://d.hatena.ne.jp/perlcodesample/20080518/1211121650 | ||||||||||||||||||||||||||||
| 197 | 0 | my (undef, undef, $ext) = fileparse($self->local_path, qw/\.[^\.]+$/); | |||||||||||||||||||||||||||
| 198 | #print $self->local_path, " ext:$ext \n"; | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($MIME_TYPES->{$ext}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | $opt->{content_type} = $MIME_TYPES->{$ext}; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | my $count_failed = 0; | |||||||||||||||||||||||||||
| 204 | 0 | my $max_retry = 5; | |||||||||||||||||||||||||||
| 205 | 0 | my $is_success = 0; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | while (! $is_success && $count_failed < $max_retry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $is_success and $count_failed < $max_retry
MC/DC0%0/2
not $is_success and $count_failed < $max_retry
not $is_success
$count_failed < $max_retry
Truth table0%0/3
not(A) and B
A= not $is_successB= $count_failed < $max_retry
| |||||||||||||||||||||||||||||
| 208 | $is_success = $bucket->add_key_filename($self->_remote_key, $self->local_path, $opt) | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | or do { warn $s3->err . ": " . $s3->errstr . "(" . $self->from_to .")" ; $count_failed++; }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | if ($is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | return 1; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | die "upload failed " . $self->from_to; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | ▶ | sub from_to { | ||||||||||||||||||||||||||
|
Subroutine
from_to: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 220 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | return $self->local_path . " -> " . $self->_remote_key; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub _remote_key { | ||||||||||||||||||||||||||
|
Subroutine
_remote_key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 226 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 227 | 0 | my $local_path = $self->{local_path}; | |||||||||||||||||||||||||||
| 228 | 0 | $local_path =~ s|^\./||; | |||||||||||||||||||||||||||
| 229 | 0 | return $self->target_dir . $local_path; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 NAME | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Amazon::S3::FastUploader - fast uploader to Amazon S3 | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | This is an internal module of Amazon::S3::FastUploader. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 METHODS | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head2 new | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | takes a hashref: | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | local_path: full path to a local file | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | target_dir: dirname on S3 | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | bucket: a bucket object | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | config: config option (hashref) | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =head2 upload | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | do uploading | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | content_type (mime type) is automatically determined by its extension name. | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | =head2 from_to | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | return a string wich shows source filename and target filename | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =cut | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 3 | 1; | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | __END__ | ||||||||||||||||||||||||||||