function uuid_features_file_export in UUID Features Integration 7
Exports the file and returns a handle to store in the features export.
Parameters
object $file: The file object to handle.
string $export_mode: The export mode - currently supported:
- remote
- local
- inline
- packaged
Return value
string Returns the handle to store in the features export or FALSE on failure.
Throws
Exception
2 calls to uuid_features_file_export()
- uuid_features_file_field_export in ./
uuid_features.module - Handle exporting file fields.
- uuid_file_entity_features_export_render in includes/
uuid_file_entity.features.inc - Implements hook_features_export_render().
File
- ./
uuid_features.module, line 554 - UUID Features module allows to export data stored in the db by features.
Code
function uuid_features_file_export($file, $export_mode) {
// Convert file to array to stay into the default
// uuid_features_file format.
$file = (object) $file;
// Check the file.
if (!isset($file->uri) || !is_file($file->uri)) {
$path = isset($file->uri) ? $file->uri : 'unknown';
throw new Exception(t("File field found on, but file '!path' doesn't exist on disk?", array(
'!path' => $path,
)));
}
if ($export_mode == 'local') {
$orig_assets_path = $assets_path = variable_get('uuid_features_file_assets_path', '');
// If files are supposed to be copied to the assets path.
if ($assets_path) {
// Ensure the assets path is created.
if (!is_dir($assets_path) && mkdir($assets_path, 0777, TRUE) == FALSE || !is_writable($assets_path)) {
// Try creating a public path if the local path isn't writeable.
// This is a kludgy solution to allow writing file assets to places
// such as the profiles/myprofile directory, which isn't supposed to
// be writeable.
$new_assets_path = 'public://' . $assets_path;
if (!is_dir($new_assets_path) && mkdir($new_assets_path, 0777, TRUE) == FALSE) {
// Don't continue if the assets path is not ready.
throw new Exception(t("Could not create assets path! '!path'", array(
'!path' => $assets_path,
)), 'error');
}
$assets_path = $new_assets_path;
}
// The writable path may be different from the path that gets
// saved during the feature export to handle the
// public path/local path dilemma mentioned above.
$writeable_export_data = $assets_path . '/' . basename($file->uri);
$export_data = $orig_assets_path . '/' . basename($file->uri);
if (!copy($file->uri, $writeable_export_data)) {
drupal_set_message(t("Export file error, could not copy '%filepath' to '%exportpath'.", array(
'%filepath' => $file->uri,
'%exportpath' => $writeable_export_data,
)), 'error');
return FALSE;
}
}
else {
$export_data = $file->uri;
}
}
elseif ($export_mode == 'remote') {
$export_data = file_create_url($file->uri);
}
elseif ($export_mode == 'packaged') {
$assets_path = variable_get('uuid_features_packaged_file_assets_path', 'assets');
$export_data = $assets_path . '/' . drupal_basename($file->uri);
}
else {
$export_data = base64_encode(file_get_contents($file->uri));
}
return $export_data;
}