You are here

function flashnode_prepare in Flash Node 5.6

Same name and namespace in other branches
  1. 5.2 flashnode.module \flashnode_prepare()
  2. 5.3 flashnode.module \flashnode_prepare()

Implementation of hook_prepare().

File

./flashnode.module, line 145

Code

function flashnode_prepare(&$node) {
  $field_name = 'flashfile';

  // check if the upload is valid
  if ($file = file_check_upload($field_name)) {

    // Modification as swftools seems to be prepending the files directory by itself
    // and this has broken the preview function. Path needs to be stored just as
    // flash/movie.swf or flash/temp/movie.swf to avoid files/files/flash/movie.swf
    $swf_path_fix = _flashnode_filename($file->filename, TRUE);

    // if it is then save it to the flash/temp folder
    $file = file_save_upload($field_name, $swf_path_fix);

    // if the upload succeeds...
    if ($file) {

      // ...check to see we have either a flash movie, and flv, or an mp3
      $file_mime = strtolower($file->filemime);
      $file_ext = strtolower(substr(strrchr($file->filename, '.'), 1));
      if (strpos(trim(variable_get('flashnode_allowable_types', 'swf flv mp3')), $file_ext) === false) {
        watchdog($field_name, t('Flash node was given %type for upload.', array(
          '%type' => $file_ext,
        )));
        form_set_error($field_name, t('The specified file is not an allowed format.'));

        // delete the uploaded file in case it is unsafe
        file_delete($file->filepath);
        return;
      }
    }
    else {
      return;
    }

    // add data to the $node->flashnode object
    $node->flashnode['_flashnode'] = $swf_path_fix;
    $node->flashnode['filemime'] = $file->filemime;
    $node->new_file = TRUE;

    // If upload is not swf, remind user to set movie size
    if ($file_ext != 'swf') {
      drupal_set_message(t('Remember you might have to set the movie size for flv or mp3 files as flash node cannot always automatically determine the player size!'), 'warning');
    }
  }

  // try to get the file settings for this file, using image_get_info
  $info = image_get_info(file_create_path($node->flashnode['_flashnode']));
  $node->flashnode['_height'] = $info['height'];
  $node->flashnode['_width'] = $info['width'];

  // If the height and width settings are not available to this user then reset them everytime we get a new file
  // Otherwise the height/width will be locked to the first file that is uploaded
  global $user;
  if (!user_access('use basic options') && $user->uid != 1) {
    unset($node->flashnode['height']);
    unset($node->flashnode['width']);
  }
}