[Rt-commit] rt branch, 5.0-trunk, updated. rt-5.0.0-55-g3d1035313a

? sunnavy sunnavy at bestpractical.com
Wed Oct 21 16:40:47 EDT 2020


The branch, 5.0-trunk has been updated
       via  3d1035313a684013c1f4dfeabcfef32f21bc29f6 (commit)
       via  64a7e39ed67851992642d54c500c61a71d8500d7 (commit)
       via  918afdea682e0784726cbc141c4d43a9b5f7c5cf (commit)
      from  ca61b7ee8973386c6f3097e5b1c1af8a461d70f0 (commit)

Summary of changes:
 lib/RT/REST2.pm                     |  3 ++-
 lib/RT/REST2/Resource/Attachment.pm | 21 +++++++++++++++++++++
 t/rest2/tickets.t                   |  9 ++++++---
 3 files changed, 29 insertions(+), 4 deletions(-)

- Log -----------------------------------------------------------------
commit 918afdea682e0784726cbc141c4d43a9b5f7c5cf
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..7ea76a1c14 100644
--- a/lib/RT/REST2/Resource/Attachment.pm
+++ b/lib/RT/REST2/Resource/Attachment.pm
@@ -50,6 +50,9 @@ package RT::REST2::Resource::Attachment;
 use strict;
 use warnings;
 
+use MIME::Base64;
+use Encode;
+
 use Moose;
 use namespace::autoclean;
 
@@ -68,6 +71,24 @@ sub dispatch_rules {
     )
 }
 
+# Tweak serialize to base-64-encode Content
+around 'serialize' => sub {
+    my ($orig, $self) = @_;
+    my $data = $self->$orig(@_);
+    return $data unless defined $data->{Content};
+
+    # 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});
+    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 64a7e39ed67851992642d54c500c61a71d8500d7
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
 

commit 3d1035313a684013c1f4dfeabcfef32f21bc29f6
Merge: ca61b7ee89 64a7e39ed6
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Oct 22 04:08:03 2020 +0800

    Merge branch '5.0/rest2-base64-encode-attachment-content' into 5.0-trunk


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


More information about the rt-commit mailing list