Skip to content

Commit 75e2b61

Browse files
authored
Merge pull request #11 from darinda/fix-issue-9
Fix issue 9
2 parents d1499c6 + b4e3d35 commit 75e2b61

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Netgen\Bundle\EnhancedBinaryFileBundle\Controller;
4+
5+
use eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse;
6+
use eZ\Publish\Core\IO\IOServiceInterface;
7+
use eZ\Publish\Core\SignalSlot\Repository;
8+
use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollection;
9+
use Netgen\Bundle\InformationCollectionBundle\Entity\EzInfoCollectionAttribute;
10+
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionAttributeRepository;
11+
use Netgen\Bundle\InformationCollectionBundle\Repository\EzInfoCollectionRepository;
12+
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
13+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
14+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15+
16+
class DownloadController
17+
{
18+
use ControllerTrait;
19+
20+
private $infocollectionAttributeRepository;
21+
22+
private $infocollectionRepository;
23+
24+
private $ioService;
25+
26+
private $repository;
27+
28+
public function __construct(
29+
EzInfoCollectionAttributeRepository $infocollectionAttributeRepository,
30+
EzInfoCollectionRepository $infocollectionRepository,
31+
IOServiceInterface $ioService,
32+
Repository $repository
33+
) {
34+
$this->infocollectionAttributeRepository = $infocollectionAttributeRepository;
35+
$this->infocollectionRepository = $infocollectionRepository;
36+
$this->ioService = $ioService;
37+
$this->repository = $repository;
38+
}
39+
40+
/**
41+
* @param int $infocollectionAttributeId
42+
*
43+
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
44+
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
45+
*
46+
* @return BinaryStreamResponse
47+
*/
48+
public function downloadCollectedEnhancedEzBinaryFileAction($infocollectionAttributeId)
49+
{
50+
/** @var EzInfoCollectionAttribute $infocollectionAttribute */
51+
$infocollectionAttribute = $this->infocollectionAttributeRepository->find($infocollectionAttributeId);
52+
53+
if ($infocollectionAttribute === null) {
54+
throw new \InvalidArgumentException(
55+
"Information collection attribute with id '#{$infocollectionAttributeId}'' could not be found"
56+
);
57+
}
58+
59+
/** @var EzInfoCollection $infocollection */
60+
$infocollection = $this->infocollectionRepository->find($infocollectionAttribute->getInformationCollectionId());
61+
62+
$contentId = $infocollection->getContentObjectId();
63+
$content = $this->repository->getContentService()->loadContent($contentId);
64+
65+
if (!$this->repository->getPermissionResolver()->canUser('infocollector', 'read', $content)) {
66+
throw new AccessDeniedException('Access denied.');
67+
}
68+
69+
$binaryFileXML = $infocollectionAttribute->getDataText();
70+
$doc = new \DOMDocument('1.0', 'utf-8');
71+
$doc->loadXML($binaryFileXML);
72+
73+
$xpath = new \DOMXPath($doc);
74+
$filePathNodes = $xpath->evaluate('/binaryfile-info/binaryfile-attributes/Filename');
75+
$originalFilenameNodes = $xpath->evaluate('/binaryfile-info/binaryfile-attributes/OriginalFilename');
76+
if (!$filePathNodes->length) {
77+
throw new \InvalidArgumentException(
78+
"Information collection attribute with id '#{$infocollectionAttributeId}'' could not be found"
79+
);
80+
}
81+
82+
$filePath = $filePathNodes->item(0)->textContent;
83+
$fileName = basename($filePath);
84+
85+
$originalFilename = $originalFilenameNodes->length ? $originalFilenameNodes->item(0)->textContent : $fileName;
86+
87+
$binaryFile = $this->ioService->loadBinaryFile('collected' . \DIRECTORY_SEPARATOR . $fileName);
88+
89+
$response = new BinaryStreamResponse($binaryFile, $this->ioService);
90+
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $originalFilename);
91+
92+
return $response;
93+
}
94+
}

bundle/DependencyInjection/NetgenEnhancedBinaryFileExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ public function load(array $configs, ContainerBuilder $container)
4747
$loader->load('storage_engines.yml');
4848
$loader->load('mime.yml');
4949
$loader->load('information_collection.yml');
50+
$loader->load('services.yml');
5051
}
5152
}

bundle/FieldHandler/EnhancedBinaryFileHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ protected function storeBinaryFileToPath(EnhancedBinaryFileValue $value, $storag
102102
{
103103
$binaryCreateStruct = $this->IOService
104104
->newBinaryCreateStructFromLocalFile($value->inputUri);
105-
$binaryCreateStruct->id = $storagePrefix . $value->fileName;
105+
$encodedFilename = uniqid();
106+
$binaryCreateStruct->id = $storagePrefix . $encodedFilename;
106107

107108
$binaryFile = $this->IOService->createBinaryFile($binaryCreateStruct);
108109

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
netgen_enhancedezbinaryfile.route.download_binary_file:
2+
path: /netgen/enhancedezbinaryfile/download/{infocollectionAttributeId}
3+
defaults: { _controller: netgen_enhancedezbinaryfile.controller.download:downloadCollectedEnhancedEzBinaryFileAction }
4+
methods: [GET]
5+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
netgen_enhancedezbinaryfile.controller.download:
3+
class: Netgen\Bundle\EnhancedBinaryFileBundle\Controller\DownloadController
4+
arguments:
5+
- "@netgen_information_collection.repository.ez_info_collection_attribute"
6+
- "@netgen_information_collection.repository.ez_info_collection"
7+
- "@ezpublish.fieldType.ezbinaryfile.io_service"
8+
- "@ezpublish.api.repository"
9+

doc/INSTALL.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ $ php app/console cache:clear
4141
```
4242

4343
For more detailed configuration, please check [documentation](DOC.md).
44+
45+
### Include the routing config in your routing.yml
46+
47+
```yml
48+
_netgen_enhancedezbinaryfile:
49+
resource: '@NetgenEnhancedBinaryFileBundle/Resources/config/routing.yml'
50+
```
51+

0 commit comments

Comments
 (0)