Skip to content

Commit ef493d6

Browse files
author
René Hrdina
committed
fix #9
1 parent d1499c6 commit ef493d6

File tree

6 files changed

+135
-16
lines changed

6 files changed

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

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: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Netgen\Bundle\EnhancedBinaryFileBundle\FieldHandler;
46

7+
use DOMDocument;
58
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
69
use eZ\Publish\Core\FieldType\Value;
7-
use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Value as EnhancedBinaryFileValue;
810
use eZ\Publish\Core\IO\IOServiceInterface;
11+
use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Value as EnhancedBinaryFileValue;
912
use Netgen\Bundle\InformationCollectionBundle\FieldHandler\Custom\CustomLegacyFieldHandlerInterface;
1013
use Netgen\Bundle\InformationCollectionBundle\Value\LegacyData;
11-
use DOMDocument;
1214

1315
class EnhancedBinaryFileHandler implements CustomLegacyFieldHandlerInterface
1416
{
@@ -28,23 +30,23 @@ public function __construct(IOServiceInterface $IOService)
2830
}
2931

3032
/**
31-
* @inheritDoc
33+
* {@inheritdoc}
3234
*/
3335
public function supports(Value $value)
3436
{
3537
return $value instanceof EnhancedBinaryFileValue;
3638
}
3739

3840
/**
39-
* @inheritDoc
41+
* {@inheritdoc}
4042
*/
4143
public function toString(Value $value, FieldDefinition $fieldDefinition)
4244
{
43-
return (string)$value;
45+
return (string) $value;
4446
}
4547

4648
/**
47-
* @inheritDoc
49+
* {@inheritdoc}
4850
*/
4951
public function getLegacyValue(Value $value, FieldDefinition $fieldDefinition)
5052
{
@@ -58,7 +60,7 @@ public function getLegacyValue(Value $value, FieldDefinition $fieldDefinition)
5860

5961
/**
6062
* Create XML doc string
61-
* and save file to filesystem
63+
* and save file to filesystem.
6264
*
6365
* @param EnhancedBinaryFileValue $value
6466
* @param FieldDefinition $fieldDefinition
@@ -69,19 +71,19 @@ protected function store(EnhancedBinaryFileValue $value, FieldDefinition $fieldD
6971
{
7072
$binaryFile = $this->storeBinaryFileToPath($value);
7173

72-
$doc = new DOMDocument( '1.0', 'utf-8' );
73-
$root = $doc->createElement( 'binaryfile-info' );
74-
$binaryFileList = $doc->createElement( 'binaryfile-attributes' );
74+
$doc = new DOMDocument('1.0', 'utf-8');
75+
$root = $doc->createElement('binaryfile-info');
76+
$binaryFileList = $doc->createElement('binaryfile-attributes');
7577

7678
$fileInfo = [
7779
'Filename' => htmlentities($binaryFile->uri),
7880
'OriginalFilename' => htmlentities($value->fileName),
7981
'Size' => $value->fileSize,
8082
];
8183

82-
foreach($fileInfo as $key => $binaryFileItem) {
84+
foreach ($fileInfo as $key => $binaryFileItem) {
8385
$binaryFileElement = $doc->createElement($key, $binaryFileItem);
84-
$binaryFileList->appendChild( $binaryFileElement );
86+
$binaryFileList->appendChild($binaryFileElement);
8587
}
8688

8789
$root->appendChild($binaryFileList);
@@ -91,7 +93,7 @@ protected function store(EnhancedBinaryFileValue $value, FieldDefinition $fieldD
9193
}
9294

9395
/**
94-
* Stores file to filesystem
96+
* Stores file to filesystem.
9597
*
9698
* @param EnhancedBinaryFileValue $value
9799
* @param string $storagePrefix
@@ -102,12 +104,12 @@ protected function storeBinaryFileToPath(EnhancedBinaryFileValue $value, $storag
102104
{
103105
$binaryCreateStruct = $this->IOService
104106
->newBinaryCreateStructFromLocalFile($value->inputUri);
105-
$binaryCreateStruct->id = $storagePrefix . $value->fileName;
107+
108+
$encodedFilename = uniqid();
109+
$binaryCreateStruct->id = $storagePrefix . $encodedFilename;
106110

107111
$binaryFile = $this->IOService->createBinaryFile($binaryCreateStruct);
108112

109113
return $binaryFile;
110114
}
111115
}
112-
113-
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
netgen_enhancedezbinaryfile.route.download_binary_file:
2+
path: /netgen/enhancedezbinaryfile/download/{infocollectionAttributeId}
3+
defaults: { _controller: netgen_enhancedezbinaryfile.controller.download:downloadCollectedEnhancedEzBinaryFileAction }
4+
methods: [GET]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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"

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)