You are here

protected function EntityShareMediasProviderAbstract::createFile in Entity Share 7

Create the file.

Parameters

string $url: Url of the remote file.

object $file_entity: Object of the original file.

string $filename: Name of the file.

Return value

bool|object File.

2 calls to EntityShareMediasProviderAbstract::createFile()
EntityShareMediasFileImport::importFile in modules/entity_share_medias/modules/entity_share_medias_file/includes/entity_share_medias_file.import.inc
Import the scald Atom.
EntityShareMediasScaldImport::importScaldAtom in modules/entity_share_medias/modules/entity_share_medias_scald/includes/entity_share_medias_scald.import.inc
Import the scald Atom.

File

modules/entity_share_medias/includes/entity_share_medias.inc, line 211
Class for handling Medias.

Class

EntityShareMediasProviderAbstract
Class EntityShareMediasProviderAbstract.

Code

protected function createFile($url, $file_entity, $filename = NULL) {
  if (empty($filename)) {
    $filename = drupal_basename($url);
  }

  // Get file folder.
  $relative_path = $this
    ->getRelativePathFromUrl($url);
  $file_scheme_dir = file_default_scheme() . '://' . $relative_path;

  // Get the media.
  $media = drupal_http_request($url);
  if ($media->code == 200 && file_prepare_directory($file_scheme_dir, FILE_CREATE_DIRECTORY)) {
    $dest = $file_scheme_dir . '/' . $filename;
    $file = file_save_data($media->data, $dest, FILE_EXISTS_REPLACE);
    if ($file) {

      // Set original uuid.
      $entity_info = entity_get_info('file');
      $uuid_key = $entity_info['entity keys']['uuid'];
      $file->{$uuid_key} = $file_entity->{$uuid_key};
      file_save($file);

      // Not added by file_save and necessary to have
      // dynamic type in the code.
      $file->entity_type = 'file';
      return $file;
    }
    else {
      return FALSE;
    }
  }
  else {
    watchdog(static::WATCHDOG_TYPE, "The file %file couldn't be created !", array(
      '%file' => $file_scheme_dir . '/' . $filename,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
}