-sub Create {
- my $self = shift;
- my ($id);
- my %args = ( id => 0,
- TransactionId => 0,
- Parent => 0,
- Attachment => undef,
- @_
- );
-
-
- #For ease of reference
- my $Attachment = $args{'Attachment'};
-
- #if we didn't specify a ticket, we need to bail
- if ( $args{'TransactionId'} == 0) {
- $RT::Logger->crit("RT::Attachment->Create couldn't, as you didn't specify a transaction\n");
- return (0);
-
- }
-
- #If we possibly can, collapse it to a singlepart
- $Attachment->make_singlepart;
-
- #Get the subject
- my $Subject = $Attachment->head->get('subject',0);
- defined($Subject) or $Subject = '';
- chomp($Subject);
-
- #Get the filename
- my $Filename = $Attachment->head->recommended_filename;
-
- if ($Attachment->parts) {
- $id = $self->SUPER::Create(TransactionId => $args{'TransactionId'},
- Parent => 0,
- ContentType => $Attachment->mime_type,
- Headers => $Attachment->head->as_string,
- Subject => $Subject,
-
- );
- foreach my $part ($Attachment->parts) {
- my $SubAttachment = new RT::Attachment($self->CurrentUser);
- $SubAttachment->Create(TransactionId => $args{'TransactionId'},
- Parent => $id,
- Attachment => $part,
- ContentType => $Attachment->mime_type,
- Headers => $Attachment->head->as_string(),
-
- );
- }
- return ($id);
- }
-
-
- #If it's not multipart
- else {
-
- my $ContentEncoding = 'none';
-
- my $Body = $Attachment->bodyhandle->as_string;
-
- #get the max attachment length from RT
- my $MaxSize = $RT::MaxAttachmentSize;
-
- #if the current attachment contains nulls and the
- #database doesn't support embedded nulls
-
- if ( (! $RT::Handle->BinarySafeBLOBs) &&
- ( $Body =~ /\x00/ ) ) {
- # set a flag telling us to mimencode the attachment
- $ContentEncoding = 'base64';
-
- #cut the max attchment size by 25% (for mime-encoding overhead.
- $RT::Logger->debug("Max size is $MaxSize\n");
- $MaxSize = $MaxSize * 3/4;
- }
-
- #if the attachment is larger than the maximum size
- if (($MaxSize) and ($MaxSize < length($Body))) {
- # if we're supposed to truncate large attachments
- if ($RT::TruncateLongAttachments) {
- # truncate the attachment to that length.
- $Body = substr ($Body, 0, $MaxSize);
-
- }
-
- # elsif we're supposed to drop large attachments on the floor,
- elsif ($RT::DropLongAttachments) {
- # drop the attachment on the floor
- $RT::Logger->info("$self: Dropped an attachment of size ". length($Body).
- "\n". "It started: ". substr($Body, 0, 60) . "\n");
- return(undef);
- }
- }
- # if we need to mimencode the attachment
- if ($ContentEncoding eq 'base64') {
- # base64 encode the attachment
- $Body = MIME::Base64::encode_base64($Body);
-
- }
-
- my $id = $self->SUPER::Create(TransactionId => $args{'TransactionId'},
- ContentType => $Attachment->mime_type,
- ContentEncoding => $ContentEncoding,
- Parent => $args{'Parent'},
- Content => $Body,
- Headers => $Attachment->head->as_string,
- Subject => $Subject,
- Filename => $Filename,
- );
- return ($id);
- }
-}