| 1 | package Net::AS2::PSGI::FileHandler; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use autodie; | ||
| 6 | our $VERSION = '1.0'; # VERSION | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::AS2::PSGI::FileHandler - Provides methods to handle files being sent and received | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | use parent 'Net::AS2::PSGI::File'; | ||
| 15 | |||
| 16 | =head1 METHODS | ||
| 17 | |||
| 18 | =over 4 | ||
| 19 | |||
| 20 | =item = $self->receiving( $content, $receiving_dir ) | ||
| 21 | |||
| 22 | This method is called when C<$content> is being received. | ||
| 23 | |||
| 24 | By default, this method stores the given content into a filename in the | ||
| 25 | RECEIVING directory as given by C<< $self->file($receiving_dir) >>. | ||
| 26 | |||
| 27 | I<Note> This method must B<not> be used to send an MDN response. It is | ||
| 28 | called immediately after the request has been received and before | ||
| 29 | the response has been sent back to the partner. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub receiving { | ||
| 34 | my ($self, $content, $receiving_dir) = @_; | ||
| 35 | |||
| 36 | my $receiving_file = $self->file($receiving_dir); | ||
| 37 | |||
| 38 | $self->write($receiving_file, $content); | ||
| 39 | |||
| 40 | $self->logger(debug => "Receiving content saved in $receiving_file"); | ||
| 41 | |||
| 42 | return $receiving_file; | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | =item = $self->received( $receiving_file, $received_dir, $message ) | ||
| 47 | |||
| 48 | This method is called when the content of C<$receiving_file> has been | ||
| 49 | received. C<$message> is an object of class L<Net::AS2::Message>. | ||
| 50 | |||
| 51 | By default, this method renames the given C<$receiving_file> to a file | ||
| 52 | in the RECEIVED directory, calculated by C<< $self->file($received_dir, $ext) >>. | ||
| 53 | If the transfer was not successful, a file extension is calculated | ||
| 54 | from the state of the C<$message> object: | ||
| 55 | |||
| 56 | State Extension | ||
| 57 | $message->is_success() None | ||
| 58 | $message->is_error() .error | ||
| 59 | $message->is_failure() .failed | ||
| 60 | |||
| 61 | The received Content-Disposition header filename attribute is | ||
| 62 | available for use as C<< $message->filename() >>. | ||
| 63 | |||
| 64 | I<Note> This method must B<not> be used to send an MDN response. It is | ||
| 65 | called immediately after the request has been received and before | ||
| 66 | the response has been sent back to the partner. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub received { | ||
| 71 | my ($self, $receiving_file, $received_dir, $message) = @_; | ||
| 72 | |||
| 73 | my $ext = $message->is_success ? '' : $message->is_error ? '.error' : '.failed'; | ||
| 74 | |||
| 75 | my $received_file = $self->file($received_dir, $ext); | ||
| 76 | |||
| 77 | rename $receiving_file, $received_file; | ||
| 78 | |||
| 79 | my $content_filename = $message->filename // ''; | ||
| 80 | |||
| 81 | $self->logger(debug => "Received '$content_filename' saved in file $received_file"); | ||
| 82 | |||
| 83 | return $received_file; | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | =item = $self->sending( $content, $sending_file ) | ||
| 88 | |||
| 89 | This method is called when C<$content> is being sent. | ||
| 90 | |||
| 91 | By default, this method stores the given content into the | ||
| 92 | C<$sending_file>, which is calculated by C<< $self->file($sending_dir) >>. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub sending { | ||
| 97 | my ($self, $content, $sending_file) = @_; | ||
| 98 | |||
| 99 | $self->write($sending_file, $content); | ||
| 100 | |||
| 101 | $self->logger(debug => "Sending file $sending_file"); | ||
| 102 | |||
| 103 | return; | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | =item = $self->sent( $sending_file, $sent_dir, $successful ) | ||
| 108 | |||
| 109 | This method is called when the content of C<$sending_file> has been sent. | ||
| 110 | C<$successful> is either a true or false value depending upon whether | ||
| 111 | the content was received by the partner successfully or not. | ||
| 112 | |||
| 113 | By default, this method renames the given C<$sending_file> to a file | ||
| 114 | in the SENT directory, calculated by C<< $self->file($sent_dir, $ext) >>. | ||
| 115 | If the transfer was not successful, a file extension, ".failed" is | ||
| 116 | used for C<$ext>. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub sent { | ||
| 121 | my ($self, $sending_file, $sent_dir, $successful) = @_; | ||
| 122 | |||
| 123 | my $ext = $successful ? '' : '.failed'; | ||
| 124 | |||
| 125 | my $sent_file = $self->file($sent_dir, $ext); | ||
| 126 | |||
| 127 | rename $sending_file, $sent_file; | ||
| 128 | |||
| 129 | $self->logger(debug => "Sent file $sent_file"); | ||
| 130 | |||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | |||
| 139 | 1; |