|
@@ -38,9 +38,28 @@ class UploadableListener extends MappedEventSubscriber
|
|
const ACTION_INSERT = 'INSERT';
|
|
const ACTION_INSERT = 'INSERT';
|
|
const ACTION_UPDATE = 'UPDATE';
|
|
const ACTION_UPDATE = 'UPDATE';
|
|
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Default path to move files in
|
|
|
|
+ *
|
|
|
|
+ * @var string
|
|
|
|
+ */
|
|
private $defaultPath;
|
|
private $defaultPath;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Default FileInfoInterface class
|
|
|
|
+ *
|
|
|
|
+ * @var string
|
|
|
|
+ */
|
|
private $defaultFileInfoClass = 'Gedmo\Uploadable\FileInfo\FileInfoArray';
|
|
private $defaultFileInfoClass = 'Gedmo\Uploadable\FileInfo\FileInfoArray';
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Array of files to remove on postFlush
|
|
|
|
+ *
|
|
|
|
+ * @var array
|
|
|
|
+ */
|
|
|
|
+ private $pendingFileRemovals = array();
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* {@inheritdoc}
|
|
* {@inheritdoc}
|
|
*/
|
|
*/
|
|
@@ -48,7 +67,8 @@ class UploadableListener extends MappedEventSubscriber
|
|
{
|
|
{
|
|
return array(
|
|
return array(
|
|
'loadClassMetadata',
|
|
'loadClassMetadata',
|
|
- 'onFlush'
|
|
|
|
|
|
+ 'onFlush',
|
|
|
|
+ 'postFlush'
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -91,12 +111,30 @@ class UploadableListener extends MappedEventSubscriber
|
|
|
|
|
|
if ($config = $this->getConfiguration($om, $meta->name)) {
|
|
if ($config = $this->getConfiguration($om, $meta->name)) {
|
|
if (isset($config['uploadable']) && $config['uploadable']) {
|
|
if (isset($config['uploadable']) && $config['uploadable']) {
|
|
- $this->removeFile($meta, $config, $object);
|
|
|
|
|
|
+ $this->pendingFileRemovals[] = $this->getFilePath($meta, $config, $object);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Handle removal of files
|
|
|
|
+ *
|
|
|
|
+ * @param EventArgs
|
|
|
|
+ *
|
|
|
|
+ * @return void
|
|
|
|
+ */
|
|
|
|
+ public function postFlush(EventArgs $args)
|
|
|
|
+ {
|
|
|
|
+ if (!empty($this->pendingFileRemovals)) {
|
|
|
|
+ foreach ($this->pendingFileRemovals as $file) {
|
|
|
|
+ $this->removeFile($file);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $this->pendingFileRemovals = array();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* If it's a Uploadable object, verify if the file was uploaded.
|
|
* If it's a Uploadable object, verify if the file was uploaded.
|
|
* If that's the case, process it.
|
|
* If that's the case, process it.
|
|
@@ -180,8 +218,10 @@ class UploadableListener extends MappedEventSubscriber
|
|
$fileSizeField->setAccessible(true);
|
|
$fileSizeField->setAccessible(true);
|
|
}
|
|
}
|
|
|
|
|
|
- // First we remove the original file
|
|
|
|
- $this->removefile($meta, $config, $object);
|
|
|
|
|
|
+ if ($action === self::ACTION_UPDATE) {
|
|
|
|
+ // First we add the original file to the pendingFileRemovals array
|
|
|
|
+ $this->pendingFileRemovals[] = $this->getFilePath($meta, $config, $object);
|
|
|
|
+ }
|
|
|
|
|
|
$info = $this->moveFile($fileInfo, $path, $config['allowOverwrite'], $config['appendNumber']);
|
|
$info = $this->moveFile($fileInfo, $path, $config['allowOverwrite'], $config['appendNumber']);
|
|
$filePathField->setValue($object, $info['filePath']);
|
|
$filePathField->setValue($object, $info['filePath']);
|
|
@@ -212,22 +252,22 @@ class UploadableListener extends MappedEventSubscriber
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Removes a file from an Uploadable file
|
|
|
|
|
|
+ * Returns the path of the entity's file
|
|
*
|
|
*
|
|
* @param ClassMetadata
|
|
* @param ClassMetadata
|
|
* @param array - Configuration
|
|
* @param array - Configuration
|
|
* @param object - Entity
|
|
* @param object - Entity
|
|
*
|
|
*
|
|
- * @return bool
|
|
|
|
|
|
+ * @return string
|
|
*/
|
|
*/
|
|
- public function removeFile(ClassMetadata $meta, array $config, $object)
|
|
|
|
|
|
+ public function getFilePath(ClassMetadata $meta, array $config, $object)
|
|
{
|
|
{
|
|
$refl = $meta->getReflectionClass();
|
|
$refl = $meta->getReflectionClass();
|
|
$filePathField = $refl->getProperty($config['filePathField']);
|
|
$filePathField = $refl->getProperty($config['filePathField']);
|
|
$filePathField->setAccessible(true);
|
|
$filePathField->setAccessible(true);
|
|
$filePath = $filePathField->getValue($object);
|
|
$filePath = $filePathField->getValue($object);
|
|
|
|
|
|
- return $this->doRemoveFile($filePath);
|
|
|
|
|
|
+ return $filePath;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -237,7 +277,7 @@ class UploadableListener extends MappedEventSubscriber
|
|
*
|
|
*
|
|
* @return bool
|
|
* @return bool
|
|
*/
|
|
*/
|
|
- public function doRemoveFile($filePath)
|
|
|
|
|
|
+ public function removeFile($filePath)
|
|
{
|
|
{
|
|
if (is_file($filePath)) {
|
|
if (is_file($filePath)) {
|
|
return unlink($filePath);
|
|
return unlink($filePath);
|
|
@@ -305,7 +345,7 @@ class UploadableListener extends MappedEventSubscriber
|
|
|
|
|
|
if (is_file($info['filePath'])) {
|
|
if (is_file($info['filePath'])) {
|
|
if ($overwrite) {
|
|
if ($overwrite) {
|
|
- $this->doRemoveFile($info['filePath']);
|
|
|
|
|
|
+ $this->removeFile($info['filePath']);
|
|
} else if ($appendNumber) {
|
|
} else if ($appendNumber) {
|
|
$counter = 1;
|
|
$counter = 1;
|
|
$extensionPos = strrpos($info['filePath'], '.');
|
|
$extensionPos = strrpos($info['filePath'], '.');
|