[Rt-commit] rt branch, 4.2/record-attachments-dropping, created. rt-4.0.1-237-ga80a65f
? sunnavy
sunnavy at bestpractical.com
Thu Jul 7 04:28:39 EDT 2011
The branch, 4.2/record-attachments-dropping has been created
at a80a65fbb8fafe0c4f12487e3f93c931de3dab3a (commit)
- Log -----------------------------------------------------------------
commit 5f5e8b703d8bfcafac898de325f02a1862aca136
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Jul 7 14:53:55 2011 +0800
record attachments' dropping/truncation as SystemWarning txn
diff --git a/lib/RT/Attachment.pm b/lib/RT/Attachment.pm
index bf6644c..f7df9b9 100644
--- a/lib/RT/Attachment.pm
+++ b/lib/RT/Attachment.pm
@@ -194,11 +194,9 @@ sub Create {
else {
my ($encoding, $type);
- ($encoding, $content, $type, $Filename) = $self->_EncodeLOB(
- $Attachment->bodyhandle->as_string,
- $Attachment->mime_type,
- $Filename
- );
+ ( $encoding, $content, $type, $Filename, my $note_args ) =
+ $self->_EncodeLOB( $Attachment->bodyhandle->as_string,
+ $Attachment->mime_type, $Filename );
my $id = $self->SUPER::Create(
TransactionId => $args{'TransactionId'},
@@ -212,7 +210,18 @@ sub Create {
MessageId => $MessageId,
);
- unless ($id) {
+ if ($id) {
+ if ($note_args) {
+ my $ticket = $self->TransactionObj->TicketObj;
+ if ( $ticket && $ticket->isa('RT::Ticket') ) {
+ $ticket->_RecordNote(%$note_args);
+ }
+ else {
+ $RT::Logger->error("only ticket supports RecordNote");
+ }
+ }
+ }
+ else {
$RT::Logger->crit("Attachment insert failed: ". $RT::Handle->dbh->errstr);
}
return $id;
@@ -229,10 +238,28 @@ sub Import {
my $self = shift;
my %args = ( ContentEncoding => 'none', @_ );
- ( $args{'ContentEncoding'}, $args{'Content'} ) =
- $self->_EncodeLOB( $args{'Content'}, $args{'MimeType'} );
+ ( $args{'ContentEncoding'}, $args{'Content'}, undef, undef, my $note_args )
+ = $self->_EncodeLOB( $args{'Content'}, $args{'MimeType'},
+ $args{'Filename'} );
+
+ my $id = $self->SUPER::Create(%args);
+ if ($id) {
+ if ($note_args) {
+ my $ticket = $self->TransactionObj->TicketObj;
+ if ( $ticket && $ticket->isa('RT::Ticket') ) {
+ $ticket->_RecordNote(%$note_args);
+ }
+ else {
+ $RT::Logger->error("only ticket supports RecordNote");
+ }
+ }
+ }
+ else {
+ $RT::Logger->crit(
+ "Attachment import failed: " . $RT::Handle->dbh->errstr );
+ }
- return ( $self->SUPER::Create(%args) );
+ return $id;
}
=head2 TransactionObj
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index fcc7bed..5e7c12c 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -723,6 +723,7 @@ sub _EncodeLOB {
my $Filename = shift;
my $ContentEncoding = 'none';
+ my $note_args;
#get the max attachment length from RT
my $MaxSize = RT->Config->Get('MaxAttachmentSize');
@@ -753,7 +754,13 @@ sub _EncodeLOB {
# truncate the attachment to that length.
$Body = substr( $Body, 0, $MaxSize );
-
+ $note_args = {
+ NoteType => 'SystemWarning',
+ Content => (
+ ( defined $Filename ? $Filename : 'content' )
+ . ' was truncated.'
+ ),
+ };
}
# elsif we're supposed to drop large attachments on the floor,
@@ -763,8 +770,18 @@ sub _EncodeLOB {
$RT::Logger->info( "$self: Dropped an attachment of size "
. length($Body));
$RT::Logger->info( "It started: " . substr( $Body, 0, 60 ) );
+
+ $note_args = {
+ NoteType => 'SystemWarning',
+ Content => (
+ ( defined $Filename ? $Filename : 'content' )
+ . ' was dropped.'
+ ),
+ };
+
$Filename .= ".txt" if $Filename;
- return ("none", "Large attachment dropped", "plain/text", $Filename );
+ return ( "none", "Large attachment dropped",
+ "plain/text", $Filename, $note_args );
}
}
@@ -781,7 +798,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 2baccc2..341de18 100644
--- a/lib/RT/Transaction.pm
+++ b/lib/RT/Transaction.pm
@@ -673,8 +673,8 @@ sub BriefDescription {
)
);
}
- elsif ( $type =~ /SystemError/ ) {
- return $self->loc("System error");
+ elsif ( $type =~ /^System(\w+)/ ) {
+ return $self->loc('System ' . lc $1);
}
elsif ( $type =~ /Forward Transaction/ ) {
return $self->loc( "Forwarded Transaction #[_1] to [_2]",
commit 4efb4626d75c2309b7692449f73c8379e623850e
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Jul 7 14:56:41 2011 +0800
warn log level seems more right for dropping/truncation of attachments
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index 5e7c12c..f0521a5 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -752,6 +752,8 @@ sub _EncodeLOB {
# if we're supposed to truncate large attachments
if (RT->Config->Get('TruncateLongAttachments')) {
+ $RT::Logger->warn( "$self: Truncated an attachment of size "
+ . length($Body));
# truncate the attachment to that length.
$Body = substr( $Body, 0, $MaxSize );
$note_args = {
@@ -767,9 +769,9 @@ sub _EncodeLOB {
elsif (RT->Config->Get('DropLongAttachments')) {
# drop the attachment on the floor
- $RT::Logger->info( "$self: Dropped an attachment of size "
+ $RT::Logger->warn( "$self: Dropped an attachment of size "
. length($Body));
- $RT::Logger->info( "It started: " . substr( $Body, 0, 60 ) );
+ $RT::Logger->warn( "It started: " . substr( $Body, 0, 60 ) );
$note_args = {
NoteType => 'SystemWarning',
commit c0d937bb4c5a9ecd7d4b4da0595991188f0dcb07
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Jul 7 15:39:04 2011 +0800
no need to append .txt if it has .txt already
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index f0521a5..12efd32 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -781,7 +781,7 @@ sub _EncodeLOB {
),
};
- $Filename .= ".txt" if $Filename;
+ $Filename .= ".txt" if $Filename && $Filename !~ /\.txt$/;
return ( "none", "Large attachment dropped",
"plain/text", $Filename, $note_args );
}
commit dc344f4536eb0ddcad46446633077fb307c07b14
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Jul 7 15:50:39 2011 +0800
test attachments dropping/truncation
diff --git a/t/web/attachment_dropping.t b/t/web/attachment_dropping.t
new file mode 100644
index 0000000..5a5eea9
--- /dev/null
+++ b/t/web/attachment_dropping.t
@@ -0,0 +1,33 @@
+use warnings;
+use strict;
+
+use RT::Test tests => 11;
+
+RT->Config->Set( 'MaxAttachmentSize', 1000 );
+use File::Temp 'tempfile';
+my ( $fh, $path ) = tempfile( UNLINK => 1, SUFFIX => '.txt' );
+print $fh 'a' x 1000 . 'b' x 10;
+close $fh;
+my $name = ( File::Spec->splitpath($path) )[2];
+
+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..c60b7a2
--- /dev/null
+++ b/t/web/attachment_truncation.t
@@ -0,0 +1,32 @@
+use warnings;
+use strict;
+
+use RT::Test tests => 12;
+RT->Config->Set( 'MaxAttachmentSize', 1000 );
+use File::Temp 'tempfile';
+my ( $fh, $path ) = tempfile( UNLINK => 1, SUFFIX => '.txt' );
+print $fh 'a' x 1000 . 'b' x 10;
+close $fh;
+my $name = ( File::Spec->splitpath($path) )[2];
+
+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' );
+
commit a80a65fbb8fafe0c4f12487e3f93c931de3dab3a
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Jul 7 16:21:05 2011 +0800
record sha1 of dropped/truncated message
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index 12efd32..5106da9 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -755,14 +755,15 @@ sub _EncodeLOB {
$RT::Logger->warn( "$self: Truncated an attachment of size "
. length($Body));
# truncate the attachment to that length.
- $Body = substr( $Body, 0, $MaxSize );
+ my $note = ( defined $Filename ? $Filename : 'content' )
+ . ' was truncated, sha1 is ' . Digest::SHA::sha1_hex($Body)
+ . '.';
+
$note_args = {
NoteType => 'SystemWarning',
- Content => (
- ( defined $Filename ? $Filename : 'content' )
- . ' was truncated.'
- ),
+ Content => $note,
};
+ $Body = substr( $Body, 0, $MaxSize );
}
# elsif we're supposed to drop large attachments on the floor,
@@ -773,12 +774,14 @@ sub _EncodeLOB {
. length($Body));
$RT::Logger->warn( "It started: " . substr( $Body, 0, 60 ) );
+ my $note =
+ ( defined $Filename ? $Filename : 'content' )
+ . ' was dropped, sha1 is '
+ . Digest::SHA::sha1_hex($Body) . '.';
+
$note_args = {
NoteType => 'SystemWarning',
- Content => (
- ( defined $Filename ? $Filename : 'content' )
- . ' was dropped.'
- ),
+ Content => $note,
};
$Filename .= ".txt" if $Filename && $Filename !~ /\.txt$/;
diff --git a/t/web/attachment_dropping.t b/t/web/attachment_dropping.t
index 5a5eea9..595d5b4 100644
--- a/t/web/attachment_dropping.t
+++ b/t/web/attachment_dropping.t
@@ -2,14 +2,18 @@ use warnings;
use strict;
use RT::Test tests => 11;
-
-RT->Config->Set( 'MaxAttachmentSize', 1000 );
+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 'a' x 1000 . 'b' x 10;
+print $fh $content;
close $fh;
+
+my $sha1 = sha1_hex($content);
my $name = ( File::Spec->splitpath($path) )[2];
+RT->Config->Set( 'MaxAttachmentSize', 1000 );
RT->Config->Set( 'TruncateLongAttachments', '0' );
RT->Config->Set( 'DropLongAttachments', '1' );
@@ -28,6 +32,6 @@ $m->field( 'Content', 'Some content' );
$m->submit;
is( $m->status, 200, "request successful" );
-$m->content_contains( "$name was dropped", 'dropped message' );
+$m->content_contains( "$name was dropped, sha1 is $sha1.", '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
index c60b7a2..9d04c03 100644
--- a/t/web/attachment_truncation.t
+++ b/t/web/attachment_truncation.t
@@ -2,13 +2,17 @@ use warnings;
use strict;
use RT::Test tests => 12;
-RT->Config->Set( 'MaxAttachmentSize', 1000 );
+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 'a' x 1000 . 'b' x 10;
+print $fh $content;
close $fh;
my $name = ( File::Spec->splitpath($path) )[2];
+my $sha1 = sha1_hex($content);
+RT->Config->Set( 'MaxAttachmentSize', 1000 );
RT->Config->Set( 'TruncateLongAttachments', '1' );
my ( $baseurl, $m ) = RT::Test->started_ok;
ok $m->login, 'logged in';
@@ -25,7 +29,7 @@ $m->field( 'Content', 'Some content' );
$m->submit;
is( $m->status, 200, "request successful" );
-$m->content_contains( "$name was truncated", 'truncated message' );
+$m->content_contains( "$name was truncated, sha1 is $sha1.", '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