Hi Devs and anyone with this problem...

I ran into a problem originally brought up by Worldfallz in http://drupal.org/node/213062 .... I managed to get the filefield cck module to work with flashvideo (5.x-2.7) with a plugin i wrote... I stole some code from your flashvideo.module to inject jobs into the flashvideo table when this module detects a filefield is uploaded..... To use it, install this into your flashvideo/plugins directory as flashvideo_filefield and enable the module.

You'll need to modify a line or two in the main flashvideo.module to help this code out..
On line ~1899, after "_flashvideo_update_files($node);" in the 'update' case section you'll need to add the module_invoke_all....

<?php
case 'update':
            _flashvideo_update_files($node);
            module_invoke_all('flashvideo_update', $node, flashvideo_get_convert_params($node->type));  /***ADDED FOR FILEFIELD PLUGIN ***/
            break;
?>

I needed a callback hook that didn't exist so you'll need to change your flashvideo.module for this to work. Also, ffmpeg needs to have "Output Directories reference from Drupal Root" enabled. And finally goto the 'admin/settings/flashvideo_filefield' and check off the appropriate fields to enable this for your filefield's and nodes.....

I'm still debugging this but I think it might help add functionality to the main flashvideo project. I'm not a drupal expert or pretend to be, so there is likely a better solution to this problem... But please let me know if this helps at all.

-hagan

Comments

haganf’s picture

Looks like i can't attach files, so here is the code a la cut/paste.

<?php

define('FLASHVIDEO_PARAM_PREFIX', "field_flashvideo_");

/**
  *  flashvideo filefield plugin 
  *
  * TODO:  DEBUG.. track down why 'removing' a node doesn't delete flashvideo entries....
  *
  **/

/**
  * Implementation of hook_menu()
  *
  **/
function flashvideo_filefield_menu($may_cache){
   $items = array();
   if ($may_cache) {
      $items[] = array(
         'path' => 'admin/settings/flashvideo_filefield',
         'title' => t('FlashVideo FileField Settings'),
         'description' => t('Administer the FlashVideo FileField Plugin.'),
         'callback' => 'drupal_get_form',
         'callback arguments' => array('flashvideo_settings_filefield'),
         'access' => user_access('administer flashvideo'),
         'type' => MENU_NORMAL_ITEM
      );    
   }
   return $items;
}

/**
  * Define the settings form.
  *
  **/
function flashvideo_settings_filefield(){
    $form['fv_filefield_nodetypes'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Hook Flashvideo to these nodes'),
      '#options' => node_get_types('names'),
      '#default_value' => variable_get('fv_filefield_nodetypes', array()),
      '#description' => t('FileField for this node will hook to FlashVideo'),
      );
    $form['fv_filefield_types'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Hook to these FileFields'),
      '#options' => filefield_get_types(),
      '#default_value' => variable_get('fv_filefield_types', array()),
      '#description' => t('FileField Fields that hook to Flashvideo'),
      );

    /************************************************/
    /* CUSTOM -> for renaming (you may remove)      */
    /* NOTE:  I did this because I cannot override  */
    /* the destination paths but not the filenames. */
    /*  Maybe include a new field to override???    */
    $form['fv_override_name'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Override Filenames?  Check to enable'),
      '#options' => array('rename' =>'Override User Input'),
      '#default_value' => variable_get('fv_override_name', array()),
      '#description' => t('This renames files on the server to: (show)-(year)-(month)-(day)-(nid).EXT'),
      );
    /* The above is optional and site specific to me */
    /*************************************************/
    
    $form['array_filter'] = array('#type' => 'hidden');
    return system_settings_form($form);
}

/**
  * Get all FileFields from the system
  *
  **/
function filefield_get_types(){
    $ret_array = Array();
    $query = db_query("SELECT field_name FROM {node_field} WHERE type ='file'");
    while($field = db_fetch_object($query))
    {
      $ret_array[$field->field_name] = $field->field_name;
    }
    return($ret_array);
}

/**
 * Implementation of hook_help()
 */
function flashvideo_filefield_help($section = 'admin/help#flashvideo_filefield') {
   switch ($section) {
      case 'admin/help#flashvideo_filefield':
         $output = '<h2>Using Filefield node</h2><br/>';
         $output .= '<p>Adding filefield to a cck breaks flashvideo.  This plugin should fix that.';
         
		   return $output;

      case 'admin/settings/modules#description':  
         return t('Allows you to use Filefield nodes with flashvideo.');
   }
}

/**
 * Hook from the FlashVideo module that gets all params to overate.
 */

function flashvideo_filefield_flashvideo_get_params($file, $flags, $params) {
   //print("<br />filefield_flashvideo_get_params FLAG: " . $flags . "<br />");
   if( ($flags > 0) ) {                                                                               // We only want to do this on the pending files.
      $node = node_load($file->nid);                                                                  // We need to load the node to get the CCK variables.
      //printf("The Node: " . sprint_r($node) . "<br />\n");
      if(flashvideo_cck_get_overloaded_params($node, $params)) {                                      // Only if they don't already provide a FLASHVIDEO_USE_CUSTOM flag.
         $params['create_thumb'] = (($flags & FLASHVIDEO_REGEN_THUMB) == FLASHVIDEO_REGEN_THUMB);     // Do we regenerate a thumbnail?
         $params['create_video'] = (($flags & FLASHVIDEO_REGEN_VIDEO) == FLASHVIDEO_REGEN_VIDEO);     // Do we regenerate a video?
      }
   }
   //printf("The params: " . sprint_r($params) . "<br />\n");
   return $params;
}//*/

//This function both updates and calls the submit for this node because we're at an update stage..
function flashvideo_filefield_flashvideo_update($node, $params)
{
    drupal_set_message("FlashVideo <-> Flashvideo Filefield Called");
    /******** Ugly ass hack **********/
    _flashvideo_flashvideo_filefield_update_files($node);  
}

//called by flashvideo module hoock '
/* this is were we bridge the gap between filefield and flashvideo */

function _flashvideo_flashvideo_filefield_update_files($node) {
    $nodetypes = variable_get('fv_filefield_nodetypes', array());
    $filefield_sections = variable_get('fv_filefield_types', array());
    if(in_array($node->type, $nodetypes))
    {
        foreach($filefield_sections as $filefield_col)
        {
            $video_index = 0;
            // Lets process each filetype which we're suppose to encode for...
            if(count($node->{$filefield_col})) 
            {
                
                foreach ($node->{$filefield_col} as $file)
                {
                    //Since the file is probably renamed at this point... we should use 'file id path' not filefield's
                    $file = (object)$file;
                    if($file->remove)
                    {
                        drupal_set_message("Deleting flashvideo/ffmpeg entries...");
                        $files = db_query("SELECT * FROM {files} f LEFT JOIN {flashvideo} fv ON f.fid = fv.fid WHERE fv.oid = %d", $file->fid);
                        db_query("DELETE FROM {ffmpeg_data} WHERE fid = %d", $file->fid);
                        db_query("DELETE FROM {flashvideo} WHERE oid = %d", $file->fid);
            
                        while($oldfile = db_fetch_object($files))
                        {
                            db_query("DELETE FROM {files} WHERE fid = %d", $oldfile->fid);
                            file_delete($oldfile->filepath);
                            module_invoke_all('flashvideo_delete_file', $oldfile);
                        }           
                    }
                    else
                    {
                        $found = db_result(db_query("SELECT count(*) FROM {flashvideo} WHERE fid=%d", $file->fid));  
                        $extension = _flashvideo_get_filetype($file->filepath);
                        if(_flashvideo_get_mime_type($extension, TRUE))
                        {  // Only add if the file is a video and is not already in our flashvideo table.
                            if(!$found)
                            {
                                // Rename the original file.
                                $filepath = substr( $file->filepath, 0, (strrpos($file->filepath, '/') + 1) );
                                /**************************************************************/
                                /* This is optional and site specific to me.. It will rename  */
                                /* the file to (show)-(year)-(month)-(day)-(nid).EXT which    */
                                /* helps me when i have tons of videos to sort through....    */
                                /**************************************************************/
                                $rename_file = $nodetypes = variable_get('fv_override_name', array());
                                $size = flashvideo_get_size($node, $file);
                                if(in_array('rename', $rename_file))
                                {
                                    
                                    $showtmp = taxonomy_node_get_terms($node->nid); 
                                    $showdate = date("Y-m-j", strtotime($node->field_air_date[0]['value']));
                                    foreach($showtmp as $value)
                                    {
                                    	if($value->vid == 1)  //Show == 1 (hardcoded this should change)
	                                    {
	                                        $show = $value->description;
	                                        $showid = array_pop(taxonomy_get_synonyms($value->tid));
                                            break; //only one showname for "Show"
	                                    }
                                    }
                                    $base_name = preg_replace("/[^a-zA-Z0-9_\-\.]/", "_", $show . "-" . $showdate . "_" . $node->nid);
                                }
                                else
                                {
                                    $base_name = preg_replace("/[^a-zA-Z0-9_\.]/", "_", $node->nid . "_" . basename($file->filepath, "." . $extension));
                                }
                                /**************************************************************/
                                /* You may remove the above code or comment it out            */
                                /**************************************************************/
                                //Uncomment below if you remove the above code....
                                //$base_name = preg_replace("/[^a-zA-Z0-9_\.]/", "_", $node->nid . "_" . basename($file->filepath, "." . $extension));
                                $filename = $base_name . "." . $extension;
                                $filepath .= $filename;
                                
                                if(rename( (getcwd() . '/' . $file->filepath),  (getcwd() . '/' . $filepath) ))
                                {
                                    drupal_set_message("Renamed File: " . $filename);
                                    db_query("UPDATE {files} SET filepath='%s',filename='%s' WHERE fid=%d", $filepath, $filename, $file->fid);
                                    db_query("UPDATE {content_type_%s} SET field_file_description='%s' WHERE field_file_fid=%d AND nid=%d", $node->type, $filename, $file->fid, $node->nid);
                                }
                                db_query("INSERT INTO {flashvideo} (fid, nid, oid, status, video_index, width, height, flags) VALUES (%d, %d, %d, %d, %d, %d, %d, %d)", 
                                    $file->fid, $node->nid, $file->fid, FLASHVIDEO_STATUS_OK, $video_index, $size['width'], $size['height'], 3);
                                // If we don't want to wait for cron, call it immediately.
                                if (flashvideo_variable_get($node->type, 'convert', 0))
                                {
                                    flashvideo_cron();
                                }              
                            }
                            $video_index++;
                        }
                    }
                }
            }
        }
    }
}


if (!function_exists('sprint_r'))
{
  function sprint_r($var)
  {
    ob_start();
    print_r($var);
    $ret = ob_get_contents();
    ob_end_clean();
    return $ret;
  }
}

?>
travist’s picture

This is great stuff! I will look into this for a future release.

Thanks,

Travis.

haganf’s picture

Hey Travis, No problem... Great module and I'm glad I could help a little. One other thing, I'm thinking of making another plugin to add 'flvtool2' as an encoding option.... I find it really helps if you run that on flv files after ffmpeg encodes them. If you've already planed this i'll skip it.

-hagan

Delta Bridges’s picture

This is wonderful... there is only one thing I can say: long live the Drupal community!
Travis, do you have an idea when you might inplement this useful feature?
many many thanks!
Expat9

zmove’s picture

Hi,

Is there a way to see that for Drupal 6 version ?

attheshow’s picture

Version: 5.x-2.7 » 6.x-1.5-beta2
Assigned: Unassigned » attheshow
Status: Active » Fixed

FileField integration has been added in the Drupal 6 version of the module. This functionality will not be backported to Drupal 5.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Pirch’s picture

The story continues there :D

http://drupal.org/node/501834