Commit f14ec035 authored by alexpott's avatar alexpott

Issue #2394157 by benjy: Update the EntityFile destination to handle temporary files

parent a3efcb6f
......@@ -42,8 +42,14 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = array()) {
$source = $this->configuration['source_base_path'] . $row->getSourceProperty($this->configuration['source_path_property']);
$file = $row->getSourceProperty($this->configuration['source_path_property']);
$destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
// We check the destination to see if this is a temporary file. If it is
// then we do not prepend the source_base_path because temporary files are
// already absolute.
$source = $this->isTempFile($destination) ? $file : $this->configuration['source_base_path'] . $file;
$replace = FILE_EXISTS_REPLACE;
if (!empty($this->configuration['rename'])) {
$entity_id = $row->getDestinationProperty($this->getKey('id'));
......@@ -102,4 +108,18 @@ protected function urlencode($filename) {
return $filename;
}
/**
* Check if a file is a temp file.
*
* @param string $file
* The destination file path.
*
* @return bool
* TRUE if the file is temporary otherwise FALSE.
*/
protected function isTempFile($file) {
$tmp = 'temporary://';
return substr($file, 0, strlen($tmp)) === $tmp;
}
}
......@@ -14,6 +14,7 @@ process:
source:
- filepath
- file_directory_path
- temp_directory_path
- is_public
filemime: filemime
filesize: filesize
......
......@@ -13,6 +13,7 @@ process:
source:
- picture
- file_directory_path
- temp_directory_path
- 'constants/is_public'
destination:
plugin: entity:file
......
......@@ -25,7 +25,13 @@ class FileUri extends ProcessPluginBase {
*/
public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) {
list($filepath, $file_directory_path, $is_public) = $value;
list($filepath, $file_directory_path, $temp_directory_path, $is_public) = $value;
// Specific handling using $temp_directory_path for temporary files.
if (substr($filepath, 0, strlen($temp_directory_path)) === $temp_directory_path) {
$uri = preg_replace('/^' . preg_quote($temp_directory_path, '/') . '/', '', $filepath);
return "temporary://$uri";
}
// Strip the files path from the uri instead of using basename
// so any additional folders in the path are preserved.
......
......@@ -26,6 +26,13 @@ class File extends DrupalSqlBase {
*/
protected $filePath;
/**
* The temporary file path.
*
* @var string
*/
protected $tempFilePath;
/**
* Flag for private or public file storage.
*
......@@ -58,6 +65,7 @@ public function query() {
protected function runQuery() {
$conf_path = isset($this->configuration['conf_path']) ? $this->configuration['conf_path'] : 'sites/default';
$this->filePath = $this->variableGet('file_directory_path', $conf_path . '/files') . '/';
$this->tempFilePath = $this->variableGet('file_directory_temp', '/tmp') . '/';
// FILE_DOWNLOADS_PUBLIC == 1 and FILE_DOWNLOADS_PRIVATE == 2.
$this->isPublic = $this->variableGet('file_downloads', 1) == 1;
......@@ -69,6 +77,7 @@ protected function runQuery() {
*/
public function prepareRow(Row $row) {
$row->setSourceProperty('file_directory_path', $this->filePath);
$row->setSourceProperty('temp_directory_path', $this->tempFilePath);
$row->setSourceProperty('is_public', $this->isPublic);
return parent::prepareRow($row);
}
......
......@@ -26,6 +26,13 @@ class UserPictureFile extends DrupalSqlBase {
*/
protected $filePath;
/**
* The temporary file path.
*
* @var string
*/
protected $tempFilePath;
/**
* {@inheritdoc}
*/
......@@ -42,6 +49,7 @@ public function query() {
public function runQuery() {
$conf_path = isset($this->configuration['conf_path']) ? $this->configuration['conf_path'] : 'sites/default';
$this->filePath = $this->variableGet('file_directory_path', $conf_path . '/files') . '/';
$this->tempFilePath = $this->variableGet('file_directory_temp', '/tmp') . '/';
return parent::runQuery();
}
......@@ -51,6 +59,7 @@ public function runQuery() {
public function prepareRow(Row $row) {
$row->setSourceProperty('filename', basename($row->getSourceProperty('picture')));
$row->setSourceProperty('file_directory_path', $this->filePath);
$row->setSourceProperty('temp_directory_path', $this->tempFilePath);
return parent::prepareRow($row);
}
......
......@@ -123,7 +123,18 @@ public function load() {
'status' => '1',
'timestamp' => '1388880668',
))
->values(array(
'fid' => '4',
'uid' => '1',
'filename' => 'some-temp-file.jpg',
'filepath' => '/tmp/some-temp-file.jpg',
'filemime' => 'image/jpeg',
'filesize' => '183',
'status' => '0',
'timestamp' => '1388880668',
))
->execute();
file_put_contents('/tmp/some-temp-file.jpg', '');
}
}
......@@ -68,6 +68,10 @@ public function testFiles() {
$file = entity_load('file', 2);
$this->assertEqual($file->getFileUri(), 'public://core/modules/simpletest/files/image-2.jpg');
// Ensure that a temporary file has been migrated.
$file = entity_load('file', 4);
$this->assertIdentical($file->getFileUri(), 'temporary://some-temp-file.jpg');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment