Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateBundle #27

Merged
merged 1 commit into from
Aug 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions Adapter/Gaufrette/AbstractCmfMediaDoctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@
use Symfony\Cmf\Bundle\MediaBundle\DirectoryInterface;
use Symfony\Cmf\Bundle\MediaBundle\FileInterface;
use Symfony\Cmf\Bundle\MediaBundle\HierarchyInterface;
use Symfony\Cmf\Bundle\MediaBundle\MediaInterface;
use Symfony\Cmf\Bundle\MediaBundle\MediaManagerInterface;

/**
* Cmf doctrine media adapter
*
* Gaufrette uses a key to identify a file or directory. This adapter uses a
* filesystem path, like /path/to/file/filename.ext, as key.
*
* The abstract method getFilePath is used to get the path for a file or
* directory object. The method mapKeyToId maps a path back to an id.
* The method getFilePath is used to get the path for a file or directory
* object. The method mapKeyToId maps a path back to an id.
*
* If you set the autoFlush flag to false, you will get better performance but
* must ensure that flush is called after all media operations are done.
*/
abstract class AbstractCmfMediaDoctrine implements Adapter,
ChecksumCalculator,
ListKeysAware,
MetadataSupporter
abstract class AbstractCmfMediaDoctrine implements
Adapter,
ChecksumCalculator,
ListKeysAware,
MetadataSupporter
{
protected $managerRegistry;
protected $managerName;
protected $class;
protected $mediaManager;
protected $rootPath;
protected $create;
protected $dirClass;
Expand All @@ -40,24 +47,26 @@ abstract class AbstractCmfMediaDoctrine implements Adapter,
/**
* Constructor
*
* @param ManagerRegistry $registry
* @param string $managerName
* @param string $class fully qualified class name of file
* @param string $rootPath path where the filesystem is located
* @param boolean $create whether to create the directory if
* it does not exist (default FALSE)
* @param string $dirClass fully qualified class name for dirs
* (default NULL: dir is same as file)
* @param string $identifier property used to identify a file and
* lookup (default NULL: let Doctrine
* determine the identifier)
* @param boolean $autoFlush whether to flush write and delete
* actions (default: true)
* @param ManagerRegistry $registry
* @param string $managerName
* @param string $class fully qualified class name of file
* @param MediaManagerInterface $mediaManager
* @param string $rootPath path where the filesystem is located
* @param boolean $create whether to create the directory if
* it does not exist (default FALSE)
* @param string $dirClass fully qualified class name for dirs
* (default NULL: dir is same as file)
* @param string $identifier property used to identify a file and
* lookup (default NULL: let Doctrine
* determine the identifier)
* @param boolean $autoFlush whether to immediately flush write
* and delete actions (default: true)
*/
public function __construct(
ManagerRegistry $registry,
$managerName,
$class,
MediaManagerInterface $mediaManager,
$rootPath = '/',
$create = false,
$dirClass = null,
Expand All @@ -67,6 +76,7 @@ public function __construct(
$this->managerRegistry = $registry;
$this->managerName = $managerName;
$this->class = $class;
$this->mediaManager = $mediaManager;
$this->rootPath = Util\Path::normalize($rootPath);
$this->create = $create;
$this->dirClass = $dirClass;
Expand Down Expand Up @@ -418,7 +428,10 @@ protected function findAll($prefix = '')
*
* @return string
*/
abstract protected function getFilePath(FileInterface $file);
protected function getFilePath(MediaInterface $file)
{
return $this->mediaManager->getPath($file);
}

/**
* Map the key to an id to retrieve the file
Expand All @@ -430,7 +443,10 @@ abstract protected function getFilePath(FileInterface $file);
*
* @return string
*/
abstract protected function mapKeyToId($key);
protected function mapKeyToId($key)
{
return $this->mediaManager->mapPathToId($key);
}

/**
* Computes the key from the specified path
Expand Down
30 changes: 3 additions & 27 deletions Adapter/Gaufrette/PhpcrCmfMediaDoctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,22 @@
namespace Symfony\Cmf\Bundle\MediaBundle\Gaufrette\Adapter;

use PHPCR\Util\PathHelper;
use Symfony\Cmf\Bundle\MediaBundle\FileInterface;

/**
* Phpcr Cmf doctrine media adapter
*
* The path to a file is: /path/to/file/filename.ext
*
* For PHPCR the id is being the path.
*/
class PhpcrCmfMediaDoctrine extends AbstractCmfMediaDoctrine
{
/**
* {@inheritdoc}
*/
protected function getFilePath(FileInterface $file)
{
return $file->getId();
}

/**
* {@inheritdoc}
*/
protected function mapKeyToId($key)
{
return $this->computePath($key);
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
protected function getParentPath($path)
{
return PathHelper::getParentPath($path);
}

/**
* {@inheritdoc}
* {@inheritDoc}
*/
protected function getBaseName($path)
{
return PathHelper::getNodeName($path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,47 @@
use Symfony\Cmf\Bundle\MediaBundle\BinaryInterface;
use Symfony\Cmf\Bundle\MediaBundle\FileSystemInterface;
use Symfony\Cmf\Bundle\MediaBundle\ImageInterface;
use Symfony\Cmf\Bundle\MediaBundle\MediaManagerInterface;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;

/**
* Cmf doctrine media loader
*
* The path to a file is: /path/to/file/filename.ext
*/
abstract class AbstractCmfMediaDoctrineLoader extends AbstractDoctrineLoader
class CmfMediaDoctrineLoader extends AbstractDoctrineLoader
{
protected $mediaManager;

/**
* Constructor.
*
* @param ImagineInterface $imagine
* @param ManagerRegistry $registry
* @param string $managerName
* @param string $class fully qualified class name of image
* @param ImagineInterface $imagine
* @param ManagerRegistry $registry
* @param string $managerName
* @param MediaManagerInterface $mediaManager
* @param string $class fully qualified class name of image
*/
public function __construct(
ImagineInterface $imagine,
ManagerRegistry $registry,
$managerName,
MediaManagerInterface $mediaManager,
$class = null)
{
$manager = $registry->getManager($managerName);

parent::__construct($imagine, $manager, $class);

$this->mediaManager = $mediaManager;
}

/**
* {@inheritdoc}
*/
protected function mapPathToId($path)
{
return $this->mediaManager->mapUrlSafePathToId($path);
}

/**
Expand Down
22 changes: 0 additions & 22 deletions Adapter/LiipImagine/PhpcrCmfMediaDoctrineLoader.php

This file was deleted.

3 changes: 3 additions & 0 deletions CmfMediaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Cmf\Bundle\MediaBundle;

use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
use Symfony\Cmf\Bundle\MediaBundle\DependencyInjection\Compiler\EditorsCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -15,6 +16,8 @@ public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new EditorsCompilerPass());

if (class_exists('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass')) {
$container->addCompilerPass(
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,59 @@
namespace Symfony\Cmf\Bundle\MediaBundle\Controller;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Cmf\Bundle\MediaBundle\BinaryInterface;
use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelper;
use Symfony\Cmf\Bundle\MediaBundle\FileInterface;
use Symfony\Cmf\Bundle\MediaBundle\FileSystemInterface;
use Symfony\Cmf\Bundle\MediaBundle\MediaManagerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\SecurityContextInterface;

/**
* Controller to handle file downloads for things that have a route
* Controller to handle file downloads, uploads and other things that have a route
*/
abstract class AbstractDownloadController
class FileController
{
protected $managerRegistry;
protected $managerName;
protected $class;
protected $rootPath;
protected $mediaManager;
protected $uploadFileHelper;
protected $requiredUploadRole;
protected $securityContext;

/**
* @param ManagerRegistry $registry
* @param string $managerName
* @param string $class fully qualified class name of file
* @param string $rootPath path where the filesystem is located
* @param ManagerRegistry $registry
* @param string $managerName
* @param string $class fully qualified class name of file
* @param string $rootPath path where the filesystem is located
* @param MediaManagerInterface $mediaManager
*/
public function __construct(ManagerRegistry $registry, $managerName, $class, $rootPath = '/')
public function __construct(
ManagerRegistry $registry,
$managerName,
$class,
$rootPath = '/',
MediaManagerInterface $mediaManager,
UploadFileHelper $uploadFileHelper,
$requiredUploadRole,
SecurityContextInterface $securityContext = null)
{
$this->managerRegistry = $registry;
$this->managerName = $managerName;
$this->class = $class === '' ? null : $class;
$this->rootPath = $rootPath;
$this->managerRegistry = $registry;
$this->managerName = $managerName;
$this->class = $class === '' ? null : $class;
$this->rootPath = $rootPath;
$this->mediaManager = $mediaManager;
$this->uploadFileHelper = $uploadFileHelper;
$this->requiredUploadRole = $requiredUploadRole;
$this->securityContext = $securityContext;
}

/**
Expand Down Expand Up @@ -72,34 +95,33 @@ public function setRootPath($rootPath)
* Get the object manager from the registry, based on the current
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typehint should be $path

* managerName
*
* @return \Doctrine\Common\Persistence\ObjectManager
* @return ObjectManager
*/
protected function getObjectManager()
{
return $this->managerRegistry->getManager($this->managerName);
}

/**
* Map the requested path (ie. subpath in the URL) to an id that can
* be used to lookup the file in the Doctrine store.
* Action to download a file object that has a route
*
* @param string $path
*
* @return string
*/
abstract protected function mapPathToId($path);

/**
* Action to download a document that has a route
*
* @param string $id
*/
public function downloadAction($path)
{
$contentDocument = $this->getObjectManager()->find($this->class, $this->mapPathToId($path));
try {
$id = $this->mediaManager->mapUrlSafePathToId($path, $this->rootPath);
} catch (\OutOfBoundsException $e) {
throw new NotFoundHttpException($e->getMessage());
}

$contentDocument = $this->getObjectManager()->find($this->class, $id);

if (! $contentDocument || ! $contentDocument instanceof FileInterface) {
throw new NotFoundHttpException('Content is no file');
throw new NotFoundHttpException(sprintf(
'Object with identifier %s cannot be resolved to a valid instance of Symfony\Cmf\Bundle\MediaBundle\FileInterface',
$path
));
}

$file = false;
Expand Down Expand Up @@ -128,4 +150,19 @@ public function downloadAction($path)

return $response;
}

/**
* Action to upload a file
*
* @param Request $request
* @return Response
*/
public function uploadAction(Request $request)
{
if ($this->securityContext && false === $this->securityContext->isGranted($this->requiredUploadRole)) {
throw new AccessDeniedException();
}

return $this->uploadFileHelper->getUploadResponse($request);
}
}
Loading