| 1 | our $VERSION = '0.02'; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | HTTP::Upload::FlowJs::Examples - examples for HTTP::Upload::FlowJs | ||
| 6 | |||
| 7 | =head1 Pure PSGI example | ||
| 8 | |||
| 9 | This is a verbose example that assumes a PSGI / Plack environment | ||
| 10 | and some helper routines to handle the payload of POST parameters. | ||
| 11 | |||
| 12 | =head2 ASSUMPTIONS | ||
| 13 | |||
| 14 | It is assumed in the below code that your HTTP framework implements some | ||
| 15 | routines to retrieve information about the current request: | ||
| 16 | |||
| 17 | sub params() { | ||
| 18 | # return request parameters, both GET and POST parameters | ||
| 19 | # as a hash reference | ||
| 20 | } | ||
| 21 | |||
| 22 | sub upload { | ||
| 23 | # return something like a file system object to retrieve the content | ||
| 24 | # of a file upload parameter | ||
| 25 | } | ||
| 26 | |||
| 27 | =head2 SETUP | ||
| 28 | |||
| 29 | use HTTP::Upload::FlowJs; | ||
| 30 | |||
| 31 | my @parameter_names = ( | ||
| 32 | 'file', # The name of the file | ||
| 33 | 'flowChunkNumber', # The index of the chunk in the current upload. | ||
| 34 | # First chunk is 1 (no base-0 counting here). | ||
| 35 | 'flowTotalChunks', # The total number of chunks. | ||
| 36 | 'flowCurrentChunkSize', # Current chunk size | ||
| 37 | 'flowChunkSize', # The general chunk size. Using this value and | ||
| 38 | # flowTotalSize you can calculate the total number of | ||
| 39 | # chunks. Please note that the size of the data received in | ||
| 40 | # the HTTP might be lower than flowChunkSize of this for | ||
| 41 | # the last chunk for a file. | ||
| 42 | 'flowTotalSize', # The total file size. | ||
| 43 | 'flowIdentifier', # A unique identifier for the file contained in the request. | ||
| 44 | 'flowFilename', # The original file name (since a bug in Firefox results in | ||
| 45 | # the file name not being transmitted in chunk | ||
| 46 | # multipart posts). | ||
| 47 | 'flowRelativePath', # The file's relative path when selecting a directory | ||
| 48 | # (defaults to file name in all browsers except Chrome). | ||
| 49 | ); | ||
| 50 | |||
| 51 | my $uploads = '/tmp/flowjs_uploads/'; | ||
| 52 | my $flowjs = HTTP::Upload::FlowJs->new( | ||
| 53 | incomingDirectory => $uploads, | ||
| 54 | allowedContentType => sub { $_[0] =~ m!^image/! }, | ||
| 55 | parameter_names => \@parameter_names, # to override the default names | ||
| 56 | ); | ||
| 57 | |||
| 58 | =head2 POST handler | ||
| 59 | |||
| 60 | Your handler for POST requests is where you have to validate and store a | ||
| 61 | received chunk. This is also where you determine whether a file was received | ||
| 62 | completely and hand it off for further processing. | ||
| 63 | |||
| 64 | sub POST_upload { | ||
| 65 | my $params = params(); | ||
| 66 | |||
| 67 | my %info; | ||
| 68 | @info{ @parameter_names } = @{$params}{@parameter_names}; | ||
| 69 | $info{ localChunkSize } = -s $params{ file }; | ||
| 70 | # or however you get the size of the uploaded chunk | ||
| 71 | |||
| 72 | # you might want to set this so users don't clobber each others upload | ||
| 73 | my $session_id = ''; | ||
| 74 | my @invalid = $flowjs->validateRequest( 'POST', \%info, $session_id ); | ||
| 75 | if( @invalid ) { | ||
| 76 | warn 'Invalid flow.js upload request:'; | ||
| 77 | warn $_ for @invalid; | ||
| 78 | return [500,[],["Invalid request"]]; | ||
| 79 | return; | ||
| 80 | }; | ||
| 81 | |||
| 82 | if( $flowjs->disallowedContentType( \%info, $session_id )) { | ||
| 83 | # We can determine the content type, and it's not an image | ||
| 84 | return [415,[],["File type disallowed"]]; | ||
| 85 | }; | ||
| 86 | |||
| 87 | my $chunkname = $flowjs->chunkName( \%info, undef ); | ||
| 88 | |||
| 89 | # Save or copy the uploaded file | ||
| 90 | upload('file')->copy_to($chunkname); | ||
| 91 | |||
| 92 | # Now check if we have received all chunks of the file | ||
| 93 | if( $flowjs->uploadComplete( \%info, undef )) { | ||
| 94 | # Combine all chunks to final name | ||
| 95 | |||
| 96 | my $digest = Digest::SHA256->new(); | ||
| 97 | |||
| 98 | my( $content_type, $ext ) = $flowjs->sniffContentType(); | ||
| 99 | my $final_name = "file1.$ext"; | ||
| 100 | open( my $fh, '>', $final_name ) | ||
| 101 | or die $!; | ||
| 102 | binmode $fh; | ||
| 103 | |||
| 104 | my( $ok, @unlink_chunks ) | ||
| 105 | = $flowjs->combineChunks( \%info, undef, $fh, $digest ); | ||
| 106 | unlink @unlink_chunks; | ||
| 107 | |||
| 108 | # Notify backend that a file arrived | ||
| 109 | print sprintf "File '%s' upload complete\n", $final_name; | ||
| 110 | }; | ||
| 111 | |||
| 112 | # Signal OK | ||
| 113 | return [200,[],[]] | ||
| 114 | }; | ||
| 115 | |||
| 116 | =head2 GET handler | ||
| 117 | |||
| 118 | The GET handler is used by the Javascript code to retrieve information about | ||
| 119 | whether a chunk already has been uploaded or not. | ||
| 120 | |||
| 121 | sub GET_upload { | ||
| 122 | my $params = params(); | ||
| 123 | my %info; | ||
| 124 | @info{ @parameter_names} = @{$params}{@parameter_names}; | ||
| 125 | my $flowjs = HTTP::Upload::FlowJs->new( | ||
| 126 | incomingDirectory => $uploads, | ||
| 127 | allowedContentType => sub { $_[0] =~ m!^image/! }, | ||
| 128 | ); | ||
| 129 | |||
| 130 | my @invalid = $flowjs->validateRequest( 'GET', \%info, session->{connid} ); | ||
| 131 | if( @invalid ) { | ||
| 132 | warn 'Invalid flow.js upload request:'; | ||
| 133 | warn $_ for @invalid; | ||
| 134 | return [500, [], [] ]; | ||
| 135 | |||
| 136 | } elsif( $flowjs->disallowedContentType( \%info, $session_id)) { | ||
| 137 | # We can determine the content type, and it's not an image | ||
| 138 | return [415,[],["File type disallowed"]]; | ||
| 139 | |||
| 140 | } else { | ||
| 141 | my( $status, @messages ) | ||
| 142 | = $flowjs->chunkOK( $uploads, \%info, $session_id ); | ||
| 143 | if( $status != 500 ) { | ||
| 144 | # 200 or 416 | ||
| 145 | return [$status, [], [] ]; | ||
| 146 | } else { | ||
| 147 | warn $_ for @messages; | ||
| 148 | return [$status, [], [] ]; | ||
| 149 | }; | ||
| 150 | }; | ||
| 151 | }; | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | 1; |