[Rt-commit] rt branch, 5.0/rest2-support-attachments-when-creating-ticket, repushed
? sunnavy
sunnavy at bestpractical.com
Tue Oct 27 17:06:03 EDT 2020
The branch 5.0/rest2-support-attachments-when-creating-ticket was deleted and repushed:
was 52a2fc0bb0e9062ca66362096b8c7ec7657ac3d3
now ba3b448a581ac3a18edb74b95611eb43e989f6e6
-: ------- > 1: 7b375e0b77 Abstract code to process file uploads to avoid duplication for REST2
-: ------- > 2: 94a1f2cd4c Fix typo
1: 52a2fc0bb0 ! 3: ba3b448a58 Allow attachments to be added when a ticket is created
@@ -1,14 +1,6 @@
Author: Dianne Skoll <dianne at bestpractical.com>
- Allow attachments to be added when a ticket is created.
-
- The attachments are supplied in an Attachments array, each element
- of which is a three-element hash containing FileName, FileType and
- FileContent members. FileContent is the base64-encoded content.
-
- The ticket can also be created with a request content type of
- multipart/form-data. In this case, attachments can be transmitted
- as raw data rather than requiring base64-encoding.
+ Allow attachments to be added when a ticket is created
diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
--- a/lib/RT/REST2.pm
@@ -24,20 +16,34 @@
=head3 Updating Tickets
For updating tickets we use the C<PUT> verb, but otherwise it looks much
-@@
- =head3 Add Attachments
-
- You can attach any binary or text file to your response or comment by
--specifying C<Attachements> property in the JSON object, which should be a
-+specifying C<Attachments> property in the JSON object, which should be a
- JSON array where each item represents a file you want to attach. Each item
- is a JSON object with the following properties:
+
+diff --git a/lib/RT/REST2/Resource/Record/Writable.pm b/lib/RT/REST2/Resource/Record/Writable.pm
+--- a/lib/RT/REST2/Resource/Record/Writable.pm
++++ b/lib/RT/REST2/Resource/Record/Writable.pm
+@@
+ $json->{CustomFields} = $cfs;
+ }
+
++ if ( my @attachments = $self->request->upload('Attachments') ) {
++ $json->{Attachments} = [ process_uploads(@attachments) ];
++ }
++
+ return $self->from_json($json);
+ }
diff --git a/lib/RT/REST2/Resource/Ticket.pm b/lib/RT/REST2/Resource/Ticket.pm
--- a/lib/RT/REST2/Resource/Ticket.pm
+++ b/lib/RT/REST2/Resource/Ticket.pm
@@
+ Object => $queue,
+ ) and $queue->Disabled != 1;
+
+- if ( defined $data->{Content} ) {
++ if ( defined $data->{Content} || defined $data->{Attachments} ) {
+ $data->{MIMEObj} = HTML::Mason::Commands::MakeMIMEEntity(
+ Interface => 'REST',
+ Subject => $data->{Subject},
Body => delete $data->{Content},
Type => delete $data->{ContentType} || 'text/plain',
);
@@ -53,55 +59,11 @@
+ Filename => $attachment->{FileName},
+ Data => MIME::Base64::decode_base64($attachment->{FileContent}));
+ }
++ delete $data->{Attachments};
+ }
}
-+ delete $data->{Attachments};
-+
my ($ok, $txn, $msg) = $self->_create_record($data);
- return ($ok, $msg);
- }
-@@
- return $links;
- }
-
-+# Allow attachments to be added as parts to the multipart/form_data
-+# upload
-+sub from_multipart {
-+ my $self = shift;
-+ my $json_str = $self->request->parameters->{JSON};
-+ return error_as_json(
-+ $self->response,
-+ \400, "JSON is a required field for multipart/form-data")
-+ unless $json_str;
-+
-+ my $data = JSON::decode_json($json_str);
-+
-+ my @attachments = $self->request->upload('Attachments');
-+
-+ foreach my $attachment (@attachments) {
-+ open my $filehandle, '<', $attachment->tempname;
-+ if (defined $filehandle && length $filehandle) {
-+ my ( @content, $buffer );
-+ while ( read( $filehandle, $buffer, 72*57 ) ) {
-+ push @content, MIME::Base64::encode_base64($buffer);
-+ }
-+ close $filehandle;
-+
-+ push @{$data->{Attachments}},
-+ {
-+ FileName => $attachment->filename,
-+ FileType => $attachment->headers->{'content-type'},
-+ FileContent => join("\n", @content),
-+ };
-+ }
-+ }
-+ return $self->from_json($data);
-+};
-+
- __PACKAGE__->meta->make_immutable;
-
- 1;
diff --git a/lib/RT/REST2/Util.pm b/lib/RT/REST2/Util.pm
--- a/lib/RT/REST2/Util.pm
@@ -272,6 +234,4 @@
+ like( $attachments->[4]->Content, qr/IHDR/, "Looks like a PNG image" );
+}
+
-+
-+
done_testing;
More information about the rt-commit
mailing list