diff --git a/.travis.yml b/.travis.yml
index ea2c758..a42973a 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,6 +7,7 @@ php:
env:
- SYMFONY_VERSION=2.2.*
- SYMFONY_VERSION=2.3.*
+ - SYMFONY_VERSION=2.4.*
before_script:
@@ -15,6 +16,7 @@ before_script:
- cp .travis.composer.config.json ~/.composer/config.json
- composer self-update
- composer require symfony/framework-bundle:${SYMFONY_VERSION}
+ - composer require symfony/templating:${SYMFONY_VERSION}
- composer install --dev
notifications:
diff --git a/Controller/ProtectedController.php b/Controller/ProtectedController.php
new file mode 100644
index 0000000..6b7333d
--- /dev/null
+++ b/Controller/ProtectedController.php
@@ -0,0 +1,79 @@
+get('iphp.filestore.mapping.factory')
+ ->getMappingConfig($request->get('_iphpfilestore_mapping'));
+
+
+ if (!$mappingConfig) throw $this->createNotFoundException('File not found');
+
+
+ $protectedFileName = $mappingConfig['protected_dir'] . '/' . $path;
+
+
+ if (!file_exists($protectedFileName))
+ throw $this->createNotFoundException('File ' . $mappingConfig['upload_path'] . '/' . $path . ' not found');
+
+
+ if (!is_readable($protectedFileName))
+ throw $this->createNotFoundException('File ' . $mappingConfig['upload_path'] . '/' . $path . ' not readable');
+
+
+
+ if ($this->getUser() && array_intersect($mappingConfig['protected_roles'],$this->getUser()->getRoles()))
+ {
+ $response = new BinaryFileResponse($protectedFileName);
+ return $response;
+ }
+ else
+ {
+
+
+ return $this->noAccessToFileAction();
+
+
+ }
+
+
+
+ }
+
+
+
+
+
+ protected function noAccessToFileAction ()
+ {
+ if (!$this->getUser()) return $this->unauthorizedAction();
+
+ else return $this->insufficientRightsAction();
+ }
+
+
+
+ protected function unauthorizedAction()
+ {
+ throw new HttpException(401, 'Unauthorized access.' );
+
+ return array();
+ }
+
+
+ protected function insufficientRightsAction()
+ {
+ return $this->render ("IphpFileStoreBundle:Protected:insufficient.html.twig", array ())->setStatusCode(Response::HTTP_FORBIDDEN);
+ }
+}
diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php
index 1f2381a..1b346b7 100644
--- a/DependencyInjection/Configuration.php
+++ b/DependencyInjection/Configuration.php
@@ -32,6 +32,16 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('upload_dir')->end()
->scalarNode('upload_path')->end()
+
+ ->scalarNode('protected')->defaultFalse()->end()
+ ->scalarNode('protected_dir')->defaultNull()->end()
+ ->scalarNode('protected_controller')->defaultNull()->end()
+ ->scalarNode('protected_authredirect')->defaultTrue()->end()
+ ->arrayNode ('protected_roles')
+ ->prototype('scalar')->end()
+ ->defaultValue(array ('ROLE_ADMIN'))
+ ->end()
+
->scalarNode('delete_on_remove')->defaultTrue()->end()
->scalarNode('overwrite_duplicates')->defaultFalse()->end()
diff --git a/EventListener/UploaderListener.php b/EventListener/UploaderListener.php
index 335ad2d..c48b80f 100644
--- a/EventListener/UploaderListener.php
+++ b/EventListener/UploaderListener.php
@@ -145,7 +145,7 @@ public function postFlush(EventArgs $args)
if (!$this->deferredFiles[$obj]) continue;
foreach ($this->deferredFiles[$obj] as $mapping) {
- $fileData = $this->fileStorage->upload($mapping, $this->deferredFiles[$obj][$mapping]);
+ $fileData = $this->fileStorage->saveFile($mapping, $this->deferredFiles[$obj][$mapping]);
$mapping->setFileDataPropertyValue($fileData);
}
@@ -170,7 +170,9 @@ public function preUpdate(\Doctrine\Common\EventArgs $args)
$file = $mapping->getFileUploadPropertyValue();
$currentFileData = $this->dataStorage->currentFieldData($mapping->getFileDataPropertyName(), $args);
- $currentFileName = $currentFileData ? $mapping->resolveFileName($currentFileData['fileName']) : null;
+ $currentFileName = $currentFileData ?
+ $mapping->resolveFileName($currentFileData['fileName'],
+ isset($currentFileData['protected']) && $currentFileData['protected'] ? true : false) : null;
//If no new file
@@ -179,6 +181,8 @@ public function preUpdate(\Doctrine\Common\EventArgs $args)
if ($currentFileData) {
if (!$this->fileStorage->fileExists($currentFileName)) {
+
+ //try to restore file by web path
$fileNameByWebDir = $_SERVER['DOCUMENT_ROOT'].$currentFileData['path'];
if ($this->fileStorage->fileExists($fileNameByWebDir))
@@ -199,14 +203,37 @@ public function preUpdate(\Doctrine\Common\EventArgs $args)
} //uploaded file has deleted status
else if ($file instanceof \Iphp\FileStoreBundle\File\File && $file->isDeleted()) {
if ($this->fileStorage->removeFile($currentFileName)) $mapping->setFileDataPropertyValue(null);
- } else {
+ }
+
+
+ //changed protect attribute
+/* else if ($file instanceof \Iphp\FileStoreBundle\File\File && $file->isProtected()) {
+
+
+ $fileData = $this->fileStorage->move($mapping, $file);
+ $mapping->setFileDataPropertyValue($fileData);
+
+ die ('Protect in action!');
+ //if ($this->fileStorage->removeFile($currentFileName)) $mapping->setFileDataPropertyValue(null);
+
+
+
+
+ }*/
+
+
+ else {
//Old value (file) exits and uploaded new file
if ($currentFileData && !$this->fileStorage->isSameFile($file, $currentFileName))
//before upload new file delete old file
$this->fileStorage->removeFile($currentFileName);
- $fileData = $this->fileStorage->upload($mapping, $file);
+ $fileData = $this->fileStorage->saveFile ($mapping, $file);
+
+
+
+
$mapping->setFileDataPropertyValue($fileData);
}
}
diff --git a/File/File.php b/File/File.php
index 6785f18..83ddbc7 100644
--- a/File/File.php
+++ b/File/File.php
@@ -8,12 +8,18 @@
namespace Iphp\FileStoreBundle\File;
-use Symfony\Component\HttpFoundation\File\UploadedFile;
+use Symfony\Component\HttpFoundation\File\File as BaseFile;
-class File extends UploadedFile
+class File extends BaseFile implements LocalFileInterface
{
protected $deleted = false;
+ protected $protected = false;
+
+ protected $originalName;
+
+ protected $saveSource = true;
+
static function createEmpty()
{
return new File();
@@ -22,14 +28,14 @@ static function createEmpty()
public function __construct($path = null, $originalName = null, $mimeType = null,
$size = null, $error = null, $test = false)
{
- if ($path !== null)
- parent::__construct($path, $originalName, $mimeType,$size, $error, $test);
+ if ($path !== null)
+ parent::__construct($path, $originalName, $mimeType, $size, $error, $test);
}
- public function delete ()
+ public function delete()
{
- // parent::__construct (__FILE__,'just for pass validator');
+ // parent::__construct (__FILE__,'just for pass validator');
$this->deleted = true;
return $this;
}
@@ -40,8 +46,58 @@ public function isDeleted()
}
- function isValid()
+ /**
+ * @param boolean $protected
+ */
+ public function setProtected($protected)
+ {
+ $this->protected = $protected;
+ return $this;
+ }
+
+ /**
+ * @return boolean
+ */
+ public function isProtected()
{
- return true;
+ return $this->protected;
}
+
+ /**
+ * @param mixed $originalName
+ */
+ public function setOriginalName($originalName)
+ {
+ $this->originalName = $originalName;
+ return $this;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getOriginalName()
+ {
+ return $this->originalName;
+ }
+
+ /**
+ * @param boolean $saveSource
+ */
+ public function setSaveSource($saveSource)
+ {
+ $this->saveSource = $saveSource;
+ return $this;
+ }
+
+ /**
+ * @return boolean
+ */
+ public function getSaveSource()
+ {
+ return $this->saveSource;
+ }
+
+
+
+
}
diff --git a/File/FileInterface.php b/File/FileInterface.php
new file mode 100644
index 0000000..e8c7468
--- /dev/null
+++ b/File/FileInterface.php
@@ -0,0 +1,14 @@
+
+ */
+
+namespace Iphp\FileStoreBundle\File;
+
+
+interface FileInterface
+{
+ function isProtected();
+ function setProtected($protected);
+
+}
\ No newline at end of file
diff --git a/File/FormFile.php b/File/FormFile.php
new file mode 100644
index 0000000..ed89272
--- /dev/null
+++ b/File/FormFile.php
@@ -0,0 +1,11 @@
+
+ */
+
+namespace Iphp\FileStoreBundle\File;
+
+
+class FormFile {
+
+}
\ No newline at end of file
diff --git a/File/LocalFileInterface.php b/File/LocalFileInterface.php
new file mode 100644
index 0000000..281946b
--- /dev/null
+++ b/File/LocalFileInterface.php
@@ -0,0 +1,23 @@
+
+ */
+
+namespace Iphp\FileStoreBundle\File;
+
+
+interface LocalFileInterface extends FileInterface
+{
+
+ function isDeleted();
+
+ function delete();
+
+ function getFilename();
+
+ function getMimeType();
+
+ function getOriginalName();
+
+ function getSaveSource();
+}
\ No newline at end of file
diff --git a/File/UploadedFile.php b/File/UploadedFile.php
new file mode 100644
index 0000000..7cfcabb
--- /dev/null
+++ b/File/UploadedFile.php
@@ -0,0 +1,48 @@
+
+ */
+class UploadedFile extends BaseUploadedFile implements UploadedFileInterface
+{
+
+
+ protected $protected = false;
+
+
+ static function createFrom(BaseUploadedFile $baseUploadedFile, $test = false)
+ {
+ return new UploadedFile (
+ $baseUploadedFile->getPathname(),
+ $baseUploadedFile->getClientOriginalName(),
+ $baseUploadedFile->getClientMimeType(),
+ $baseUploadedFile->getClientSize(),
+ $baseUploadedFile->getError(),
+ $test);
+
+
+ }
+
+ /**
+ * @param boolean $protected
+ */
+ public function setProtected($protected)
+ {
+ $this->protected = $protected;
+ return $this;
+ }
+
+ /**
+ * @return boolean
+ */
+ public function isProtected()
+ {
+ return $this->protected;
+ }
+
+
+}
\ No newline at end of file
diff --git a/File/UploadedFileInterface.php b/File/UploadedFileInterface.php
new file mode 100644
index 0000000..5ef246a
--- /dev/null
+++ b/File/UploadedFileInterface.php
@@ -0,0 +1,17 @@
+
+ */
+
+namespace Iphp\FileStoreBundle\File;
+
+
+interface UploadedFileInterface extends FileInterface
+{
+ function isProtected();
+ function setProtected($protected);
+
+ function getClientOriginalName();
+ function getClientMimeType();
+
+}
\ No newline at end of file
diff --git a/FileStorage/FileStorageInterface.php b/FileStorage/FileStorageInterface.php
index 09275b2..5789eb4 100644
--- a/FileStorage/FileStorageInterface.php
+++ b/FileStorage/FileStorageInterface.php
@@ -2,8 +2,12 @@
namespace Iphp\FileStoreBundle\FileStorage;
+use Iphp\FileStoreBundle\File\FileInterface;
+use Iphp\FileStoreBundle\File\LocalFileInterface;
+use Iphp\FileStoreBundle\File\UploadedFileInterface;
use Iphp\FileStoreBundle\Mapping\PropertyMapping;
use Symfony\Component\HttpFoundation\File\File;
+use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* StorageInterface.
@@ -20,7 +24,12 @@ interface FileStorageInterface
*
* @param object $obj The object.
*/
- public function upload(PropertyMapping $mapping, File $file);
+ public function saveUploadedFile (PropertyMapping $mapping, UploadedFile $file);
+
+ public function saveLocalFile (PropertyMapping $mapping, File $file);
+
+
+ public function saveFile (PropertyMapping $mapping, File $file);
/**
diff --git a/FileStorage/FileSystemStorage.php b/FileStorage/FileSystemStorage.php
index d351d46..62b39da 100644
--- a/FileStorage/FileSystemStorage.php
+++ b/FileStorage/FileSystemStorage.php
@@ -2,6 +2,9 @@
namespace Iphp\FileStoreBundle\FileStorage;
+use Iphp\FileStoreBundle\File\FileInterface;
+use Iphp\FileStoreBundle\File\LocalFileInterface;
+use Iphp\FileStoreBundle\File\UploadedFileInterface;
use Iphp\FileStoreBundle\FileStorage\FileStorageInterface;
use Iphp\FileStoreBundle\Mapping\PropertyMapping;
@@ -57,22 +60,6 @@ public function setSameFileChecker (\Closure $checker)
}
-
-
- protected function getOriginalName(File $file)
- {
- return $file instanceof UploadedFile ?
- $file->getClientOriginalName() : $file->getFilename();
- }
-
-
- protected function getMimeType(File $file)
- {
- return $file instanceof UploadedFile ?
- $file->getClientMimeType() : $file->getMimeType();
- }
-
-
public function isSameFile (File $file, $fullFileName)
{
return call_user_func(
@@ -123,38 +110,70 @@ protected function checkDirectory ($directory)
}
- /**
- * {@inheritDoc}
- * File may be \Symfony\Component\HttpFoundation\File\File or \Symfony\Component\HttpFoundation\File\UploadedFile
- */
- public function upload(PropertyMapping $mapping, File $file)
+
+
+ public function saveLocalFile(PropertyMapping $mapping, File $file)
{
- $originalName = $this->getOriginalName($file);
- $mimeType = $this->getMimeType($file);
+ $originalName = $file instanceof FileInterface && $file->getOriginalName() ?
+ $file->getOriginalName() : $file->getFilename();
+ $mimeType = $file->getMimeType();
+
+ $fileProtected = $file instanceof FileInterface && $file->isProtected();
//transform filename and directory name if namer exists in mapping definition
- list ($fileName, $webPath) = $mapping->prepareFileName($originalName, $this);
- $fullFileName = $mapping->resolveFileName($fileName);
+ list ($fileName, $webPath) = $mapping->prepareFileName($originalName, $this, $fileProtected );
+
+ $fullFileName = $mapping->resolveFileName($fileName, $fileProtected );
+
//check if file already placed in needed position
if (!$this->isSameFile($file, $fullFileName)) {
$fileInfo = pathinfo($fullFileName);
+ $this->checkDirectory($fileInfo['dirname']);
- if ($file instanceof UploadedFile)
+ if ( $file instanceof FileInterface && $file->getSaveSource() == false) $file->move($fileInfo['dirname'], $fileInfo['basename']); else
{
- $this->checkDirectory($fileInfo['dirname']);
- $file->move($fileInfo['dirname'], $fileInfo['basename']);
+ $this->copyFile($file->getPathname(), $fileInfo['dirname'], $fileInfo['basename']);
}
- else $this->copyFile($file->getPathname(), $fileInfo['dirname'], $fileInfo['basename']);
}
+ return $this->prepareFileData($fileName, $originalName, $mimeType, $fullFileName, $webPath, $fileProtected);
+ }
+
+
+ public function saveUploadedFile(PropertyMapping $mapping, UploadedFile $file)
+ {
+ $originalName = $file->getClientOriginalName();
+ $mimeType = $file->getClientMimeType();
+
+ //transform filename and directory name if namer exists in mapping definition
+ list ($fileName, $webPath) = $mapping->prepareFileName($originalName, $this);
+
+ $fileProtected = $file instanceof UploadedFileInterface && $file->isProtected();
+
+ $fullFileName = $mapping->resolveFileName($fileName, $fileProtected );
+
+
+ $fileInfo = pathinfo($fullFileName);
+
+ $this->checkDirectory($fileInfo['dirname']);
+ $file->move($fileInfo['dirname'], $fileInfo['basename']);
+
+ return $this->prepareFileData($fileName, $originalName, $mimeType, $fullFileName, $webPath, $fileProtected);
+ }
+
+
+
+ protected function prepareFileData ($fileName, $originalName, $mimeType, $fullFileName, $webPath, $protected)
+ {
$fileData = array(
'fileName' => $fileName,
'originalName' => $originalName,
'mimeType' => $mimeType,
'size' => filesize($fullFileName),
- 'path' => $webPath
+ 'path' => $webPath,
+ 'protected' => $protected ? true : false
);
if (!$fileData['path'])
@@ -164,7 +183,7 @@ public function upload(PropertyMapping $mapping, File $file)
$ext = substr($originalName,strrpos ($originalName,'.')+1);
if ((in_array($fileData['mimeType'], array('image/png', 'image/jpeg', 'image/pjpeg')) ||
- in_array ($ext,array ('jpeg','jpg','png')))
+ in_array ($ext,array ('jpeg','jpg','png')))
&& function_exists('getimagesize')
) {
list($width, $height, $type) = @getimagesize($fullFileName);
@@ -177,6 +196,17 @@ public function upload(PropertyMapping $mapping, File $file)
}
+ /**
+ * {@inheritDoc}
+ * File may be \Symfony\Component\HttpFoundation\File\File or \Symfony\Component\HttpFoundation\File\UploadedFile
+ */
+ public function saveFile (PropertyMapping $mapping, File $file)
+ {
+ if ($file instanceof UploadedFile ) return $this->saveUploadedFile($mapping, $file);
+ else return $this->saveLocalFile($mapping, $file);
+ }
+
+
/**
* {@inheritDoc}
*/
@@ -198,10 +228,4 @@ public function fileExists($fullFileName)
{
return file_exists($fullFileName);
}
-
-
-
-
-
-
}
diff --git a/Form/DataTransformer/FileDataTransformer.php b/Form/DataTransformer/FileDataTransformer.php
index 2fe654f..3b0ec11 100644
--- a/Form/DataTransformer/FileDataTransformer.php
+++ b/Form/DataTransformer/FileDataTransformer.php
@@ -2,6 +2,7 @@
namespace Iphp\FileStoreBundle\Form\DataTransformer;
+use Iphp\FileStoreBundle\File\File;
use Symfony\Component\Form\DataTransformerInterface;
use Iphp\FileStoreBundle\FileStorage\FileStorageInterface;
use Iphp\FileStoreBundle\Mapping\PropertyMapping;
@@ -38,9 +39,20 @@ public function setMapping(PropertyMapping $mapping)
return $this;
}
+ /**
+ * @return \Iphp\FileStoreBundle\Mapping\PropertyMapping
+ */
+ public function getMapping()
+ {
+ return $this->mapping;
+ }
+
public function transform($fileDataFromDb)
{
+ if (is_array($fileDataFromDb) && isset($fileDataFromDb['fileName']))
+ $fileDataFromDb['protected'] = $fileDataFromDb['isprotected'] = isset($fileDataFromDb['protected']) && $fileDataFromDb['protected'];
+
return $fileDataFromDb;
}
@@ -52,16 +64,86 @@ public function transform($fileDataFromDb)
*/
public function reverseTransform($fileDataFromForm)
{
- if ($this->mapping && $fileDataFromForm['delete']) {
+ if (!$this->mapping) return $fileDataFromForm['file'];
+
+ if ($fileDataFromForm['delete']) {
+
+ $existFileProtected = isset($fileDataFromForm['protected']) && $fileDataFromForm['protected'];
+
//File may no exists
try {
- $this->fileStorage->removeFile($this->mapping->resolveFileName($fileDataFromForm['fileName']));
-
+ $this->fileStorage->removeFile(
+ $this->mapping->resolveFileName($fileDataFromForm['fileName'], $existFileProtected));
} catch (\Exception $e) {
}
}
+
+
+ if ($this->mapping->getProtected() == 'ondemand') {
+
+
+ $protected = isset($fileDataFromForm['isprotected']) && $fileDataFromForm['isprotected'];
+
+ //transform to \Iphp\FileStoreBundle\File\UploadedFile with attribute protected = true
+ if ($fileDataFromForm['file']) {
+ $fileDataFromForm['file'] = \Iphp\FileStoreBundle\File\UploadedFile::createFrom($fileDataFromForm['file']);
+ $fileDataFromForm['file']->setProtected($protected);
+
+ } else {
+
+ //cur file data exists
+ if (isset($fileDataFromForm['fileName'])) {
+
+ //print_r($fileDataFromForm);
+
+ $setFileProtected = isset($fileDataFromForm['isprotected']) && $fileDataFromForm['isprotected'];
+ $existFileProtected = isset($fileDataFromForm['protected']) && $fileDataFromForm['protected'];
+
+
+ //change status of exists file
+ if ($setFileProtected != $existFileProtected) {
+ $existFileName = $this->mapping->resolveFileName($fileDataFromForm['fileName'],
+ $existFileProtected);
+
+
+ if ($this->fileStorage->fileExists($existFileName)) {
+ // print $existFileName.' '.$setFileProtected;
+ $fileDataFromForm['file'] = new File($existFileName);
+ $fileDataFromForm['file']
+ ->setProtected($setFileProtected)
+ ->setOriginalName($fileDataFromForm['originalName'])
+ ->setSaveSource(false);
+
+
+ }
+ //exit();
+
+ }
+ //exit();
+
+ }
+
+
+ //
+
+
+ /* print_r($fileDataFromForm);
+
+ print $fileDataFromForm['fileName'] . '-' . $protected;
+ exit();
+
+
+ // Данные из текущего положения файла
+ $fileDataFromForm['file'] = new File($this->mapping->resolveFileName($fileDataFromForm['fileName'],
+
+ isset($fileDataFromForm['fileName'])
+ ));
+ $fileDataFromForm['file']->setProtected($protected);*/
+ }
+ }
+
return $fileDataFromForm['file'];
}
}
diff --git a/Form/Type/FileType.php b/Form/Type/FileType.php
index 2494823..15d0cc0 100644
--- a/Form/Type/FileType.php
+++ b/Form/Type/FileType.php
@@ -38,13 +38,15 @@ class FileType extends AbstractType
/**
* @var \Iphp\FileStoreBundle\DataStorage\DataStorageInterface
*/
- protected $dataStorage;
+ protected $dataStorage;
/**
* @var \Iphp\FileStoreBundle\FileStorage\FileStorageInterface
*/
protected $fileStorage;
+ protected $transformer;
+
public function __construct(PropertyMappingFactory $mappingFactory,
DataStorageInterface $dataStorage,
FileStorageInterface $fileStorage)
@@ -52,6 +54,7 @@ public function __construct(PropertyMappingFactory $mappingFactory,
$this->mappingFactory = $mappingFactory;
$this->dataStorage = $dataStorage;
$this->fileStorage = $fileStorage;
+ $this->transformer = new FileDataTransformer($this->fileStorage);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
@@ -65,20 +68,38 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
}
+ public function buildView(FormView $view, FormInterface $form, array $options)
+ {
+
+ $mapping = $this->transformer->getMapping();
+ $view->vars['is_mapped'] = $mapping ? true : false;
+
+ if (!$mapping) return;
+
+
+ // $view->vars['mappingProtected'] = $mapping->getProtected();
+ }
+
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
-
- $transformer = new FileDataTransformer($this->fileStorage);
- $subscriber = new FileTypeBindSubscriber($this->mappingFactory,$this->dataStorage, $transformer);
+ $subscriber = new FileTypeBindSubscriber(
+ $this->mappingFactory,
+ $this->dataStorage,
+ $this->transformer,
+ $builder->getFormFactory()
+ );
$builder->addEventSubscriber($subscriber);
$builder->add('file', 'file', array('required' => false))
->add('delete', 'checkbox', array('required' => false))
- ->addViewTransformer($transformer);
+ ->addViewTransformer($this->transformer);
+
+
+
//for sonata admin
// ->addViewTransformer(new FileDataViewTransformer());
diff --git a/Form/Type/FileTypeBindSubscriber.php b/Form/Type/FileTypeBindSubscriber.php
index 3e13d4b..6e7bd38 100644
--- a/Form/Type/FileTypeBindSubscriber.php
+++ b/Form/Type/FileTypeBindSubscriber.php
@@ -1,11 +1,13 @@
@@ -29,21 +31,72 @@ class FileTypeBindSubscriber implements EventSubscriberInterface
*/
private $dataStorage;
+ /**
+ * @var \Symfony\Component\Form\FormFactoryInterface
+ */
+ private $formFactory;
+
+
public function __construct(PropertyMappingFactory $mappingFactory,
DataStorageInterface $dataStorage,
- FileDataTransformer $transformer)
+ FileDataTransformer $transformer,
+ FormFactoryInterface $formFactory)
{
$this->mappingFactory = $mappingFactory;
$this->dataStorage = $dataStorage;
$this->transformer = $transformer;
+ $this->formFactory = $formFactory;
}
public static function getSubscribedEvents()
{
- return array(FormEvents::PRE_BIND => 'preBind');
+ return array(
+ FormEvents::PRE_BIND => 'preBind',
+ FormEvents::POST_SET_DATA => 'postSet'
+ );
}
+ public function postSet(FormEvent $event)
+ {
+ $data = $event->getData();
+ $form = $event->getForm();
+
+ //for first call event parent is null, cause of setData(null) in Form constructor.
+ if (!$form->getParent()) return;
+ $obj = $form->getParent()->getData();
+ if (!$obj) return;
+
+ $mapping = $this->mappingFactory->getMappingFromField($obj,
+ $this->dataStorage->getReflectionClass($obj),
+ $form->getName());
+ if ($mapping) {
+ $this->transformer->setMapping($mapping);
+
+
+
+ //print '---'.$mapping->getProtected();
+
+ if ($mapping->getProtected() == 'ondemand') {
+
+ // print 55;
+
+ $form->add($this->formFactory->createNamed('isprotected', 'checkbox', null, array(
+
+ 'label' => 'Protected file',
+ 'required' => false
+ )));
+
+ /* $form->add ('isprotected', 'checkbox', array(
+
+ 'label' => 'Protected file',
+ 'required' => false
+ ));*/
+ }
+ }
+
+ }
+
public function preBind(FormEvent $event)
{
@@ -54,7 +107,7 @@ public function preBind(FormEvent $event)
//For oneToMany at SonataAdmin
if (!$obj) return;
- $mapping = $this->mappingFactory->getMappingFromField($obj,
+ $mapping = $this->mappingFactory->getMappingFromField($obj,
$this->dataStorage->getReflectionClass($obj),
$form->getName());
if ($mapping) $this->transformer->setMapping($mapping);
diff --git a/Locator/FileLocator.php b/Locator/FileLocator.php
new file mode 100644
index 0000000..86f5272
--- /dev/null
+++ b/Locator/FileLocator.php
@@ -0,0 +1,40 @@
+
+ */
+class FileLocator
+{
+
+
+ /**
+ * @var \Iphp\FileStoreBundle\Mapping\PropertyMappingFactory
+ */
+ protected $propertyMappingFactory;
+
+ function __construct(PropertyMappingFactory $propertyMappingFactory)
+ {
+ $this->propertyMappingFactory = $propertyMappingFactory;
+ }
+
+
+ function getFileFromEntity($entity, $field)
+ {
+ $mapping = $this->propertyMappingFactory->getMappingFromField($entity, new \ReflectionClass ($entity), $field);
+ if (!$mapping) return null;
+
+ $fileName = $mapping->resolveFileName();
+ if (!$fileName ) return null;
+
+ return new File ($fileName);
+ }
+
+
+}
\ No newline at end of file
diff --git a/Mapping/PropertyMapping.php b/Mapping/PropertyMapping.php
index 482d724..6586193 100644
--- a/Mapping/PropertyMapping.php
+++ b/Mapping/PropertyMapping.php
@@ -16,8 +16,6 @@
*/
class PropertyMapping
{
-
-
protected $obj;
/**
@@ -152,13 +150,6 @@ public function useDirectoryNamer($fileName, $clientOriginalName)
isset($namer['params']) ? $namer['params'] : array());
-/* $subPath = call_user_func(
- array($this->container->get($namer['service']), $method . 'Rename'),
- $this,
- $replaceMode ? $path : $fileName,
- isset($namer['params']) ? $namer['params'] : array());*/
-
-
if ($replaceMode) $path = $subPath;
else $path .= ($subPath ? '/' : '') . $subPath;
}
@@ -169,10 +160,9 @@ public function useDirectoryNamer($fileName, $clientOriginalName)
}
- public function needResolveCollision($fileName, FileStorageInterface $fileStorage)
+ public function needResolveCollision($fileName, FileStorageInterface $fileStorage, $protected = false)
{
- //print "\n -->".$fileName;
- return !$this->isOverwriteDuplicates() && $fileStorage->fileExists($this->resolveFileName($fileName));
+ return !$this->isOverwriteDuplicates() && $fileStorage->fileExists($this->resolveFileName($fileName,$protected));
}
@@ -182,22 +172,23 @@ public function needResolveCollision($fileName, FileStorageInterface $fileStorag
* @return array relative or full fileName and file path at web
* @throws \Exception
*/
- public function prepareFileName($originalName, FileStorageInterface $fileStorage)
+ public function prepareFileName($originalName, FileStorageInterface $fileStorage, $protected = false)
{
+
$fileName = $origName = $this->useFileNamer($originalName);
$dirName = $this->useDirectoryNamer($fileName, $originalName);
if (substr($dirName,-1) != '/') $dirName.='/';
$try = 0;
- while ($this->needResolveCollision( $dirName . $fileName , $fileStorage)) {
+ while ($this->needResolveCollision( $dirName . $fileName , $fileStorage, $protected)) {
+
if ($try > 15)
throw new \Exception ("Can't resolve collision for file " . $fileName);
$fileName = $this->resolveFileCollision($origName, $originalName, ++$try);
}
-
return array(
//file system path
($this->isStoreFullDir() ? $this->getUploadDir() : '') . $dirName. $fileName ,
@@ -215,11 +206,9 @@ public function prepareFileName($originalName, FileStorageInterface $fileStorage
*/
public function resolveFileCollision($fileName, $clientOriginalName, $attempt = 1)
{
-
if ($this->hasNamer()) {
$firstNamer = current($this->config['namer']);
-
return $this->namerServiceInvoker->resolveCollision ($firstNamer['service'], $fileName, $attempt);
}
@@ -233,12 +222,35 @@ public function getUploadDir()
}
+ public function getStoreDir ($protected = false)
+ {
+ return $this->getProtected() === true || $this->getProtected() == 'ondemand' && $protected ?
+ $this->getProtectedDir() : $this->getUploadDir();
+ }
+
+
public function getUploadPath()
{
return $this->config['upload_path'];
}
+ public function getProtected()
+ {
+ return isset($this->config['protected']) ? $this->config['protected'] : null;
+ }
+
+
+
+ public function getProtectedDir()
+ {
+ return $this->config['protected_dir'];
+ }
+
+
+
+
+
/**
* Sets the configured configuration mapping.
*
@@ -344,15 +356,20 @@ public function getObj()
}
- public function resolveFileName($fileName = null)
+ public function resolveFileName($fileName = null, $protected = null)
{
if (!$fileName) {
$fileData = $this->getFileDataPropertyValue();
- if ($fileData) $fileName = $fileData['fileName'];
+ if ($fileData)
+ {
+ $fileName = $fileData['fileName'];
+ $protected = isset($fileData['protected']) && $fileData['protected'];
+ }
}
if (!$fileName) return null;
- $dir = $this->isStoreFullDir() ? '' : $this->getUploadDir();
+ $dir = $this->isStoreFullDir() ? '' : $this->getStoreDir($protected);
+
return $dir . (substr($dir,-1) != '/' && substr($fileName,0,1) != '/' ? '/' : ''). $fileName;
}
@@ -363,4 +380,4 @@ public function getConfig()
{
return $this->config;
}
-}
+}
\ No newline at end of file
diff --git a/Mapping/PropertyMappingFactory.php b/Mapping/PropertyMappingFactory.php
index ebfb198..7c7f2e6 100644
--- a/Mapping/PropertyMappingFactory.php
+++ b/Mapping/PropertyMappingFactory.php
@@ -126,5 +126,11 @@ protected function createMapping($obj, \ReflectionClass $class, UploadableField
}
+ function getMappingConfig ($mappingName)
+ {
+ return isset($this->mappingsConfig[$mappingName]) ? $this->mappingsConfig[$mappingName] : null;
+ }
+
+
}
diff --git a/Resources/config/routing.xml b/Resources/config/routing.xml
deleted file mode 100644
index 4c9969a..0000000
--- a/Resources/config/routing.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
-
- IphpFilestoreBundle:Default:index
-
-
diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml
new file mode 100644
index 0000000..c09c46e
--- /dev/null
+++ b/Resources/config/routing.yml
@@ -0,0 +1,3 @@
+_iphp_filestore:
+ resource: .
+ type: iphp_filestore
diff --git a/Resources/config/services.xml b/Resources/config/services.xml
index d48280f..a5ddc9d 100644
--- a/Resources/config/services.xml
+++ b/Resources/config/services.xml
@@ -6,16 +6,35 @@
+
+
+ Iphp\FileStoreBundle\EventListener\UploaderListener
+ Iphp\FileStoreBundle\Mapping\PropertyMappingFactory
+ Iphp\FileStoreBundle\Form\Type\FileType
Iphp\FileStoreBundle\Naming\DefaultNamer
Iphp\FileStoreBundle\Naming\DefaultDirectoryNamer
Iphp\FileStoreBundle\Naming\NamerServiceInvoker
+ Iphp\FileStoreBundle\Routing\ProtectFilesLoader
+ Iphp\FileStoreBundle\Driver\AnnotationDriver
+ Iphp\FileStoreBundle\FileStorage\FileSystemStorage
+ Iphp\FileStoreBundle\Locator\FileLocator
+
+
+
+
+
+ %iphp.filestore.mappings%
+
+
+
+
-
+
@@ -37,7 +56,7 @@
-
@@ -45,7 +64,7 @@
-
@@ -54,15 +73,22 @@
-
+
-
+
+
+
+
+
+
+
diff --git a/Resources/translations/messages.ru.xliff b/Resources/translations/messages.ru.xliff
index 70ef4a9..22c1a42 100644
--- a/Resources/translations/messages.ru.xliff
+++ b/Resources/translations/messages.ru.xliff
@@ -11,6 +11,12 @@
Delete
Удалить
+
+
+ Protected file
+ Защищенный файл
+
+