|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Netgen\Bundle\EnhancedBinaryFileBundle\FieldHandler; |
| 4 | + |
| 5 | +use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; |
| 6 | +use eZ\Publish\Core\FieldType\Value; |
| 7 | +use Netgen\Bundle\EnhancedBinaryFileBundle\Core\FieldType\EnhancedBinaryFile\Value as EnhancedBinaryFileValue; |
| 8 | +use eZ\Publish\Core\IO\IOServiceInterface; |
| 9 | +use Netgen\Bundle\InformationCollectionBundle\FieldHandler\Custom\CustomLegacyFieldHandlerInterface; |
| 10 | +use Netgen\Bundle\InformationCollectionBundle\Value\LegacyData; |
| 11 | +use DOMDocument; |
| 12 | + |
| 13 | +class EnhancedBinaryFileHandler implements CustomLegacyFieldHandlerInterface |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var IOServiceInterface |
| 17 | + */ |
| 18 | + protected $IOService; |
| 19 | + |
| 20 | + /** |
| 21 | + * EnhancedBinaryFileHandler constructor. |
| 22 | + * |
| 23 | + * @param IOServiceInterface $IOService |
| 24 | + */ |
| 25 | + public function __construct(IOServiceInterface $IOService) |
| 26 | + { |
| 27 | + $this->IOService = $IOService; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritDoc |
| 32 | + */ |
| 33 | + public function supports(Value $value) |
| 34 | + { |
| 35 | + return $value instanceof EnhancedBinaryFileValue; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritDoc |
| 40 | + */ |
| 41 | + public function toString(Value $value, FieldDefinition $fieldDefinition) |
| 42 | + { |
| 43 | + return (string)$value; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + public function getLegacyValue(Value $value, FieldDefinition $fieldDefinition) |
| 50 | + { |
| 51 | + return new LegacyData( |
| 52 | + $fieldDefinition->id, |
| 53 | + 0, |
| 54 | + 0, |
| 55 | + $this->store($value, $fieldDefinition) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create XML doc string |
| 61 | + * and save file to filesystem |
| 62 | + * |
| 63 | + * @param EnhancedBinaryFileValue $value |
| 64 | + * @param FieldDefinition $fieldDefinition |
| 65 | + * |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + protected function store(EnhancedBinaryFileValue $value, FieldDefinition $fieldDefinition) |
| 69 | + { |
| 70 | + $binaryFile = $this->storeBinaryFileToPath($value); |
| 71 | + |
| 72 | + $doc = new DOMDocument( '1.0', 'utf-8' ); |
| 73 | + $root = $doc->createElement( 'binaryfile-info' ); |
| 74 | + $binaryFileList = $doc->createElement( 'binaryfile-attributes' ); |
| 75 | + |
| 76 | + $fileInfo = [ |
| 77 | + 'Filename' => $binaryFile->uri, |
| 78 | + 'OriginalFilename' => $value->fileName, |
| 79 | + 'Size' => $value->fileSize, |
| 80 | + ]; |
| 81 | + |
| 82 | + foreach($fileInfo as $key => $binaryFileItem) { |
| 83 | + $binaryFileElement = $doc->createElement($key, $binaryFileItem); |
| 84 | + $binaryFileList->appendChild( $binaryFileElement ); |
| 85 | + } |
| 86 | + |
| 87 | + $root->appendChild($binaryFileList); |
| 88 | + $doc->appendChild($root); |
| 89 | + |
| 90 | + return $doc->saveXML(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Stores file to filesystem |
| 95 | + * |
| 96 | + * @param EnhancedBinaryFileValue $value |
| 97 | + * @param string $storagePrefix |
| 98 | + * |
| 99 | + * @return \eZ\Publish\Core\IO\Values\BinaryFile |
| 100 | + */ |
| 101 | + protected function storeBinaryFileToPath(EnhancedBinaryFileValue $value, $storagePrefix = '/original/collected/') |
| 102 | + { |
| 103 | + $binaryCreateStruct = $this->IOService |
| 104 | + ->newBinaryCreateStructFromLocalFile($value->inputUri); |
| 105 | + $binaryCreateStruct->id = $storagePrefix . $value->fileName; |
| 106 | + |
| 107 | + $binaryFile = $this->IOService->createBinaryFile($binaryCreateStruct); |
| 108 | + |
| 109 | + return $binaryFile; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
0 commit comments