src/Tus/EventListener/TusListener.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Tus\EventListener;
  3. use App\Entity\UserUpload;
  4. use App\Tus\Service\FileCollectorService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttributable;
  7. use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttributeValue;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Filesystem\Filesystem;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use TusPhp\Events\UploadComplete;
  13. use TusPhp\Events\UploadCreated;
  14. use TusPhp\Events\UploadProgress;
  15. use TusPhp\File;
  16. class TusListener implements EventSubscriberInterface
  17. {
  18. public function __construct(
  19. private KernelInterface $kernel,
  20. private RouterInterface $router,
  21. private FileCollectorService $collectorService,
  22. private EntityManagerInterface $entityManager,
  23. private ?File $file = null
  24. ){}
  25. public static function getSubscribedEvents()
  26. {
  27. return [
  28. UploadComplete::NAME => ['onComplete'],
  29. ];
  30. }
  31. public function onComplete(UploadComplete $event){
  32. $this->file = $event->getFile();
  33. if(!$this->hasRequiredMetaData(['entity_id', 'entity_fqcn', 'property_name'])){
  34. return;
  35. }
  36. $metaData = $this->getMetaData();
  37. $entity = $this->entityManager->getRepository($metaData['entity_fqcn'])->find($metaData['entity_id']);
  38. $fileCollector = $this->collectorService->findCollectorSupports($metaData['entity_fqcn']);
  39. $fileCollector->setProperty($metaData['property_name']);
  40. $fileCollector->setEntity($entity);
  41. $file = $fileCollector->relocate($event->getFile());
  42. $hash = $fileCollector->hashFile($file);
  43. $event->getResponse()->setHeaders([
  44. 'x-file-download' => $this->router->generate('tus_file_action', [
  45. 'hash' => $hash ,
  46. "action" => 'download'
  47. ], 0),
  48. 'x-file-delete' => $this->router->generate('tus_file_action', [
  49. 'hash' => $hash ,
  50. "action" => 'delete'
  51. ], 0),
  52. 'x-file-id' => $hash,
  53. 'x-file-public' => str_replace($this->kernel->getProjectDir().'/public/', "", $file->getRealPath())
  54. ]);
  55. }
  56. private function getTargetDir(){
  57. return $this->getUploadsDir().'/' .$this->getEntityDir();
  58. }
  59. private function getEntityDir()
  60. {
  61. return static::getEntityDirFromMeta(
  62. $this->getMetaData('entity_fqcn'),
  63. $this->getMetaData('property_name'),
  64. $this->getMetaData('entity_id')
  65. );
  66. }
  67. public static function getEntityDirFromMeta($class, $prop, $entityId){
  68. if(!$class && !$prop && !$entityId) {return ''; }
  69. return sprintf("%s/%s/%s",
  70. strtolower((new \ReflectionClass($class))->getShortName()), // class
  71. md5($entityId), // id
  72. $prop
  73. );
  74. }
  75. private function getUploadsDir()
  76. {
  77. return $this->kernel->getProjectDir().'/public/uploads';
  78. }
  79. private function getMetaData($key=null)
  80. {
  81. $meta = $this->file->details()['metadata'];
  82. if($key === null) {
  83. return $meta;
  84. }
  85. if(array_key_exists($key, $meta)){
  86. return $meta[$key];
  87. }
  88. return null;
  89. }
  90. private function hasRequiredMetaData(array $keys){
  91. $exists = array_filter($keys, function($key) {
  92. return !!$this->getMetaData($key);
  93. });
  94. return count($keys) === count($exists);
  95. }
  96. }