|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Core\Service; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Auth\AuthenticationService; |
|
9
|
|
|
use Auth\Entity\AnonymousUser; |
|
10
|
|
|
use Auth\Entity\UserImage; |
|
11
|
|
|
use Core\Entity\FileInterface; |
|
12
|
|
|
use Core\Entity\FileMetadataInterface; |
|
13
|
|
|
use Core\Entity\ImageInterface; |
|
14
|
|
|
use Core\Entity\PermissionsInterface; |
|
15
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
16
|
|
|
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; |
|
17
|
|
|
use Imagine\Image\ImageInterface as ImagineImage; |
|
18
|
|
|
use Doctrine\ODM\MongoDB\Repository\GridFSRepository; |
|
19
|
|
|
use Doctrine\ODM\MongoDB\Repository\UploadOptions; |
|
20
|
|
|
use Doctrine\Persistence\ObjectRepository; |
|
21
|
|
|
use Imagine\Image\ImagineInterface; |
|
22
|
|
|
use Psr\Container\ContainerInterface; |
|
23
|
|
|
|
|
24
|
|
|
class FileManager |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var DocumentManager |
|
28
|
|
|
*/ |
|
29
|
|
|
private DocumentManager $dm; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AuthenticationService |
|
33
|
|
|
*/ |
|
34
|
|
|
private AuthenticationService $auth; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
DocumentManager $dm, |
|
38
|
|
|
AuthenticationService $auth |
|
39
|
|
|
) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->dm = $dm; |
|
42
|
|
|
$this->auth = $auth; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function factory(ContainerInterface $container): self |
|
46
|
|
|
{ |
|
47
|
|
|
$dm = $container->get(DocumentManager::class); |
|
48
|
|
|
$auth = $container->get('AuthenticationService'); |
|
49
|
|
|
return new FileManager($dm, $auth); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $entityClass |
|
54
|
|
|
* @param string $id |
|
55
|
|
|
* @return object|null|FileInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function findByID(string $entityClass, string $id) |
|
58
|
|
|
{ |
|
59
|
|
|
$repo = $this->getRepository($entityClass); |
|
60
|
|
|
return $repo->find($id); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param FileInterface $file |
|
65
|
|
|
* @return resource |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getStream(FileInterface $file) |
|
68
|
|
|
{ |
|
69
|
|
|
$repo = $this->getRepository(get_class($file)); |
|
70
|
|
|
return $repo->openDownloadStream($file->getId()); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getContents(FileInterface $file): string |
|
74
|
|
|
{ |
|
75
|
|
|
$repo = $this->getRepository(get_class($file)); |
|
76
|
|
|
$stream = $repo->openDownloadStream($file->getId()); |
|
77
|
|
|
return stream_get_contents($stream); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function uploadFromFile(string $entityClass, FileMetadataInterface $metadata, string $source, ?string $fileName = null): object |
|
81
|
|
|
{ |
|
82
|
|
|
$repo = $this->getRepository($entityClass); |
|
83
|
|
|
|
|
84
|
|
|
if(UserImage::class !== $entityClass && is_null($metadata->getUser())){ |
|
85
|
|
|
$user = $this->auth->getUser(); |
|
86
|
|
|
if($user instanceof AnonymousUser){ |
|
|
|
|
|
|
87
|
|
|
$metadata->getPermissions()->grant($user, PermissionsInterface::PERMISSION_ALL); |
|
88
|
|
|
}else{ |
|
89
|
|
|
$metadata->setUser($user); |
|
90
|
|
|
$this->dm->persist($user); |
|
91
|
|
|
$this->dm->flush(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$options = new UploadOptions(); |
|
96
|
|
|
$options->metadata = $metadata; |
|
97
|
|
|
|
|
98
|
|
|
return $repo->uploadFromFile($source, $fileName, $options); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $entityClass |
|
103
|
|
|
* @param FileMetadataInterface $metadata |
|
104
|
|
|
* @param string $fileName |
|
105
|
|
|
* @param resource $stream |
|
106
|
|
|
* @return object|FileInterface|ImageInterface |
|
107
|
|
|
*/ |
|
108
|
|
|
public function uploadFromStream(string $entityClass, FileMetadataInterface $metadata, string $fileName, $stream): object |
|
109
|
|
|
{ |
|
110
|
|
|
$repo = $this->getRepository($entityClass); |
|
111
|
|
|
$options = new UploadOptions(); |
|
112
|
|
|
$options->metadata = $metadata; |
|
113
|
|
|
|
|
114
|
|
|
return $repo->uploadFromStream($fileName, $stream, $options); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function remove(FileInterface $file, $andFlush = false) |
|
118
|
|
|
{ |
|
119
|
|
|
$dm = $this->dm; |
|
120
|
|
|
$events = $dm->getEventManager(); |
|
121
|
|
|
|
|
122
|
|
|
$dm->remove($file); |
|
123
|
|
|
$events->hasListeners('postRemoveEntity') && |
|
124
|
|
|
$events->dispatchEvent('postRemoveEntity', new LifecycleEventArgs($file, $dm)); |
|
125
|
|
|
|
|
126
|
|
|
if($andFlush){ |
|
127
|
|
|
$dm->flush(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $entityClass |
|
133
|
|
|
* @return ObjectRepository|GridFSRepository |
|
134
|
|
|
*/ |
|
135
|
|
|
private function getRepository(string $entityClass) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->dm->getRepository($entityClass); |
|
138
|
|
|
} |
|
139
|
|
|
} |