You are here

public function MigrateFile::processFile in Migrate 7.2

Default implementation of MigrateFileInterface::processFiles().

Parameters

$value: The URI or local filespec of a file to be imported.

$owner: User ID (uid) to be the owner of the file.

Return value

object The file entity being created or referenced.

Overrides MigrateFileInterface::processFile

2 calls to MigrateFile::processFile()
MigrateFileBlob::processFile in plugins/destinations/file.inc
Implementation of MigrateFileInterface::processFile().
MigrateFileUri::processFile in plugins/destinations/file.inc
Implementation of MigrateFileInterface::processFiles().
2 methods override MigrateFile::processFile()
MigrateFileBlob::processFile in plugins/destinations/file.inc
Implementation of MigrateFileInterface::processFile().
MigrateFileUri::processFile in plugins/destinations/file.inc
Implementation of MigrateFileInterface::processFiles().

File

plugins/destinations/file.inc, line 273
Support for file entity as destination. Note that File Fields have their own destination in fields.inc

Class

MigrateFile
Base class for creating core file entities.

Code

public function processFile($value, $owner) {
  $migration = Migration::currentMigration();

  // Determine the final path we want in Drupal - start with our preferred path.
  $destination = file_stream_wrapper_uri_normalize($this->destinationDir . '/' . ltrim($this->destinationFile, "/\\"));

  // Our own file_replace behavior - if the file exists, use it without
  // replacing it
  if ($this->fileReplace == self::FILE_EXISTS_REUSE) {

    // See if we this file already (we'll reuse and resave a file entity if it exists).
    if (file_exists($destination)) {
      $file = $this
        ->createFileEntity($destination, $owner);
      $file = file_save($file);
      $this
        ->markForPreservation($file->fid);
      return $file;
    }

    // No existing one to reuse, reset to REPLACE
    $this->fileReplace = FILE_EXISTS_REPLACE;
  }

  // Prepare the destination directory.
  $destdir = drupal_dirname($destination);
  if (!file_prepare_directory($destdir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    $migration
      ->saveMessage(t('Could not create destination directory for !dest', array(
      '!dest' => $destination,
    )));
    return FALSE;
  }

  // Determine whether we can perform this operation based on overwrite rules.
  $destination = file_destination($destination, $this->fileReplace);
  if ($destination === FALSE) {
    $migration
      ->saveMessage(t('The file could not be copied because file %dest already exists in the destination directory.', array(
      '%dest' => $destination,
    )));
    return FALSE;
  }

  // Make sure the .htaccess files are present.
  file_ensure_htaccess();

  // Put the file where it needs to be.
  if (!$this
    ->copyFile($destination)) {
    return FALSE;
  }

  // Set the permissions on the new file.
  drupal_chmod($destination);

  // Create and save the file entity.
  $file = file_save($this
    ->createFileEntity($destination, $owner));

  // Prevent deletion of the file on rollback if requested.
  if (is_object($file)) {
    $this
      ->markForPreservation($file->fid);
    return $file;
  }
  else {
    return FALSE;
  }
}