[Rt-commit] rt branch, 4.2/record-attachments-dropping, created. rt-4.2.3-84-g6b61083

? sunnavy sunnavy at bestpractical.com
Wed Apr 9 12:00:50 EDT 2014


The branch, 4.2/record-attachments-dropping has been created
        at  6b61083208d83da0599715e1268ccd8d55e33025 (commit)

- Log -----------------------------------------------------------------
commit ff90d31d0e4c76b318ecb83cdce8b617a55c00e9
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sun Dec 8 10:26:09 2013 +0800

    record attachments' dropping/truncation as SystemWarning txn

diff --git a/lib/RT/Attachment.pm b/lib/RT/Attachment.pm
index 800f4f5..11a5277 100644
--- a/lib/RT/Attachment.pm
+++ b/lib/RT/Attachment.pm
@@ -198,12 +198,19 @@ sub Create {
     #If it's not multipart
     else {
 
-        my ($encoding, $type);
-        ($encoding, $content, $type, $Filename) = $self->_EncodeLOB(
-            $Attachment->bodyhandle->as_string,
-            $Attachment->mime_type,
-            $Filename
-        );
+        my ( $encoding, $type, $note_args );
+
+        my $txn = RT::Transaction->new( RT->SystemUser );
+        $txn->Load($args{'TransactionId'});
+        if ( $txn->Type =~ /^System/ ) {
+            $encoding = 'none';
+            $type = $Attachment->mime_type;
+            $content  = $Attachment->bodyhandle->as_string;
+        }
+        else {
+            ( $encoding, $content, $type, $Filename, $note_args ) =
+                $self->_EncodeLOB( $Attachment->bodyhandle->as_string, $Attachment->mime_type, $Filename, );
+        }
 
         my $id = $self->SUPER::Create(
             TransactionId   => $args{'TransactionId'},
@@ -217,7 +224,18 @@ sub Create {
             MessageId       => $MessageId,
         );
 
-        unless ($id) {
+        if ($id) {
+            if ($note_args) {
+                my $object = $self->TransactionObj->Object;
+                if ( $object && $object->can('_RecordNote') ) {
+                    $object->_RecordNote(%$note_args);
+                }
+                else {
+                    $RT::Logger->error( ref($object) . " doesn't support _RecordNote" );
+                }
+            }
+        }
+        else {
             $RT::Logger->crit("Attachment insert failed: ". $RT::Handle->dbh->errstr);
         }
         return $id;
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index 788aa42..f82a879 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -748,7 +748,7 @@ sub _Accessible  {
 =head2 _EncodeLOB BODY MIME_TYPE FILENAME
 
 Takes a potentially large attachment. Returns (ContentEncoding,
-EncodedBody, MimeType, Filename) based on system configuration and
+EncodedBody, MimeType, Filename, NoteArgs) based on system configuration and
 selected database.  Returns a custom (short) text/plain message if
 DropLongAttachments causes an attachment to not be stored.
 
@@ -760,6 +760,10 @@ encoded on databases which are strict.
 This function expects to receive an octet string in order to properly
 evaluate and encode it.  It will return an octet string.
 
+NoteArgs is currently used to indicate caller that the message is too long and
+is truncated or dropped, which could be used to create a system warning
+transaction.
+
 =cut
 
 sub _EncodeLOB {
@@ -767,8 +771,10 @@ sub _EncodeLOB {
         my $Body = shift;
         my $MIMEType = shift || '';
         my $Filename = shift;
+        my $TransactionId = shift;
 
         my $ContentEncoding = 'none';
+        my $note_args;
 
         #get the max attachment length from RT
         my $MaxSize = RT->Config->Get('MaxAttachmentSize');
@@ -792,25 +798,45 @@ sub _EncodeLOB {
         }
 
         #if the attachment is larger than the maximum size
-        if ( ($MaxSize) and ( $MaxSize < length($Body) ) ) {
+        my $check_size = $MaxSize && $MaxSize < length($Body);
+
+        if ( $check_size ) {
 
+            my $size = length($Body);
             # if we're supposed to truncate large attachments
             if (RT->Config->Get('TruncateLongAttachments')) {
+                $RT::Logger->info("$self: Truncated an attachment of size $size");
+                my $note = ( defined $Filename ? $Filename : 'content' )
+                  . " was truncated because its size($size) exceeds max size setting($MaxSize).";
 
                 # truncate the attachment to that length.
                 $Body = substr( $Body, 0, $MaxSize );
 
+                $note_args = {
+                    NoteType => 'SystemWarning',
+                    Content => $note,
+                };
+
             }
 
             # elsif we're supposed to drop large attachments on the floor,
             elsif (RT->Config->Get('DropLongAttachments')) {
 
                 # drop the attachment on the floor
-                $RT::Logger->info( "$self: Dropped an attachment of size "
-                                   . length($Body));
+                $RT::Logger->info("$self: Dropped an attachment of size $size");
                 $RT::Logger->info( "It started: " . substr( $Body, 0, 60 ) );
-                $Filename .= ".txt" if $Filename;
-                return ("none", "Large attachment dropped", "text/plain", $Filename );
+
+                my $note =
+                    ( defined $Filename ? $Filename : 'content' )
+                  . " was dropped because its size($size) exceeds max size setting($MaxSize).";
+
+                $note_args = {
+                    NoteType => 'SystemWarning',
+                    Content => $note,
+                };
+
+                $Filename .= ".txt" if $Filename && $Filename !~ /\.txt$/;
+                return ( "none", "Large attachment dropped", "plain/text", $Filename, $note_args );
             }
         }
 
@@ -827,7 +853,7 @@ sub _EncodeLOB {
         }
 
 
-        return ($ContentEncoding, $Body, $MIMEType, $Filename );
+        return ($ContentEncoding, $Body, $MIMEType, $Filename, $note_args );
 
 }
 
diff --git a/lib/RT/Transaction.pm b/lib/RT/Transaction.pm
index 025de94..19da573 100644
--- a/lib/RT/Transaction.pm
+++ b/lib/RT/Transaction.pm
@@ -841,6 +841,10 @@ sub _FormatUser {
         my $self = shift;
         return ("System error"); #loc()
     },
+    SystemWarning => sub {
+        my $self = shift;
+        return ("System warning"); #loc()
+    },
     "Forward Transaction" => sub {
         my $self = shift;
         my $recipients = join ", ", map {

commit 6b61083208d83da0599715e1268ccd8d55e33025
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sun Dec 8 10:27:06 2013 +0800

    test attachment dropping/truncation

diff --git a/t/web/attachment_dropping.t b/t/web/attachment_dropping.t
new file mode 100644
index 0000000..aaed85b
--- /dev/null
+++ b/t/web/attachment_dropping.t
@@ -0,0 +1,51 @@
+use warnings;
+use strict;
+
+use RT::Test tests => undef;
+use File::Temp 'tempfile';
+
+my $content = 'a' x 1000 . 'b' x 10;
+my ( $fh, $path ) = tempfile( UNLINK => 1, SUFFIX => '.txt' );
+print $fh $content;
+close $fh;
+
+my $name = ( File::Spec->splitpath($path) )[2];
+
+RT->Config->Set( 'MaxAttachmentSize', 1000 );
+RT->Config->Set( 'TruncateLongAttachments', '0' );
+RT->Config->Set( 'DropLongAttachments',     '1' );
+
+my $cf = RT::CustomField->new( RT->SystemUser );
+ok(
+    $cf->Create(
+        Name  => 'test truncation',
+        Queue => '0',
+        Type  => 'FreeformSingle',
+    ),
+);
+my $cfid = $cf->id;
+
+my ( $baseurl, $m ) = RT::Test->started_ok;
+ok $m->login, 'logged in';
+
+my $queue = RT::Test->load_or_create_queue( Name => 'General' );
+ok( $queue->id, "Loaded General queue" );
+$m->get_ok( $baseurl . '/Ticket/Create.html?Queue=' . $queue->id );
+$m->content_contains( "Create a new ticket", 'ticket create page' );
+
+$m->form_name('TicketCreate');
+$m->field( 'Subject', 'Attachments dropping test' );
+$m->field( 'Attach',  $path );
+$m->field( 'Content', 'Some content' );
+my $cf_content = 'cf' . 'a' x 998 . 'cfb';
+$m->field( "Object-RT::Ticket--CustomField-$cfid-Value", $cf_content );
+$m->submit;
+is( $m->status, 200, "request successful" );
+
+$m->content_contains( "$name was dropped", 'dropped message' );
+$m->content_lacks( 'cfaaaa', 'cf value was dropped' );
+$m->follow_link_ok( { text => "Download $name" } );
+is( $m->content, 'Large attachment dropped', 'dropped $name' );
+
+undef $m;
+done_testing;
diff --git a/t/web/attachment_truncation.t b/t/web/attachment_truncation.t
new file mode 100644
index 0000000..a291f90
--- /dev/null
+++ b/t/web/attachment_truncation.t
@@ -0,0 +1,52 @@
+use warnings;
+use strict;
+
+use RT::Test tests => undef;
+use File::Temp 'tempfile';
+
+my $content = 'a' x 1000 . 'b' x 10;
+my ( $fh, $path ) = tempfile( UNLINK => 1, SUFFIX => '.txt' );
+print $fh $content;
+close $fh;
+my $name = ( File::Spec->splitpath($path) )[2];
+
+RT->Config->Set( 'MaxAttachmentSize', 1000 );
+RT->Config->Set( 'TruncateLongAttachments', '1' );
+
+my $queue = RT::Test->load_or_create_queue( Name => 'General' );
+ok( $queue->id, "Loaded General queue" );
+
+my $cf = RT::CustomField->new( RT->SystemUser );
+ok(
+    $cf->Create(
+        Name  => 'test truncation',
+        Queue => '0',
+        Type  => 'FreeformSingle',
+    ),
+);
+my $cfid = $cf->id;
+
+my ( $baseurl, $m ) = RT::Test->started_ok;
+ok $m->login, 'logged in';
+
+$m->get_ok( $baseurl . '/Ticket/Create.html?Queue=' . $queue->id );
+$m->content_contains( "Create a new ticket", 'ticket create page' );
+
+$m->form_name('TicketCreate');
+$m->field( 'Subject', 'Attachments test' );
+$m->field( 'Attach',  $path );
+$m->field( 'Content', 'Some content' );
+my $cf_content = 'cf' . 'a' x 998 . 'cfb';
+$m->field( "Object-RT::Ticket--CustomField-$cfid-Value", $cf_content );
+$m->submit;
+is( $m->status, 200, "request successful" );
+
+$m->content_contains( "$name was truncated", 'truncated message' );
+$m->content_contains( 'cf' . 'a' x 998, 'has the first 1000 cf chars' );
+$m->content_lacks( 'cfb', 'lacks cf chars after that' );
+$m->follow_link_ok( { text => "Download $name" } );
+$m->content_contains( 'a' x 1000, 'has the first 1000 chars' );
+$m->content_lacks( 'b', 'lacks chars after that' );
+
+undef $m;
+done_testing;

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


More information about the rt-commit mailing list