--- ../rt-3.6.4/html/Ticket/Attachment/dhandler	Tue Jun 20 02:44:04 2006
+++ html/Ticket/Attachment/dhandler	Wed Aug  1 09:26:31 2007
@@ -44,22 +44,43 @@
 %# 
 %# END BPS TAGGED BLOCK }}}
 <%perl>
-     my ($ticket, $trans,$attach, $filename);
+     my ($ticket, $trans,$attach, $filename, $is_thumb, $rotate, $invert);
      my $arg = $m->dhandler_arg;                # get rest of path
+     $is_thumb = 0;
+     $rotate = 0;
      if ($arg =~ '^(\d+)/(\d+)') {
         $trans = $1;
         $attach = $2;
+     }elsif ( $arg =~ '^th(\d+)/(\d+)' ){
+        $trans = $1;
+        $attach = $2;
+        $is_thumb = 1;
+     }elsif ( $arg =~ '^thr(\d+)/(\d+)' ){
+        $trans = $1;
+        $attach = $2;
+        $is_thumb = 1;
+        $rotate = 1;
+     }elsif ( $arg =~ '^thi(\d+)/(\d+)' ){
+	$trans = $1;
+	$attach = $2;
+	$is_thumb = 1;
+	$invert = 1;
+     }elsif ( $arg =~ '^thri(\d+)/(\d+)' ){
+        $trans = $1;
+        $attach = $2;
+        $is_thumb = 1;
+	$rotate = 1;
+        $invert = 1;
+     }else{
+        Abort("Corrupted attachment URL. Arg was '".$arg."'");
      }
-    else {
-        Abort("Corrupted attachment URL.");
-        }
      my $AttachmentObj = new RT::Attachment($session{'CurrentUser'});
      $AttachmentObj->Load($attach) || Abort("Attachment '$attach' could not be loaded");
 
 
      unless ($AttachmentObj->id) {
         Abort("Bad attachment id. Couldn't find attachment '$attach'\n");
-    }
+     }
      unless ($AttachmentObj->TransactionId() == $trans ) {
         Abort("Bad transaction number for attachment. $trans should be".$AttachmentObj->TransactionId() ."\n");
 
@@ -84,7 +105,64 @@
 
      $r->content_type( $content_type );
      $m->clear_buffer();
-     $m->out($AttachmentObj->OriginalContent);
+     my $Contents = $AttachmentObj->OriginalContent;     
+     if ($Contents eq "AttachmentMovedToFile" ){
+         my $origfile = $RT::AttachmentsDirectory."/".$trans.".".$attach;
+         my $filetoload = $origfile;
+         if ( $RT::ProduceImageThumbs && $is_thumb == 1 && -f "$origfile" ){ # we use thumbs technique and want thumb as output, we do not produce thumbs from DB
+             my $filenamethumb;
+	     my $format = "jpeg";
+	     my $nprefix = '';
+	     $nprefix .= 'r' if ( $rotate == 1);
+	     $nprefix .= 'i' if ( $invert == 1);
+             $filenamethumb = $RT::ImageThumbsDirectory."/".$nprefix.$trans.".".$attach.'.'.$format;
+             if ( ! -f "$filenamethumb" ){ # we do not have such thumb, let's generate it
+                  my $q=Image::Magick->new;
+		  alarm(3);
+                  my $x = $q->Read("$filetoload");
+		  alarm(0);
+                  $RT::Logger->debug("ImageMagic: $x") if "$x";
+                  if ($q->Get('width') >= 900 ){
+                      $q->Resample(density=>'96x96');
+                      $q->Resize(geometry=>"800x30000", filter=>'Cubic', support=> 0.5);
+                  }
+                  my $image = $q->Montage(mode =>'Concatenate', tile=>'1x20');
+                  undef $q;
+
+		  $image->Set(quality=>'80');
+                  $x = $image->Write($format.":".$RT::ImageThumbsDirectory."/".$trans.".".$attach.'.'.$format);
+                  $RT::Logger->debug("ImageMagic: $x") if "$x";
+
+		  $image->Rotate(degrees=>'180');
+		  $x = $image->Write($format.":".$RT::ImageThumbsDirectory."/"."r".$trans.".".$attach.'.'.$format);
+		  $RT::Logger->debug("ImageMagic: $x") if "$x";
+
+		  $image->Negate();
+		  $x = $image->Write($format.":".$RT::ImageThumbsDirectory."/"."ri".$trans.".".$attach.'.'.$format);
+		  $RT::Logger->debug("ImageMagic: $x") if "$x";
+
+		  $image->Rotate(degrees=>'180');
+		  $x = $image->Write($format.":".$RT::ImageThumbsDirectory."/"."i".$trans.".".$attach.'.'.$format);
+		  $RT::Logger->debug("ImageMagic: $x") if "$x";
+             }
+             $filetoload = $filenamethumb;
+             $r->content_type( 'image/'.$format );
+         }
+         if (open (ATTACHF, $filetoload) ) {
+             $RT::Logger->debug("Loading attachment (TrID:".$trans.", AtID:".$attach.", Filename:'".$filetoload."') from file.\n") if ($RT::LogAttachmentsLoading);
+	     binmode (ATTACHF);
+             read(ATTACHF, $Contents, (stat($filetoload))[7],0);
+             close ATTACHF;
+         }else{
+             $r->content_type( 'text/plain' );
+             $is_thumb = undef;
+             $Contents = "File was deleted from RT!\n";
+             $RT::Logger->debug("Attachment (TrID:".$trans.", AtID:".$attach.", Filename:'".$filetoload."') deleted from DB, load from file failed!\n") if ($RT::LogAttachmentsLoading);
+         }
+     }else{
+         $RT::Logger->debug("Loading attachment (TrID:".$trans.", AtID:".$attach.") from DB.\n") if ($RT::LogAttachmentsLoading);
+     }
+     $m->out($Contents);
      $m->abort; 
 </%perl>
 <%attr>
--- ../rt-3.6.4/html/Ticket/Elements/ShowTransactionAttachments	Tue Nov 14 23:53:31 2006
+++ html/Ticket/Elements/ShowTransactionAttachments	Wed Jun  6 12:05:27 2007
@@ -89,7 +89,7 @@
 <div class="messagebody">
 <%perl>
 # {{{   if it has a content-disposition: attachment, don't show inline
-unless ( ($message->GetHeader('Content-Disposition')||"") =~ /attachment/i ) {
+unless ( ($message->GetHeader('Content-Disposition')||"") =~ /attachment/i && $message->ContentType !~ /^image\//i ) {
 
     my $content;
 
@@ -153,11 +1153,24 @@
 
     # if it's an image, show it as an image
     elsif ( $RT::ShowTransactionImages and  $message->ContentType =~ /^image\//i ) {
-        $m->out('<img src="'
-              . $AttachPath . '/'
+        my $mid = $message->Id;
+        $m->out('<SCRIPT TYPE="TEXT/JAVASCRIPT">'."\n"
+              . 'var i'.$mid.'state = 0;'."\n"
+              . 'var i'.$mid.'srcs = new Array(4);'."\n"
+              . 'i'.$mid.'srcs[0] = "'.$AttachPath.'/th'.$Transaction->Id.'/'.$message->Id.'";'."\n"
+              . 'i'.$mid.'srcs[1] = "'.$AttachPath.'/thr'.$Transaction->Id.'/'.$message->Id.'";'."\n"
+              . 'i'.$mid.'srcs[2] = "'.$AttachPath.'/thi'.$Transaction->Id.'/'.$message->Id.'";'."\n"
+              . 'i'.$mid.'srcs[3] = "'.$AttachPath.'/thri'.$Transaction->Id.'/'.$message->Id.'";'."\n"
+              . 'function ri'.$mid.'(){'."\n"
+              . '  i'.$mid.'state++;'."\n"
+              . '  if (i'.$mid.'state == 4){i'.$mid.'state = 0 ;}'."\n"
+              . '  document.images["i'.$mid.'"].src = i'.$mid.'srcs[i'.$mid.'state];'."\n"
+              . '}</SCRIPT>'."\n"
+              . '<img src="'
+              . $AttachPath . '/th'
               . $Transaction->Id . '/'
               . $message->Id
-              . '/" />' );
+              . '/" name=i'.$mid.' onclick=ri'.$mid.'() />' );
     }
     elsif ( $message->ContentLength > 0 ) {
         $m->out(
