[Rt-commit] rt branch, 4.2/record-attachments-dropping, created. rt-4.2.1-55-gd6eee84

? sunnavy sunnavy at bestpractical.com
Tue Dec 10 04:15:31 EST 2013


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

- Log -----------------------------------------------------------------
commit 0c2b3eb177b6c75707acc43c4771ac38403c7df1
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 ee10301..5dd3996 100644
--- a/lib/RT/Attachment.pm
+++ b/lib/RT/Attachment.pm
@@ -199,10 +199,11 @@ sub Create {
     else {
 
         my ($encoding, $type);
-        ($encoding, $content, $type, $Filename) = $self->_EncodeLOB(
+        ($encoding, $content, $type, $Filename, my $note_args) = $self->_EncodeLOB(
             $Attachment->bodyhandle->as_string,
             $Attachment->mime_type,
-            $Filename
+            $Filename,
+            $args{'TransactionId'}
         );
 
         my $id = $self->SUPER::Create(
@@ -217,7 +218,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 d113c57..da3db7e 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -756,8 +756,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');
@@ -781,25 +783,52 @@ 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 ( $TransactionId ) {
+            my $txn = RT::Transaction->new(RT->SystemUser);
+            $txn->Load($TransactionId);
+            if ( $txn->id && $txn->Type =~ /^System/ ) {
+                $check_size = 0; # no size limit for system txn
+            }
+        }
 
+        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 );
             }
         }
 
@@ -816,7 +845,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 3b17319..f3eafe0 100644
--- a/lib/RT/Transaction.pm
+++ b/lib/RT/Transaction.pm
@@ -821,6 +821,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 d6eee84f1d6fc4d462b65fddca3469c090a18144
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..f6f3e3d
--- /dev/null
+++ b/t/web/attachment_dropping.t
@@ -0,0 +1,36 @@
+use warnings;
+use strict;
+
+use RT::Test tests => 11;
+use Digest::SHA 'sha1_hex';
+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 ( $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' );
+$m->submit;
+is( $m->status, 200, "request successful" );
+
+$m->content_contains( "$name was dropped", 'dropped message' );
+$m->follow_link_ok( { text => "Download $name" } );
+is( $m->content, 'Large attachment dropped', 'dropped $name' );
diff --git a/t/web/attachment_truncation.t b/t/web/attachment_truncation.t
new file mode 100644
index 0000000..f50f4d0
--- /dev/null
+++ b/t/web/attachment_truncation.t
@@ -0,0 +1,34 @@
+use warnings;
+use strict;
+
+use RT::Test tests => 12;
+use Digest::SHA 'sha1_hex';
+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 ( $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 test' );
+$m->field( 'Attach',  $path );
+$m->field( 'Content', 'Some content' );
+$m->submit;
+is( $m->status, 200, "request successful" );
+
+$m->content_contains( "$name was truncated", 'truncated message' );
+$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' );

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


More information about the rt-commit mailing list