Mailing List Archive

rt branch, 5.0/rest2-base64-encode-attachment-content, created. rt-5.0.0-54-ge75be21c8e
The branch, 5.0/rest2-base64-encode-attachment-content has been created
at e75be21c8e4bee4c7e6d535cba1b55b21ab8ad33 (commit)

- Log -----------------------------------------------------------------
commit 9976d751243195e34e417272f3eadd32222dcd83
Author: Dianne Skoll <dianne@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..32eb8e70be 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 textual content, OR if the raw data
+ # contains wide characters. If the raw data does indeed contain
+ # wide characters, encode_base64 will die anyway
+ if (RT::I18N::IsTextualContentType($self->record->ContentType) ||
+ $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 e75be21c8e4bee4c7e6d535cba1b55b21ab8ad33
Author: Dianne Skoll <dianne@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


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