Skip to content

Commit

Permalink
calculate attachment's length on create and store
Browse files Browse the repository at this point in the history
  • Loading branch information
ruz committed Feb 16, 2011
1 parent 7918101 commit 337893c
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/RT/Attachment.pm
Expand Up @@ -125,26 +125,39 @@ sub Create {
# If we possibly can, collapse it to a singlepart
$Attachment->make_singlepart;

my $head = $Attachment->head;

# Get the subject
my $Subject = $Attachment->head->get( 'subject', 0 );
my $Subject = $head->get( 'subject', 0 );
$Subject = '' unless defined $Subject;
chomp $Subject;
utf8::decode( $Subject ) unless utf8::is_utf8( $Subject );

#Get the Message-ID
my $MessageId = $Attachment->head->get( 'Message-ID', 0 );
my $MessageId = $head->get( 'Message-ID', 0 );
defined($MessageId) or $MessageId = '';
chomp ($MessageId);
$MessageId =~ s/^<(.*?)>$/$1/o;

#Get the filename
my $Filename = $Attachment->head->recommended_filename;
my $Filename = $head->recommended_filename;
# remove path part.
$Filename =~ s!.*/!! if $Filename;

my $content;
unless ( $head->get('Content-Length') ) {
my $length = 0;
if ( defined $Attachment->bodyhandle ) {
$content = $Attachment->bodyhandle->as_string;
utf8::encode( $content ) if utf8::is_utf8( $content );
$length = length $content;
}
$head->replace( 'Content-Length' => $length );
}
$head = $head->as_string;

# MIME::Head doesn't support perl strings well and can return
# octets which later will be double encoded in low-level code
my $head = $Attachment->head->as_string;
utf8::decode( $head ) unless utf8::is_utf8( $head );

# If a message has no bodyhandle, that means that it has subparts (or appears to)
Expand Down Expand Up @@ -180,20 +193,21 @@ sub Create {
#If it's not multipart
else {

my ($ContentEncoding, $Body, $ContentType, $Filename) = $self->_EncodeLOB(
my ($encoding, $type);
($encoding, $content, $type, $Filename) = $self->_EncodeLOB(
$Attachment->bodyhandle->as_string,
$Attachment->mime_type,
$Filename
);

my $id = $self->SUPER::Create(
TransactionId => $args{'TransactionId'},
ContentType => $ContentType,
ContentEncoding => $ContentEncoding,
ContentType => $type,
ContentEncoding => $encoding,
Parent => $args{'Parent'},
Headers => $head,
Subject => $Subject,
Content => $Body,
Content => $content,
Filename => $Filename,
MessageId => $MessageId,
);
Expand Down

0 comments on commit 337893c

Please sign in to comment.