You are here

function filefield_deploy in Deploy - Content Staging 6

@file Deployment API which enables modules to deploy items between servers.

This module manages deployment-related issues for files added via filefield.

File

modules/filefield_deploy/filefield_deploy.module, line 9
Deployment API which enables modules to deploy items between servers.

Code

function filefield_deploy($fid) {
  $file_info = field_file_load($fid);
  if (empty($file_info)) {
    return xmlrpc_error($xmlrpcusererr + 1, t('File not found'));
  }

  // Save the base64_encode()d file into the file_info array for transfer
  $filepath = file_create_path($file_info['filepath']);
  $binaryfile = fopen($filepath, 'rb');
  $filelength = filesize($filepath);
  $file = base64_encode(fread($binaryfile, $filelength));
  $file_info['file'] = $file;

  // Normally, like with users and nodes, the uuid is added to the object in the 'load'
  // op of their hook. Files have no load op, so we have to get it by hand.
  $remote_key = deploy_get_remote_key($file_info['uuid'], 'files');
  $file_info['fid'] = isset($remote_key['fid']) ? $remote_key['fid'] : NULL;

  // If there is no remote file, then we also set the status to
  // FILE_STATUS_TEMPORARY. If you don't do this, then Filefield
  // will fail out with a validation error on the remote site.
  // It will get set to FILE_STATUS_PERMANENT when the node
  // is saved.
  if (isset($remote_key['fid'])) {
    $file_info['fid'] = $remote_key['fid'];
  }
  else {
    $file_info['fid'] = NULL;
    $file_info['timestamp'] = time();
    $file_info['status'] = FILE_STATUS_TEMPORARY;
  }

  // The filefield service requires an object and I don't have time
  // to go change all the above at the moment
  $file_info = (object) $file_info;

  // And we're off.
  $fid = deploy_send(array(
    'file.save',
  ), array(
    $file_info,
  ));
  return $fid;
}