-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2eacdee
commit 0f7802c
Showing
24 changed files
with
1,740 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
|
||
namespace Nines\MediaBundle\Controller; | ||
|
||
|
||
use Nines\MediaBundle\Entity\Image; | ||
use Nines\MediaBundle\Entity\ImageContainerInterface; | ||
use Nines\MediaBundle\Form\ImageType; | ||
use Nines\MediaBundle\Services\AbstractFileManager; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\Form\Extension\Core\Type\FileType; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
abstract class AbstractImageController extends AbstractController { | ||
|
||
public function newImageAction(Request $request, ImageContainerInterface $container, $route) { | ||
$image = new Image(); | ||
$form = $this->createForm(ImageType::class, $image); | ||
$form->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
$image->setEntity($container); | ||
$entityManager = $this->getDoctrine()->getManager(); | ||
$entityManager->persist($image); | ||
$entityManager->flush(); | ||
$this->addFlash('success', 'The new image has been saved.'); | ||
|
||
return $this->redirectToRoute($route, ['id' => $container->getId()]); | ||
} | ||
|
||
return [ | ||
'image' => $image, | ||
'form' => $form->createView(), | ||
'entity' => $container, | ||
]; | ||
} | ||
|
||
public function editImageAction(Request $request, ImageContainerInterface $container, Image $image, $route) { | ||
if( ! $container->hasImage($image)) { | ||
throw new NotFoundHttpException("That image is not associated."); | ||
} | ||
|
||
$size = AbstractFileManager::getMaxUploadSize(false); | ||
$form = $this->createForm(ImageType::class, $image); | ||
$form->remove('imageFile'); | ||
$form->add('newImageFile', FileType::class, [ | ||
'label' => 'Replacement Image', | ||
'required' => false, | ||
'attr' => [ | ||
'help_block' => "Select a file to upload which is less than {$size} in size.", | ||
'data-maxsize' => AbstractFileManager::getMaxUploadSize(true), | ||
], | ||
'mapped' => false, | ||
]); | ||
|
||
$form->handleRequest($request); | ||
if($form->isSubmitted() && $form->isValid()) { | ||
if (($upload = $form->get('newImageFile')->getData())) { | ||
$image->setImageFile($upload); | ||
$image->preUpdate(); // force doctrine to update. | ||
} | ||
$entityManager = $this->getDoctrine()->getManager(); | ||
$entityManager->flush(); | ||
$this->addFlash('success', 'The image has been updated.'); | ||
return $this->redirectToRoute($route, ['id' => $container->getId()]); | ||
} | ||
|
||
return [ | ||
'image' => $image, | ||
'form' => $form->createView(), | ||
'entity' => $container, | ||
]; | ||
} | ||
|
||
public function deleteImageAction(Request $request, ImageContainerInterface $container, Image $image, $route) { | ||
if ( ! $this->isCsrfTokenValid('delete' . $image->getId(), $request->request->get('_token'))) { | ||
$this->addFlash('warning', 'Invalid security token.'); | ||
return $this->redirectToRoute($route, ['id' => $container->getId()]); | ||
} | ||
if( ! $container->hasImage($image)) { | ||
throw new NotFoundHttpException("That image is not associated."); | ||
} | ||
$entityManager = $this->getDoctrine()->getManager(); | ||
$container->removeImage($image); | ||
$entityManager->remove($image); | ||
$entityManager->flush(); | ||
$this->addFlash('success', 'The image has been removed.'); | ||
return $this->redirectToRoute($route, ['id' => $container->getId()]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
namespace Nines\MediaBundle\Controller; | ||
|
||
use Nines\MediaBundle\Entity\Image; | ||
use Nines\MediaBundle\Form\ImageType; | ||
use Nines\MediaBundle\Repository\ImageRepository; | ||
|
||
use Nines\MediaBundle\Services\ImageManager; | ||
use Knp\Bundle\PaginatorBundle\Definition\PaginatorAwareInterface; | ||
use Nines\UtilBundle\Controller\PaginatorTrait; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; | ||
|
||
/** | ||
* @Route("/image") | ||
*/ | ||
class ImageController extends AbstractController implements PaginatorAwareInterface | ||
{ | ||
use PaginatorTrait; | ||
|
||
/** | ||
* @Route("/", name="image_index", methods={"GET"}) | ||
* @param Request $request | ||
* @param ImageRepository $imageRepository | ||
* | ||
* @Template() | ||
* | ||
* @return array | ||
*/ | ||
public function index(Request $request, ImageRepository $imageRepository) : array | ||
{ | ||
$query = $imageRepository->indexQuery(); | ||
$pageSize = $this->getParameter('page_size'); | ||
$page = $request->query->getint('page', 1); | ||
|
||
return [ | ||
'images' => $this->paginator->paginate($query, $page, $pageSize), | ||
]; | ||
} | ||
|
||
/** | ||
* @Route("/search", name="image_search", methods={"GET"}) | ||
* | ||
* @Template() | ||
* | ||
* @return array | ||
*/ | ||
public function search(Request $request, ImageRepository $imageRepository) { | ||
$q = $request->query->get('q'); | ||
if ($q) { | ||
$query = $imageRepository->searchQuery($q); | ||
$images = $this->paginator->paginate($query, $request->query->getInt('page', 1), $this->getParameter('page_size'), array('wrap-queries'=>true)); | ||
} else { | ||
$images = []; | ||
} | ||
|
||
return [ | ||
'images' => $images, | ||
'q' => $q, | ||
]; | ||
} | ||
|
||
/** | ||
* @Route("/typeahead", name="image_typeahead", methods={"GET"}) | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function typeahead(Request $request, ImageRepository $imageRepository) { | ||
$q = $request->query->get('q'); | ||
if ( ! $q) { | ||
return new JsonResponse([]); | ||
} | ||
$data = []; | ||
foreach ($imageRepository->typeaheadQuery($q) as $result) { | ||
$data[] = [ | ||
'id' => $result->getId(), | ||
'text' => (string)$result, | ||
]; | ||
} | ||
|
||
return new JsonResponse($data); | ||
} | ||
|
||
/** | ||
* @Route("/{id}", name="image_show", methods={"GET"}) | ||
* @Template() | ||
* @param Image $image | ||
* | ||
* @param ImageManager $manager | ||
* | ||
* @return array | ||
*/ | ||
public function show(Image $image, ImageManager $manager) { | ||
return [ | ||
'image' => $image, | ||
'manager' => $manager | ||
]; | ||
} | ||
|
||
/** | ||
* @Route("/{id}/view", name="image_view", methods={"GET"}) | ||
* @param Image $image | ||
* | ||
* @return BinaryFileResponse | ||
*/ | ||
public function view(Image $image) { | ||
if ( ! $image->getPublic() && ! $this->getUser()) { | ||
throw new AccessDeniedHttpException(); | ||
} | ||
return new BinaryFileResponse($image->getImageFile()); | ||
} | ||
|
||
/** | ||
* @Route("/{id}/thumb", name="image_thumb", methods={"GET"}) | ||
* @param Image $image | ||
* | ||
* @return BinaryFileResponse | ||
*/ | ||
public function thumbnail(Image $image) { | ||
if ( ! $image->getPublic() && ! $this->getUser()) { | ||
throw new AccessDeniedHttpException(); | ||
} | ||
|
||
return new BinaryFileResponse($image->getThumbFile()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* (c) 2020 Michael Joyce <mjoyce@sfu.ca> | ||
* This source file is subject to the GPL v2, bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Nines\MediaBundle\Controller; | ||
|
||
use Nines\MediaBundle\Entity\Link; | ||
use Nines\MediaBundle\Repository\LinkRepository; | ||
use Knp\Bundle\PaginatorBundle\Definition\PaginatorAwareInterface; | ||
use Nines\UtilBundle\Controller\PaginatorTrait; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
/** | ||
* @Route("/link") | ||
* @IsGranted("ROLE_ADMIN") | ||
*/ | ||
class LinkController extends AbstractController implements PaginatorAwareInterface { | ||
use PaginatorTrait; | ||
|
||
/** | ||
* @Route("/", name="link_index", methods={"GET"}) | ||
* | ||
* @Template() | ||
*/ | ||
public function index(Request $request, LinkRepository $linkRepository) : array { | ||
$query = $linkRepository->indexQuery(); | ||
$pageSize = $this->getParameter('page_size'); | ||
$page = $request->query->getint('page', 1); | ||
|
||
return [ | ||
'links' => $this->paginator->paginate($query, $page, $pageSize), | ||
]; | ||
} | ||
|
||
/** | ||
* @Route("/search", name="link_search", methods={"GET"}) | ||
* | ||
* @Template() | ||
* | ||
* @return array | ||
*/ | ||
public function search(Request $request, LinkRepository $linkRepository) { | ||
$q = $request->query->get('q'); | ||
if ($q) { | ||
$query = $linkRepository->searchQuery($q); | ||
$links = $this->paginator->paginate($query, $request->query->getInt('page', 1), $this->getParameter('page_size'), ['wrap-queries' => true]); | ||
} else { | ||
$links = []; | ||
} | ||
|
||
return [ | ||
'links' => $links, | ||
'q' => $q, | ||
]; | ||
} | ||
|
||
/** | ||
* @Route("/{id}", name="link_show", methods={"GET"}) | ||
* @Template() | ||
* | ||
* @return array | ||
*/ | ||
public function show(Link $link) { | ||
return [ | ||
'link' => $link, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Nines\MediaBundle\DataFixtures; | ||
|
||
use Nines\MediaBundle\Entity\Image; | ||
use Doctrine\Bundle\FixturesBundle\Fixture; | ||
use Doctrine\Persistence\ObjectManager; | ||
use Imagick; | ||
use ImagickPixel; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
||
class ImageFixtures extends Fixture { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function load(ObjectManager $em) { | ||
for ($i = 0; $i < 4; $i++) { | ||
|
||
$image = new Imagick(); | ||
$hue = $i * 20; | ||
$image->newImage(640,480,new ImagickPixel("hsb({$hue}%, 100%, 75%)")); | ||
$image->setImageFormat('png'); | ||
$tmp = tmpfile(); | ||
fwrite($tmp, $image->getImageBlob()); | ||
$upload = new UploadedFile(stream_get_meta_data($tmp)['uri'], "image_{$i}.png", 'image/png', null, true); | ||
|
||
$fixture = new Image(); | ||
$fixture->setImageFile($upload); | ||
$fixture->setPublic($i % 2 == 0); | ||
$fixture->setOriginalName('OriginalName ' . $i); | ||
$fixture->setImagePath('ImagePath ' . $i); | ||
$fixture->setThumbPath('ThumbPath ' . $i); | ||
$fixture->setImageSize($i); | ||
$fixture->setImageWidth($i); | ||
$fixture->setImageHeight($i); | ||
$fixture->setDescription("<p>This is paragraph ${i}</p>"); | ||
$fixture->setLicense("<p>This is paragraph ${i}</p>"); | ||
$fixture->setEntity('stdClass:' . $i); | ||
|
||
$em->persist($fixture); | ||
$this->setReference('image.' . $i, $fixture); | ||
} | ||
$em->flush(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.