? .video.module.swp
? patches
Index: video.module
===================================================================
--- video.module.orig	2007-06-04 09:40:31.000000000 -0700
+++ video.module	2007-06-10 07:03:10.000000000 -0700
@@ -109,7 +109,7 @@
           $menu_type = MENU_LOCAL_TASK;
         } 
         else {
-          $menu_type = MENU_CALLBACK;	     	
+          $menu_type = MENU_CALLBACK;
         }
         $items[] = array('path' => 'node/'. arg(1) .'/play',
           'title' => t('play'),
@@ -199,7 +199,7 @@
           );
         
       }
-	}
+    }
     if ($display_download_link == 1) {
       $link['video_download'] = array(
         'title' => t('download'),
@@ -626,7 +626,7 @@
     //If file is on the local server get size, otherwise get size from function.
     $path = getcwd() . '/' . $node->vidfile; //Local path to video file.
     $path_for_size = $path; // file_check_file will make $path unusable for filesize()
-    if (file_check_path($path) && is_file($path_for_size)) { //If file exists locally set size.
+    if (is_file($path_for_size) && file_check_path($path)) { //If file exists locally set size.
       $node->size = filesize($path_for_size);
     }
     else {
@@ -660,13 +660,11 @@
     if (isset($node->videoy) && $node->videoy <= 0) {
       form_set_error('videoy', t('You have to insert a valid vertical pixel size for this video'));
     }
+    //Make sure file size is valid.
+    if ((!isset($node->size) || !is_numeric($node->size) || $node->size < 0) && !isset($node->video_fid)) { //If the file is not local or not a valid number then set error.
+      form_set_error('size', t('You have to insert a valid file size for this video.'));
+    }
   }
-  //Make sure file size is valid.
-  $path = getcwd() . '/' . $node->vidfile; //Local path to video file.
-  if ((!isset($node->size) || !is_numeric($node->size) || $node->size < 0) && !$_SESSION['video_upload_file']) { //If the file is not local or not a valid number then set error. $_SESSION check needed for video_upload functionality
-    form_set_error('size', t('You have to insert a valid file size for this video.'));
-  }
-  
   //Makes sure the total playtime is greater than 0.
   $time = $node->playtime_seconds + $node->playtime_minutes + $node->playtime_hours;
   if ((isset($node->playtime_minutes) and isset($node->playtime_hours) and isset($node->playtime_seconds)) and $time == 0) {
@@ -713,19 +711,19 @@
   // if we are viewing the page, run the body through the theme
   if ($page) {
     $output = '';
-	if (variable_get('video_playinbody', 0)) {
-	  if (user_access('play video')) {
+    if (variable_get('video_playinbody', 0)) {
+      if (user_access('play video')) {
         $output .= theme('video_player', $node);
       }
-	  else {
-	    $output .= l(t('login'), "user/login", array('class' => 'outgoing', 'title' => t('login to your account')));
+      else {
+        $output .= l(t('login'), "user/login", array('class' => 'outgoing', 'title' => t('login to your account')));
         $output .= ' ' . t('or') . ' ';
         $output .= l(t('register'), "user/register", array('class' => 'outgoing', 'title' => t('create a new account')));
         $output .= t(' to play video');
       }
-	}
+    }
     $output .= theme('video_view', $node, $teaser, $page);
-	$node->content['body'] = array('#value' => $output);
+    $node->content['body'] = array('#value' => $output);
   }
 
   return $node;
@@ -882,7 +880,7 @@
 function video_play() {
   if ($node = node_load(arg(1))) {
     drupal_set_title(t('Playing') . ' ' . theme('placeholder', $node->title));
-	$output = theme('video_player', $node);
+    $output = theme('video_player', $node);
     if($output == ''){
       drupal_goto("node/$node->nid");
     }
@@ -1127,12 +1125,12 @@
   $output = '<!-- [if IE] -->
 <object classid="clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616" width="'.$node->videox.'" height="'.$height.'" codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab">
 <!--> <![endif]-->'. "\n";
-	// this will be executed by not Internet Explorer browsers
+  // this will be executed by not Internet Explorer browsers
   $output = '<!-- [if !IE] --> 
 <object type="video/divx" data="'.$url.'" width="'.$node->videox.'" height="'.$height.'" mode="zero"> 
 <!--> <![endif]-->'."\n";
-  	
- 	$output .= '<param name="src" value="'.$url.'"/>'."\n";
+
+  $output .= '<param name="src" value="'.$url.'"/>'."\n";
   $output .= '<param name="mode" value="zero"/>'."\n";
   $output .= '</object>';
   $output = _theme_video_format_play($output,t('http://www.divx.com/divx/webplayer/'),
Index: plugins/video_ffmpeg_helper/video_ffmpeg_helper.module
===================================================================
--- plugins/video_ffmpeg_helper/video_ffmpeg_helper.module.orig	2007-06-04 09:40:31.000000000 -0700
+++ plugins/video_ffmpeg_helper/video_ffmpeg_helper.module	2007-06-10 06:42:44.000000000 -0700
@@ -1,5 +1,6 @@
 <?php
 // $Id: video_ffmpeg_helper.module,v 1.7 2007/02/18 13:31:29 fax8 Exp $
+/* ex: set syntax=php tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
 
 /**
  * @file
@@ -61,10 +62,10 @@
     if (!is_numeric($form_values['video_ffmpeg_helper_auto_thumbnail_seek'])) {
       form_set_error('video_ffmpeg_helper_auto_thumbnail_seek', t('Seek time must be an integer'));
     }
-	$options = $form_values['video_ffmpeg_helper_thumbnailer_options'];
-	if (!strstr($options, '%videofile') || !strstr($options, '%thumbfile')) {
-	  form_set_error('video_ffmpeg_helper_thumbnailer_options', t('Thumbnail options must contain mandatory arguments %videofile and %thumbfile'));
-	}
+    $options = $form_values['video_ffmpeg_helper_thumbnailer_options'];
+    if (!strstr($options, '%videofile') || !strstr($options, '%thumbfile')) {
+      form_set_error('video_ffmpeg_helper_thumbnailer_options', t('Thumbnail options must contain mandatory arguments %videofile and %thumbfile'));
+    }
   }
 }
 
@@ -282,7 +283,8 @@
  * Add a video conversion rendering process to the queue
 */
 function _video_ffmpeg_helper_add_rendering(&$node) {
-  $file = $_SESSION['video_upload_file_stored']->filepath;
+  $file = _video_upload_fetch_file($node->video_fid);
+  $file = $file->filepath;
   //print_r($node); die;
   db_query('INSERT INTO {video_rendering} (vid, nid, origfile, pid, status, started, completed) VALUES (%d, %d, "%s", %d, %d, %d, %d)', $node->vid, $node->nid, $file, 0, VIDEO_RENDERING_PENDING, 0, 0);
   
@@ -300,12 +302,11 @@
 function _video_ffmpeg_helper_get_video_info(&$node) {
 
   // escape file name for safety
-  $file = escapeshellarg($_SESSION['video_upload_file']->filepath);
+  $file = _video_upload_load_file($node->video_fid);
+  $file = escapeshellarg($file->filepath);
   // create the full command to execute
   $command = variable_get('video_ffmpeg_helper_ffmpeg_path', '/usr/bin/ffmpeg') . ' -i ' . $file;
   
-  drupal_set_message('executing' . $command);
-  
   //execute the command
   ob_start();
   passthru($command." 2>&1", $command_return);
@@ -361,18 +362,20 @@
  *   a drupal file object
  */
 function _video_ffmpeg_helper_auto_thumbnail(&$node) {
-  if(empty($_SESSION['video_upload_file']) ||
-      !$_SESSION['video_upload_file']->newfile ||
-      $node->iid || $_SESSION['video_upload_file']->iid ||
-      $_SESSION['video_upload_file']->thumbnailed) {
-    if (variable_get('video_image_auto_thumbnail_debug', false)) {
-      if (empty($_SESSION['video_upload_file']))
+  $debug = variable_get('video_ffmpeg_helper_auto_thumbnail_debug', false);
+  if(empty($node->video_upload_file->filepath) ||
+      $node->thumbnailed || !$node->new_vidfile) {
+    if ($debug) {
+      if (empty($node->video_upload_file->filepath))
         drupal_set_message(t('no video has been uploaded: make sure that video_image weight is greater than video_upload weight; make sure that the video file is not too large to be uploaded.'));
+      else if ($node->thumbnailed)
+        drupal_set_message(t('not thumbnailing: video has already been thumbnailed'));
+      else if (!$node->new_vidfile)
+        drupal_set_message(t('not thumbnailing: video file was not newly uploaded'));
     }
     return null;
   }
-  $debug = variable_get('video_image_auto_thumbnail_debug', false);
-  $videofile = escapeshellarg($_SESSION['video_upload_file']->filepath);
+  $videofile = escapeshellarg($node->video_upload_file->filepath);
   $thumbfile = tempnam(file_directory_temp(), 'tnail-thumb');
   $seek = variable_get('video_image_auto_thumbnail_seek', 2);
   $tnail = variable_get('video_ffmpeg_helper_ffmpeg_path', '/usr/bin/ffmpeg');
@@ -392,13 +395,13 @@
     drupal_set_message(t('video_image_auto_thumbnail: file %file does not exist', array('%file' => $thumbfile)), 'error');
   }
   $file = array(
-      'filename' => $_SESSION['video_upload_file']->filename . ".video-thumb.jpg",
+      'filename' => basename($node->video_upload_file->filepath) . ".video-thumb.jpg",
       'filemime' => 'image/jpeg',
       'filesize' => filesize($thumbfile),
       'filepath' => $thumbfile,
       'nid' => $node->nid,
       );
-  $_SESSION['video_upload_file']->thumbnailed = TRUE;
+  $node->thumbnailed = true;
   if ($debug) {
     if ($tnail_return) {
       drupal_set_message(t('Failed to thumbnail video'));
Index: plugins/video_image/video_image.module
===================================================================
--- plugins/video_image/video_image.module.orig	2007-06-04 09:40:31.000000000 -0700
+++ plugins/video_image/video_image.module	2007-06-04 09:42:14.000000000 -0700
@@ -1,5 +1,6 @@
 <?php
 // $Id: video_image.module,v 1.14 2007/02/18 14:45:47 fax8 Exp $
+/* ex: set tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
 
 /**
  * @file
@@ -43,12 +44,6 @@
  * Settings form
  */
 function video_image_admin_settings() {
-  if (module_exists('video_upload')) {
-    if (variable_get('video_image_auto_thumbnail', 0)) {
-      $upload_weight = db_result(db_query("SELECT weight FROM {system} WHERE name='video_upload'"));
-      db_query("UPDATE {system} SET weight=".($upload_weight+1)." WHERE name='video_image'");
-    }
-  }
   $form = array();
   $form['video_image_publish_thumbnail'] = array(
       '#type' => 'checkbox',
@@ -72,95 +67,140 @@
 
   if($form_id == 'video_node_form') {
 
+    _video_image_check_settings();
+
     $node = $form['#node'];
     $value = ($node->new_image) ? '#value' : '#default_value';
     $form['iid'] = array('#type' => 'hidden', $value => $node->iid);
+    if (!is_array($node->tempimage['fids'])) {
+      $fids = array('_original' => 0);
+      foreach (_image_get_sizes() as $size) {
+        $fids[$size['label']] = 0;
+      }
+      $node->tempimage['fids'] = $fids;
+    }
+    $form['tempimage']['#tree'] = true;
+    foreach ($node->tempimage['fids'] as $label => $fid) {
+      $form['tempimage']['fids'][$label] = array('#type' => 'hidden', $value => $fid);
+    }
 
     if (function_exists('_image_check_settings')) {
       _image_check_settings();
       $form['#attributes'] = array("enctype" => "multipart/form-data");
 
-     
       $form['image'] = array('#type' => 'fieldset', '#title' => t('Image thumbnails'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -17, '#description' => t('Use this form to upload an image.'));
-      
+
       $form['image']['image'] = array('#type' => 'file', '#title' => t('Image'));
-      $form['image']['image_title'] = array('#type' => 'textfield', '#title' => t('Image title'), '#default_value' => $node->image->image_title);
     }
   }
-
 }
 
-
 /**
  * Implementation of hook_nodeapi()
  */
-function video_image_nodeapi(&$node, $op, $teaser) {
+function video_image_nodeapi(&$node, $op, $teaser=false) {
   if($node->type == 'video') {
     switch ($op) {
       case 'load': 
         $output['iid'] = $node->serial_data['iid'];
         return $output;
-      case 'submit':
-        if (variable_get('video_image_auto_thumbnail_debug', false) && variable_get('video_image_auto_thumbnail', false)) {
-          drupal_set_message(t('video_image_nodeapi: prepare: ready to thumbnail image'));
-        }
+      case 'prepare':
         $field_name = file_check_upload('image');
-        $image->uid = $node->uid;
-        $image->name = $node->name;
-        $image->created = $node->created;
-        $image->type = 'image';
-        //$image->status = _video_image_publish_thumbnails();
-        //$image->promote = _video_image_promote_thumbnails();
-        if (!$field_name && module_exists('video_ffmpeg_helper') && variable_get('video_ffmpeg_helper_auto_thumbnail', false)) {
-          $image->title = $_SESSION['video_upload_file']->filename;
+        if (!$field_name && variable_get('video_ffmpeg_helper_auto_thumbnail', false) && $node->new_vidfile) {
+          if (variable_get('video_ffmpeg_helper_auto_thumbnail_debug', false)) {
+            drupal_set_message(t('video_image_nodeapi: prepare: ready to thumbnail video'));
+          }
           $field_name = _video_ffmpeg_helper_auto_thumbnail($node);
         }
-        else {
-          $image->title = $_POST['edit']['image_title'];
-          $field_name = 'image';
+        if ($field_name) {
+          $node->tempimage = _video_image_temp_image_store($field_name);
+          $node->new_image = TRUE;
         }
-        image_prepare($image, $field_name);
-        if ($image->images) {
-          node_validate($image);
-          if (!form_get_errors()) {
-            $image = node_submit($image);
+        break;
+
+      case 'submit':
+        if (is_array($node->tempimage['fids'])) {
+          $image = _video_image_temp_image_load(array_values($node->tempimage['fids']));
+          db_query("DELETE FROM {files} WHERE fid in (%s)",
+            implode(',', array_values($node->tempimage['fids'])));
+          $image->uid = $node->uid;
+          $image->created = time();
+          $image->title = t('Video thumbnail for !title', array('!title' => $node->title));
+          $image = node_submit($image);
+          $image->uid = $node->uid;
+          $image->status = _video_image_publish_thumbnails();
+          $image->promote = _video_image_promote_thumbnails();
+          // This is a messages hack (we don't want to see what happens under the covers)
+          _video_image_pause_messages(true);
+          if ($node->iid) {
+            $oldimage = node_load($node->iid);
+            $oldimage->images = $image->images;
+            $oldimage->new_image = 1;
+            node_save($oldimage);
+            $node->iid = $oldimage->nid;
+          } else {
             node_save($image);
-            
-            // needed to set the correct status and promote values if the user does not have enought permissions. Is there a better solution???
-            db_query('UPDATE {node} SET status = %d, promote = %d WHERE nid = %d AND vid = %d', _video_image_publish_thumbnails(), _video_image_promote_thumbnails(), $image->nid, $image->vid); 
-            
-            $node->iid = $node->serial_data['iid'] = $image->nid;
-            $_SESSION['video_upload_file']->iid = $image->nid;
-            $node->new_image = TRUE;
+            $node->iid = $image->nid;
           }
+          _video_image_pause_messages();
         }
-        else if (isset($_SESSION['video_upload_file']->iid)) {
-          $node->iid = $_SESSION['video_upload_file']->iid;
-        }
-      
+
+        // needed to set the correct status and promote values if the user does not have enought permissions. Is there a better solution???
+        //db_query('UPDATE {node} SET status = %d, promote = %d WHERE nid = %d AND vid = %d', _video_image_publish_thumbnails(), _video_image_promote_thumbnails(), $image->nid, $image->vid);
+
         $node->serial_data['iid'] = $node->iid;
-      break;
-      case 'prepare':
-        ;
-      break;
+        break;
 
       case 'view':
-        if ($node->iid) {
-          if($teaser) {
-            $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_teaser', $node));
-          }
-          else {
-            $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_body', $node));
-          }
+        if (is_array($node->tempimage['fids']) && $node->tempimage['fids']['_original']) {
+          $image = _video_image_temp_image_load(array_values($node->tempimage['fids']));
+          $image->nid = $node->nid;
+        } else if ($node->iid) {
+          $image = node_load($node->iid);
+          $image->nid = $node->nid;
+        } else {
+          $image = $node;
+        }
+        if($teaser) {
+          $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_teaser', $image));
+        }
+        else {
+          $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_body', $image));
         }
         break;
       case 'delete':
-        node_delete(array('nid' => $node->iid));
+        _video_image_pause_messages(true);
+        node_delete($node->iid);
+        _video_image_pause_messages();
         break;
     }
   }
 }
 
+function video_image_node_operations() {
+  $operations = array(
+    'rebuild_video_thumbs' => array(
+      'label' => t('Rebuild video thumbnails'),
+      'callback' => 'video_image_operations_rebuild',
+    ),
+  );
+  return $operations;
+}
+
+function video_image_operations_rebuild($nids) {
+  foreach ($nids as $nid) {
+    if ($node = node_load($nid)) {
+      if ($node->type == 'video') {
+        drupal_set_message(t("Rebuilding %node-title's thumbnail.", array('%node-title' => $node->title)));
+        $node->new_vidfile = 1;
+        video_image_nodeapi($node, 'prepare');
+        video_image_nodeapi($node, 'submit');
+        node_save($node);
+      }
+    }
+  }
+}
+
 
 /**
  * Render the output for the node teaser.
@@ -172,9 +212,8 @@
  *   string of content to display
  */
 function theme_video_image_teaser($node) {
-  if($node->serial_data['iid']) {
-    $image = node_load($node->serial_data['iid']);
-    $image = image_display($image, 'thumbnail', array('class' => 'video_image_teaser'));
+  if($node->type == 'image') {
+    $image = image_display($node, 'thumbnail', array('class' => 'video_image_teaser'));
   }
   else { // only for backward compatibility
     $image = theme('image', $node->serial_data['image_teaser'], $node->title, $node->title, array('class' => 'video_image_teaser'), FALSE);
@@ -196,9 +235,8 @@
  */
 function theme_video_image_body($node) {
   if(variable_get('video_playinbody', 0) == 0){
-    if($node->serial_data['iid']) {
-      $image = node_load($node->serial_data['iid']);
-      $image = image_display($image, 'thumbnail');
+    if($node->type == 'image') {
+      $image = image_display($node, 'thumbnail');
     }
     else { // only for backward compatibility
       $image = theme('image', $node->serial_data['image_view'], $node->title, $node->title, array('class' => 'video_image_view'), FALSE); //Create image HTML
@@ -209,6 +247,51 @@
 }
 
 
+/* Create a fake node object that acts like an image node
+ * and insert each of the image derivatives into the files
+ * table so we can keep track of them properly
+ */
+function _video_image_temp_image_store(&$file) {
+  $image = new stdClass();
+  $image->type = 'image';
+  $image->uid = 1;
+  $image->created = time();
+  $image->title = t('video image thumbnail');
+  image_prepare($image, $file);
+  if ($image->images) {
+    node_validate($image);
+    if (!form_get_errors()) {
+      // save the images in the files table
+      foreach ($image->images as $l => $f) {
+        $info = image_get_info($f);
+        $fid = db_next_id('{files}_fid');
+        db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')",
+            $fid, 1, "video_image_temp.$l", $f, $info['mime_type'], $info['file_size']);
+        $image->fids[$l] = $fid;
+      }
+    }
+  }
+  return (array)$image;
+}
+
+/* Create a fake node object that acts like an image node
+ * by looking up each file in the array $fids and loading
+ * them into the images array.
+ */
+function _video_image_temp_image_load($fids) {
+  $image = new stdClass();
+  $image->type = 'image';
+  $image->new_file = 1;
+  $fids = implode(',', $fids);
+  $results = db_query("SELECT fid, filename, filepath FROM {files} WHERE fid IN (%s)", $fids);
+  while ($file = db_fetch_object($results)) {
+    $label = substr($file->filename, 17);
+    $image->images[$label] = $file->filepath;
+    $iamge->fids[$label] = $file->fid;
+  }
+  return $image;
+}
+
 /* If the user has set a promote preference, use that, otherwise return
  * if 'promote' is set in the drupal content type settings
  *
@@ -238,4 +321,35 @@
   }
   return $settings_override;
 }
+
+/* The video_image module requires to have a heavier weight than the video_upload
+ * module (if it is enabled).  Otherwise the video will never get thumbnailed.
+ * We check for that at video_form time so we know it will work for the following
+ * video creation.
+ */
+function _video_image_check_settings() {
+  if (module_exists('video_upload') &&
+     variable_get('video_ffmpeg_helper_auto_thumbnail', 0)) {
+    $upload_weight = db_result(db_query("SELECT weight FROM {system} WHERE name='video_upload'"));
+    db_query("UPDATE {system} SET weight=".($upload_weight+1)." WHERE name='video_image'");
+  }
+}
+
+/* This function is a kludge, but I really think it is needed
+ * in order to help .-~smooth~-. out the end-user's experience
+ * The idea is to snapshot the drupal messages so anything
+ * that happens in the meantime doesn't show up
+ * For example, when the video image thumbnail node gets deleted
+ * the user might get worried, since he never created it
+ */
+function _video_image_pause_messages($snapshot = false) {
+  static $messages = null;
+  if ($snapshot) {
+    $messages = drupal_get_messages();
+  } else if (is_array($messages)) {
+    $_SESSION['messages'] = $messages;
+    $messages = null;
+  }
+}
+
 ?>
Index: plugins/video_upload/video_upload.module
===================================================================
--- plugins/video_upload/video_upload.module.orig	2007-06-04 09:40:31.000000000 -0700
+++ plugins/video_upload/video_upload.module	2007-06-10 07:02:38.000000000 -0700
@@ -1,5 +1,6 @@
 <?php
 // $Id: video_upload.module,v 1.16 2007/01/14 17:30:40 fax8 Exp $
+/* ex: set syntax=php tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
 
 /**
  * @file
@@ -71,25 +72,33 @@
   return array('upload video files');
 }
 
+/**
+ * Implementation of hook_cron()
+ */
+function video_upload_cron() {
+  /* look for crusty files */
+  $temppath = file_directory_temp() . '/video/';
+  $files = file_scan_directory(file_create_path($temppath), '.*');
+  foreach ($files as $file => $info) {
+    if (time() - filemtime($file) > 60*60*6) {
+      db_query("DELETE FROM {files} WHERE filename LIKE 'video_upload_temp.%' AND nid = 1 AND filepath = '%s'", $file);
+      file_delete($file);
+    }
+  }
+}
 
 /**
  * Implementation of hook_nodeapi()
  */
-function video_upload_nodeapi(&$node, $op, $teaser) {
+function video_upload_nodeapi(&$node, $op, $arg=null) {
   if($node->type == 'video') {
     switch ($op) {
   
       case 'load':
-        $output['video_upload_file'] = _video_upload_load($node);
-        if($node->vidfile == '') { // we will disable uploaded file if a path is already live
-          $output['vidfile'] = file_create_url($output['video_upload_file']->filepath);
-          // set the filesize - this seems not to work.. why???
-          $output['size'] = $output['video_upload_file']->filesize;
-        }
-        return $output;
+        return _video_upload_load($node);
       
       case 'prepare':
-        _video_upload_prepare($node);
+        _video_upload_prepare($node, $arg);
         break;
   
       case 'validate':
@@ -105,10 +114,7 @@
         _video_upload_store($node);
         break;
       case 'update':
-        // is there a better way ???
-        $node->video_upload_file = _video_upload_load($node);
-        _video_upload_delete($node);
-        _video_upload_store($node);
+        _video_upload_update($node);
         break;
   
       case 'delete':
@@ -152,7 +158,10 @@
     }
     else {
       // vidfile field is no more required while upload is enabled.
-      $form['video']['vidfile']['#required'] = FALSE;
+      if ($node->video_fid) {
+        $form['video']['vidfile']['#required'] = FALSE;
+        $form['video']['vidfile']['#default_value'] = '';
+      }
 
       $form['video']['vidfile']['#description'] .= '<p>' . t('If you want to upload a video simply ignore this field and select your video file at the "Upload video file" field.') . '</p>';
       $form['video']['video_upload'] = array(
@@ -160,85 +169,97 @@
         '#title' => t('Upload video'),
         '#weight' => -19,
         '#collapsible' => TRUE,
-        '#collapsed' => (isset($node->video_upload_file) ? TRUE : FALSE ),
       );
 
       $form['video']['video_upload'] += _video_upload_form($node);
+      $form['video']['video_upload']['#collapsed'] = !isset($_SESSION['video_upload_file_info']);
     }
   }
 }
 
+function _video_upload_load_file($fid) {
+  static $files = array();
 
-function _video_upload_load(&$node) {
-  if ($node->vid) {
-    $result = db_query('SELECT * FROM {files} f INNER JOIN {file_revisions} r ON f.fid = r.fid WHERE r.vid = %d ORDER BY f.fid DESC', $node->vid);
-    //while($item =  db_fetch_object($result)) {
-    //  print_r($item); 
-    //}
-    return db_fetch_object($result);
+  if (!$fid) {
+    return null;
+  }
+  if (!isset($files[$fid])) {
+    $files[$fid] = db_fetch_object(db_query('SELECT * from {files} WHERE fid = %d', $fid));
+    if (strstr($files[$fid]->filename, 'video_upload_temp.')) {
+      $files[$fid]->filename = substr($files[$fid]->filename, strlen('video_upload_temp.'));
+    }
   }
+  return $files[$fid];
 }
 
-
 /**
  * Validate video file
  */
 function _video_upload_validate(&$node) {
-  // if we override the default video module vidfile field and we don't have a file uploaded set error
-  if (user_access('upload video files') && variable_get('video_upload_override_vidfile', false) && !isset($node->video_upload_file) && !isset($_SESSION['video_upload_file'])) {
-    form_set_error('video_upload_file', t('A file must be provided.'));
-    return;
-  }
 }
 
 
 function _video_upload_submit(&$node) {
-  ;
-}
-
-
-function _video_upload_prepare(&$node) {
-  // clear video file informations
-  if(count($_POST) == 0) {
-    if (!empty($_SESSION['video_upload_file'])) {
-      file_delete($_SESSION['video_upload_file']->filepath);
+  $file = _video_upload_load_file($node->video_fid);
+  if ($node->vidfile && $node->video_fid) {
+    $data = unserialize($node->serialized_data);
+    if (strstr($file->filepath, file_directory_temp() . '/video/')) {
+      $node->vidfile = '';
+      $node->new_vidfile = 1;
+    } else {
+      $node->video_fid = 0;
     }
-    unset($_SESSION['video_upload_file']);
   }
+  $node->serial_data['video_fid'] = $node->video_fid;
+  return;
+  $video_upload_file = _video_upload_load_file($node->video_fid);
+  if ($video_upload_file && $node->video_fid != $video_upload_file->fid) {
+    // reset vidfile: the real value gets loaded at nodeapi(load) time
+    $node->vidfile = '';
+  }
+  $node->serial_data['video_fid'] = $node->video_fid;
+}
 
+function _video_upload_load(&$node) {
+  $output = array();
+  $output['video_fid'] = $node->video_fid = $node->serial_data['video_fid'];
+  $file = _video_upload_load_file($node->video_fid);
+  $output['video_upload_file'] = $file;
+  if ($node->vidfile == '') { // we will disable uploaded file if a path is already live
+    $node->vidfile = file_create_url($file->filepath);
+    // set the filesize - this seems not to work.. why???
+    $node->size = $file->filesize;
+  }
+  return $output;
+}
 
-  if ($file = file_check_upload('video_upload_file')) {
+function _video_upload_prepare(&$node, $field_name=null) {
+  if (is_null($field_name)) {
+    $field_name = 'video_upload_file';
+  }
+  if ($file = file_check_upload($field_name)) {
     $temppath = file_directory_temp() . '/video/';
     file_check_directory($temppath, TRUE);
-    $node->video_upload_file = file_save_upload($file, $temppath .'/'. $file->filename, FILE_EXISTS_REPLACE);
-    $node->video_upload_file->newfile = TRUE;
+    $node->video_upload_file = $file = file_save_upload($file, $temppath .'/'. $file->filename, FILE_EXISTS_REPLACE);
+    $node->new_vidfile = true;
+    $file->fid = db_next_id('{files}_fid');
+    db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 1, 'video_upload_temp.'.$file->filename, $file->filepath, $file->filemime, $file->filesize);
+    $node->video_fid = $file->fid;
     
     // set video size
     $node->size = $node->video_upload_file->filesize;
-
-    $_SESSION['video_upload_file'] = $node->video_upload_file;
-  }
-  else if (!empty($_SESSION['video_upload_file'])) {
-    $node->video_upload_file = $_SESSION['video_upload_file'];
-    // set video size
-    $node->size = $node->video_upload_file->filesize;
-
-  } else {
-    $_SESSION['video_upload_file'] = $node->video_upload_file;
   }
 }
 
 
 function _video_upload_store(&$node) {
-  if(!empty($_SESSION['video_upload_file'])) {
-    $file = $_SESSION['video_upload_file'];
+  if ($node->video_fid) {
+    $file = _video_upload_load_file($node->video_fid);
     $dest_dir = variable_get('video_upload_default_path', 'videos') .'/';
-    if ($file = file_save_upload($file, $dest_dir . $file->filename)) {
-      $file->fid = db_next_id('{files}_fid');
-      db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
+    if (file_move($file, $dest_dir . $file->filename)) {
+      db_query("UPDATE {files} SET nid = %d, filename = '%s', filepath = '%s', filemime = '%s', filesize = %d WHERE fid = %d", $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->fid);
       db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, $file->list, $file->description);
-      $_SESSION['video_upload_file_stored'] = $file;
-      unset($_SESSION['video_upload_file']);
+      $node->video_upload_file_stored = $file;
     }
     else {
       drupal_set_message(t('An error occurred during file saving. Your video file has not been stored.'), 'error');
@@ -246,6 +267,13 @@
   }
 }
 
+function _video_upload_update(&$node) {
+  $data = unserialize($node->serialized_data);
+  if ($data['video_fid'] != $node->video_fid) {
+    $node->video_upload_file = _video_upload_load_file($node->video_fid);
+    _video_upload_store($node);
+  }
+}
 
 /**
  * Delete files associated to this video node
@@ -264,44 +292,61 @@
 /**
 * Create video upload specific form fields
 */
-function _video_upload_form($node) {
+function _video_upload_form(&$node) {
   _video_upload_check_settings();
 
+  if (count($_POST) == 0) {
+    unset($_SESSION['video_upload_file_info']);
+  }
+
   $form = array();
 
+  if ($node->new_vidfile) {
+    $form['video_fid'] = array('#type' => 'hidden', '#value' => $node->video_fid);
+    $_SESSION['video_upload_file_info'] = $node->video_upload_file;
+  }
+  else {
+    if ($node->video_fid) {
+      $_SESSION['video_upload_file_info'] = $node->video_upload_file;
+    }
+    $form['video_fid'] = array('#type' => 'hidden', '#default_value' => $node->video_fid);
+  }
+
   $form['video_upload_file'] = array(
     '#type' => 'file',
-    '#title' => t('Upload video file'),
+    '#title' => $_SESSION['video_upload_file_info']?t('Replace current video file'):t('Upload video file'),
     '#size' => 40,
-    '#weight' => -19,
+    '#weight' => -9,
     '#description' => t('The uploaded file will be used as video file for this node.<br /><b>NOTE:</b> The max upload size is') . ' ' . format_size(_video_upload_get_max_upload_size()) . '.',
   );
-
-  if (isset($node->video_upload_file)) {
-    $form['video_upload_file']['#prefix'] = theme('video_upload_file_info_form', $node);
-    $form['video_upload_file']['#title'] = t('Replace with');
+  if (isset($_SESSION['video_upload_file_info'])) {
+    $form['video_upload_file_info']['#after_build'][] = 'video_upload_add_info';
   }
 
   return $form;
 }
 
+function video_upload_add_info($form_id, $edit) {
+  if ($_SESSION['video_upload_file_info']) {
+    $node = (object)($edit);
+    $node->video_upload_file = $_SESSION['video_upload_file_info'];
+    $form = array('#type' => 'item', '#value' => theme('video_upload_file_info_form', $node), '#weight' => -10);
+  }
+  return $form;
+}
 
 /**
  * Display informations about already uploaded file
  */
 function theme_video_upload_file_info_form(&$node) {
-  $output = '<p>' . t('A video file has been already uploaded.') . '</p>';
-
   // create array containing uploaded file informations
   $items = array(
   '<b>'. t('file name') .':</b> ' . $node->video_upload_file->filename,
-  '<b>'. t('file path') .':</b> ' . $node->video_upload_file->filepath,
   '<b>'. t('file size') .':</b> ' . format_size($node->video_upload_file->filesize),
-  '<b>'. t('file mime') .':</b> ' . $node->video_upload_file->filemime,
   );
 
   // create information list
-  $output .= theme_item_list($items, t('uploaded video informations:'));
+  $output .= theme_item_list($items, t('uploaded video information:'));
 
   return $output;
 }
@@ -370,7 +415,7 @@
 function theme_video_upload_busy() {
   return '<div id="sending" style="display: none;">
 				 <h3>' . t('Sending video... please wait.') . '</h3>
-						<img src="'. drupal_get_path('module', 'video_upload') . '/busy.gif" alt="' . t('Sending video... please wait.') . '"/>
+						<img src="'. base_path() . drupal_get_path('module', 'video_upload') . '/busy.gif'.'" alt="' . t('Sending video... please wait.') . '"/>
             <p>'. t('Video upload could take some minutes.') . '<br /><a href="#" id="video_upload_cancel_link">abort upload.</a></p>
 						</div>';
 }
Index: plugins/video_upload/video_upload.install
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ plugins/video_upload/video_upload.install	2007-06-04 11:05:50.000000000 -0700
@@ -0,0 +1,31 @@
+<?php
+/* $Id: $ */
+/* ex: set syntax=php tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
+
+function video_upload_install() {
+}
+
+function _video_upload_update_1_load_file($node) {
+  if ($node->vid) {
+    $result = db_query('SELECT * FROM {files} f INNER JOIN {file_revisions} r ON f.fid = r.fid WHERE r.vid = %d ORDER BY f.fid DESC', $node->vid);
+    return db_fetch_object($result);
+  }
+}
+
+/* added video_fid to serial data
+ * update the video nodes to reflect this
+ */
+function video_upload_update_1() {
+  $results = db_query("SELECT n.nid,n.vid,serialized_data FROM {node} n JOIN {video} v on n.vid = v.vid WHERE n.type = 'video'");
+  while ($video = db_fetch_object($results)) {
+    $video->serial_data = unserialize($video->serialized_data);
+    $file = _video_upload_update_1_load_file($video);
+    $video->serial_data['video_fid'] = $file->fid;
+    $video->serialized_data = serialize($video->serial_data);
+    $r = db_query("UPDATE {video} SET serialized_data = '%s' WHERE vid = %d",
+        $video->serialized_data, $video->vid);
+  }
+  return array(array('success' => 1, 'query' => t('added video_fid to serialized data column')));
+}
+
+?>
