Mailing List Archive

rt branch, 5.0/rest2-support-attachments-when-creating-ticket, created. rt-5.0.0-56-g52a2fc0bb0
The branch, 5.0/rest2-support-attachments-when-creating-ticket has been created
at 52a2fc0bb0e9062ca66362096b8c7ec7657ac3d3 (commit)

- Log -----------------------------------------------------------------
commit 52a2fc0bb0e9062ca66362096b8c7ec7657ac3d3
Author: Dianne Skoll <dianne@bestpractical.com>
Date: Wed Oct 21 11:14:44 2020 -0400

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.

diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index e7a4f6d27e..505ef2d408 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -204,6 +204,10 @@ used for conflict avoidance
(L<https://en.wikipedia.org/wiki/HTTP_ETag>). We'll first try updating this
ticket with an I<invalid> C<ETag> to see what happens.

+You can add attachments when you create a ticket. Simply add an "Attachments"
+entry in the JSON request content; the format of this entry is described
+in L<Add Attachments>
+
=head3 Updating Tickets

For updating tickets we use the C<PUT> verb, but otherwise it looks much
@@ -295,7 +299,7 @@ The time, in minutes, you've taken to work on your response/comment, optional.
=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/Ticket.pm b/lib/RT/REST2/Resource/Ticket.pm
index 02a020d02c..98e190966e 100644
--- a/lib/RT/REST2/Resource/Ticket.pm
+++ b/lib/RT/REST2/Resource/Ticket.pm
@@ -98,8 +98,23 @@ sub create_record {
Body => delete $data->{Content},
Type => delete $data->{ContentType} || 'text/plain',
);
+ if ( defined $data->{Attachments} ) {
+ return (\400, "Attachments must be an array") unless ref($data->{Attachments}) eq 'ARRAY';
+ foreach my $attachment (@{$data->{Attachments}}) {
+ return (\400, "Each element of Attachments must be a hash") unless ref($attachment) eq 'HASH';
+ foreach my $field (qw(FileName FileType FileContent)) {
+ return (\400, "Field $field is required for each attachment in Attachments") unless $attachment->{$field};
+ }
+ $data->{MIMEObj}->attach(
+ Type => $attachment->{FileType},
+ Filename => $attachment->{FileName},
+ Data => MIME::Base64::decode_base64($attachment->{FileContent}));
+ }
+ }
}

+ delete $data->{Attachments};
+
my ($ok, $txn, $msg) = $self->_create_record($data);
return ($ok, $msg);
}
@@ -171,6 +186,40 @@ sub hypermedia_links {
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
index ccfca41d22..a7c2a6c05a 100644
--- a/lib/RT/REST2/Util.pm
+++ b/lib/RT/REST2/Util.pm
@@ -217,6 +217,7 @@ sub deserialize_record {
# Sanitize input for the Perl API
for my $field (sort keys %$data) {
next if $field eq 'CustomFields';
+ next if $field eq 'Attachments';

my $value = $data->{$field};
next unless ref $value;
diff --git a/t/rest2/data/html.htm b/t/rest2/data/html.htm
new file mode 100644
index 0000000000..abce6679d6
--- /dev/null
+++ b/t/rest2/data/html.htm
@@ -0,0 +1 @@
+<p><i>Easy</i> as ?</p>
diff --git a/t/rest2/data/plain.txt b/t/rest2/data/plain.txt
new file mode 100644
index 0000000000..8ab686eafe
--- /dev/null
+++ b/t/rest2/data/plain.txt
@@ -0,0 +1 @@
+Hello, World!
diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 1642e80f3f..3573e4140c 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -4,6 +4,8 @@ use RT::Test::REST2 tests => undef;
use Test::Deep;
use MIME::Base64;

+use Encode qw(decode encode);
+
# Test using integer priorities
RT->Config->Set(EnablePriorityAsString => 0);
my $mech = RT::Test::REST2->mech;
@@ -508,4 +510,133 @@ my ($ticket_url, $ticket_id);
like($third_ticket->{_url}, qr{$rest_base_path/ticket/$ticket3_id$});
}

+# Ticket Creation - with attachments
+{
+ my $payload = {
+ Subject => 'Ticket creation using REST, with attachments.',
+ Queue => 'General',
+ Content => 'Testing ticket creation with attachments using REST API.',
+ Attachments => [.
+ { FileName => 'plain.txt',
+ FileType => 'text/plain',
+ FileContent => MIME::Base64::encode_base64('Hello, World!')
+ },
+ { FileName => 'html.htm',
+ FileType => 'text/html',
+ FileContent => MIME::Base64::encode_base64(
+ encode( 'UTF-8', "<p><i>Easy</i> as \x{03c0}</p>" )
+ ),
+ },
+ { FileName => 'moon.png',
+ FileType => 'image/png',
+ FileContent =>
+ 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAGQAAABkABchkaRQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAADfSURBVDiNndM9TsNAFATgzy5yjZSAE85JBygETgENUPF3iBCitHAFQkcIhZ/Ryn9gRlrZmp2Z3ef3TBOHOMULPrDBMrhpi/4HI5xjix2+4nmJRbx/Yh7ahvkpRPVV4QDXwT3UQy46zGkAZDgK/iytefvHgCrkJsqZUH6cLnNbABSxd5Jhhf1IbkMXv8Qux7hH1Ic1xvk/jBWy6gavumvtwx7ectwZXkKh7MA95XgObeOtpI2U4zl0kGbpxgiPvwQUcXLrKFchc82f6Ur0PK49azOnmOI4TBu84zm4SV38DeIVYkrYJyNbAAAAAElFTkSuQmCC'
+ },
+ ]
+ };
+
+ my $res = $mech->post_json( "$rest_base_path/ticket", $payload,
+ 'Authorization' => $auth, );
+ is( $res->code, 201 );
+ ok( $ticket_url = $res->header('location') );
+ ok( ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
+}
+
+# We have the attachments added above
+{
+ my $ticket = RT::Ticket->new($user);
+ $ticket->Load($ticket_id);
+ my $transaction_id = $ticket->Transactions->Last->id;
+ my $attachments = $ticket->Attachments->ItemsArrayRef;
+
+ # The 5 attachments are:
+ # 1) Top-level multipart
+ # 2) Top-level ticket content
+ # 3-5) The three attachments added in the Attachments array
+ is( scalar(@$attachments), 5 );
+
+ is( $attachments->[0]->ContentType, 'multipart/mixed' );
+
+ is( $attachments->[1]->ContentType, 'text/plain' );
+ is( $attachments->[1]->Content,
+ 'Testing ticket creation with attachments using REST API.' );
+
+ is( $attachments->[2]->ContentType, 'text/plain' );
+ is( $attachments->[2]->Filename, 'plain.txt' );
+ is( $attachments->[2]->Content, 'Hello, World!' );
+
+ is( $attachments->[3]->ContentType, 'text/html' );
+ is( $attachments->[3]->Filename, 'html.htm' );
+ is( $attachments->[3]->Content, "<p><i>Easy</i> as \x{03c0}</p>" );
+
+ is( $attachments->[4]->ContentType, 'image/png' );
+ is( $attachments->[4]->Filename, 'moon.png' );
+ like( $attachments->[4]->Content, qr/IHDR/, "Looks like a PNG image" );
+}
+
+# Ticket Creation - with attachments, using multipart/form-data
+my $json = JSON->new->utf8;
+{
+ my $plain_name = 'plain.txt';
+ my $plain_path = RT::Test::get_relocatable_file($plain_name, 'data');
+ my $html_name = 'html.htm';
+ my $html_path = RT::Test::get_relocatable_file($html_name, 'data');
+ my $img_name = 'image.png';
+ my $img_path = RT::Test::get_relocatable_file($img_name, 'data');
+
+ my $payload = {
+ Subject => 'Ticket creation using REST, multipart/form-data, with attachments.',
+ Queue => 'General',
+ Content => 'Testing ticket creation, multipart/form-data, with attachments using REST API.',
+ };
+
+ my $res = $mech->post( "$rest_base_path/ticket", $payload,
+ 'Authorization' => $auth,
+ 'Content_Type' => 'form-data',
+ 'Content' => [.
+ 'JSON' => $json->encode($payload),
+ 'Attachments' => [$plain_path, $plain_name, 'text/plain'],
+ 'Attachments' => [$html_path, $html_name, 'text/html' ],
+ 'Attachments' => [$img_path, $img_name, 'image/png' ]
+ ]);
+ is( $res->code, 201 );
+ ok( $ticket_url = $res->header('location') );
+ ok( ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
+}
+
+# Validate that the ticket was created correctly
+
+{
+ my $ticket = RT::Ticket->new($user);
+ $ticket->Load($ticket_id);
+ my $transaction_id = $ticket->Transactions->Last->id;
+ my $attachments = $ticket->Attachments->ItemsArrayRef;
+
+ # The 5 attachments are:
+ # 1) Top-level multipart
+ # 2) Top-level ticket content
+ # 3-5) The three attachments added in the Attachments array
+ is( scalar(@$attachments), 5 );
+
+ is( $attachments->[0]->ContentType, 'multipart/mixed' );
+
+ is( $attachments->[1]->ContentType, 'text/plain' );
+ is( $attachments->[1]->Content,
+ 'Testing ticket creation, multipart/form-data, with attachments using REST API.' );
+
+ is( $attachments->[2]->ContentType, 'text/plain' );
+ is( $attachments->[2]->Filename, 'plain.txt' );
+ is( $attachments->[2]->Content, "Hello, World!\n" );
+
+ is( $attachments->[3]->ContentType, 'text/html' );
+ is( $attachments->[3]->Filename, 'html.htm' );
+ is( $attachments->[3]->Content, "<p><i>Easy</i> as \x{03c0}</p>\n" );
+
+ is( $attachments->[4]->ContentType, 'image/png' );
+ is( $attachments->[4]->Filename, 'image.png' );
+ like( $attachments->[4]->Content, qr/IHDR/, "Looks like a PNG image" );
+}
+
+
+
done_testing;

-----------------------------------------------------------------------
_______________________________________________
rt-commit mailing list
rt-commit@lists.bestpractical.com
http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-commit