Index: imagecache.module
===================================================================
--- imagecache.module	(revision 2)
+++ imagecache.module	(revision 6)
@@ -129,6 +129,48 @@ function imagecache_cache() {
   $path = implode('/', $args); 
   // Verify that the source exists, if not exit. Maybe display a missing image.
   $source = file_create_path($path);
+  
+  
+  // If file wasn't located let's just check to see if this is a freshly submitted file that's still in temp being previewed
+  $preview = FALSE ;
+  if (!is_file($source)) {
+    // for CCK Imagefield uploads in temp
+    if (is_array($_SESSION['imagefield'])){
+      foreach ($_SESSION['imagefield'] as $fieldname=>$fieldimages) {
+      	if (is_array($fieldimages)) {
+          foreach ($fieldimages as $imagedata) {
+            if ($imagedata['preview'] == $path) { 
+			  $source = $imagedata['filepath'];
+			  $preview = TRUE;
+		    }
+          }
+        }
+      }
+    }
+    // for CCK Filefield uploads in temp
+    if (is_array($_SESSION['filefield'])){
+      foreach ($_SESSION['filefield'] as $fieldname=>$fieldimages) {
+      	if (is_array($fieldimages)) {
+          foreach ($fieldimages as $imagedata) {
+            if ($imagedata['preview'] == $path) {
+			  $source = $imagedata['filepath'];
+			  $preview = TRUE;
+			}
+          }
+        }
+	  }
+    }
+    // for Drupal Upload module uploads in temp
+    if (is_array($_SESSION['file_previews'])){
+      foreach ($_SESSION['file_previews'] as $upload=>$filedata) {
+        if ($filedata->_filename == $path) { 
+	      $source = $filedata->filepath; 
+		  $preview = TRUE;
+		}
+      }
+    }
+  }
+
   if (!is_file($source)) {
      return drupal_not_found();
      // @todo: Add a global not found image, and a not found image per preset,
@@ -203,12 +245,23 @@ function imagecache_cache() {
           break;
       }
     }
-    file_move($tmpdestination, $destination);
+	if (!$preview) file_move($tmpdestination, $destination);
   }
   else {
     $generated = TRUE;
   }
 
+  if ($preview)  {
+    if (function_exists('mime_content_type')) {
+      $mime = mime_content_type($tmpdestination);
+    }
+    else {
+      $size = getimagesize($tmpdestination);
+      $mime = $size['mime'];
+    }
+    preview_transfer($tmpdestination, array('Content-Type: '. mime_header_encode($mime), 'Content-Length: '. filesize($tmpdestination)));
+  }
+  
   if ($generated)  {
     if (function_exists('mime_content_type')) {
       $mime = mime_content_type($destination);
@@ -226,6 +279,36 @@ function imagecache_cache() {
 }
 
 /**
+ * Taken from Drupal's implementation of file_transfer.  Added unlink() to remove preview file after delivery.'
+ */
+function preview_transfer($source, $headers) { 
+  ob_end_clean(); 
+
+  foreach ($headers as $header) { 
+    // To prevent HTTP header injection, we delete new lines that are 
+    // not followed by a space or a tab. 
+    // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 
+    $header = preg_replace('/\r?\n(?!\t| )/', '', $header); 
+    drupal_set_header($header); 
+  } 
+
+  $source = file_create_path($source); 
+
+  // Transfer file in 1024 byte chunks to save memory usage. 
+  if ($fd = fopen($source, 'rb')) { 
+    while (!feof($fd)) { 
+      print fread($fd, 1024); 
+    } 
+    fclose($fd); 
+  } 
+  else { 
+    drupal_not_found(); 
+  }
+  unlink($source); 
+  exit(); 
+} 
+
+/**
  * Implementation of hook_field_formatter_info().
  */
 function imagecache_field_formatter_info() {
