I use media_item_create with the filename as the only parameter, this sends a media object to media_item_insert with $media->path set to the file name.

media_item_insert however, tries to copy the complete path+the filename again ( /tmp/image.gifimage.gif). This is the failing section:

  $name = (!empty($media->name) ? $media->name : media_file_name($media->path));
  $ext  = (!empty($media->ext)  ? $media->ext : media_file_ext($media->path));
  $fname = $name .'.'. $ext;
  if (!($file = media_file_copy($media->path . $fname, $path . $fname))) {
    media_error(t('Unable to copy !filename to !path', array('!filename' => $media->path . $fname, '!path' => $path . $fname)));
    return FALSE;
  }

As you can see, if $media->name is not set - the filename will become double.

Suggested replacement:

  $name = (!empty($media->name) ? $media->name : media_file_name($media->path));
  $ext  = (!empty($media->ext)  ? $media->ext : media_file_ext($media->path));
  $target = $name .'.'. $ext;
  if (empty($media->name)){
    $source = "";
  } else {
    $source = $target;
  }
  if (!($file = media_file_copy($media->path . $source, $path . $target))) {
    media_error(t('Unable to copy !filename to !path', array('!filename' => $media->path . $source, '!path' => $path . $target)));
    return FALSE;
  }

Comments

rhys’s picture

Version: 5.x-0.x-dev » 5.x-5.x-dev
Status: Active » Fixed

I changed a few things, including what happens in media_item_create, so that the process works fine.
Both having a media object with only the path set, as well as having everything set, has been accounted for.

Updated in the CVS.

Ibn al-Hazardous’s picture

Cool, thanks! (Goes for the other bugfix as well. :)

Anonymous’s picture

Status: Fixed » Closed (fixed)

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