[Rt-commit] rt branch, 5.0/rest2-base64-encode-attachment-content, created. rt-5.0.0-54-g8e58a3f43d

Dianne Skoll dianne at bestpractical.com
Wed Oct 21 15:48:35 EDT 2020


The branch, 5.0/rest2-base64-encode-attachment-content has been created
        at  8e58a3f43d3d13ac3bdc60f44de9446f71c31b4f (commit)

- Log -----------------------------------------------------------------
commit f1724f95666f20142b10cbc69b56cdf15f9b743d
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Fri Oct 16 11:53:22 2020 -0400

    Make GET .../REST2/attachment/:id base64-encode the Content field.

diff --git a/lib/RT/REST2/Resource/Attachment.pm b/lib/RT/REST2/Resource/Attachment.pm
index 40539fe238..a752a59a23 100644
--- a/lib/RT/REST2/Resource/Attachment.pm
+++ b/lib/RT/REST2/Resource/Attachment.pm
@@ -50,6 +50,10 @@ package RT::REST2::Resource::Attachment;
 use strict;
 use warnings;
 
+use MIME::Base64;
+use RT::I18N;
+use Encode;
+
 use Moose;
 use namespace::autoclean;
 
@@ -68,6 +72,23 @@ sub dispatch_rules {
     )
 }
 
+# Tweak serialize to base-64-encode Content
+around 'serialize' => sub {
+    my ($orig, $self) = @_;
+    my $data = $self->$orig(@_);
+
+    # Encode as UTF-8 if it's an internal Perl Unicode string, or if it
+    # contains wide characters.  If the raw data does indeed contain
+    # wide characters, encode_base64 will die anyway, so encoding
+    # seems like a safer choice.
+    if (utf8::is_utf8($data->{Content}) || $data->{Content} =~ /[^\x00-\xFF]/) {
+        # Encode internal Perl string to UTF-8
+        $data->{Content} = encode('UTF-8', $data->{Content}, Encode::FB_PERLQQ);
+    }
+    $data->{Content} = encode_base64($data->{Content}) if defined($data->{Content});
+    return $data;
+};
+
 __PACKAGE__->meta->make_immutable;
 
 1;
diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 837efd2992..1642e80f3f 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -2,6 +2,7 @@ use strict;
 use warnings;
 use RT::Test::REST2 tests => undef;
 use Test::Deep;
+use MIME::Base64;
 
 # Test using integer priorities
 RT->Config->Set(EnablePriorityAsString => 0);
@@ -405,14 +406,14 @@ my ($ticket_url, $ticket_id);
     );
     is($res->code, 200);
     $content = $mech->json_response;
-    is($content->{Content}, 'Hello from hypermedia!');
+    is($content->{Content}, encode_base64('Hello from hypermedia!'));
     is($content->{ContentType}, 'text/plain');
 }
 
 # Ticket Comment
 {
     my $payload = {
-        Content     => '<i>(hello secret camera)</i>',
+        Content     => "<i>(hello secret camera \x{5e9}\x{5dc}\x{5d5}\x{5dd})</i>",
         ContentType => 'text/html',
         Subject     => 'shh',
         TimeTaken   => 129,
@@ -465,7 +466,9 @@ my ($ticket_url, $ticket_id);
     is($res->code, 200);
     $content = $mech->json_response;
     is($content->{Subject}, 'shh');
-    is($content->{Content}, '<i>(hello secret camera)</i>');
+
+    # Note below: D7 A9 is the UTF-8 encoding of U+5E9, etc.
+    is($content->{Content}, encode_base64("<i>(hello secret camera \xD7\xA9\xD7\x9C\xD7\x95\xD7\x9D)</i>"));
     is($content->{ContentType}, 'text/html');
 }
 

commit 8e58a3f43d3d13ac3bdc60f44de9446f71c31b4f
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Fri Oct 16 12:06:23 2020 -0400

    Document that GET /attachment/:id base64-encodes the content.

diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 1079f95cd6..e7a4f6d27e 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -558,7 +558,8 @@ Below are some examples using the endpoints above.
         get attachments for transaction
 
     GET /attachment/:id
-        retrieve an attachment
+        retrieve an attachment.  Note that the C<Content> field contains
+        the base64-encoded representation of the raw content.
 
 =head3 Image and Binary Object Custom Field Values
 

-----------------------------------------------------------------------


More information about the rt-commit mailing list