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
|
|
|
|
87
|
|
|
if($user instanceof AnonymousUser){ |
|
|
|
|
88
|
|
|
$metadata->getPermissions()->grant($user, PermissionsInterface::PERMISSION_ALL); |
89
|
|
|
}else{ |
90
|
|
|
$this->dm->getUnitOfWork()->registerManaged($user, $user->getId(), []); |
91
|
|
|
|
92
|
|
|
$metadata->setUser($user); |
93
|
|
|
$this->dm->persist($user); |
94
|
|
|
$this->dm->flush(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$options = new UploadOptions(); |
99
|
|
|
$options->metadata = $metadata; |
100
|
|
|
|
101
|
|
|
return $repo->uploadFromFile($source, $fileName, $options); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $entityClass |
106
|
|
|
* @param FileMetadataInterface $metadata |
107
|
|
|
* @param string $fileName |
108
|
|
|
* @param resource $stream |
109
|
|
|
* @return object|FileInterface|ImageInterface |
110
|
|
|
*/ |
111
|
|
|
public function uploadFromStream(string $entityClass, FileMetadataInterface $metadata, string $fileName, $stream): object |
112
|
|
|
{ |
113
|
|
|
$repo = $this->getRepository($entityClass); |
114
|
|
|
$options = new UploadOptions(); |
115
|
|
|
$options->metadata = $metadata; |
116
|
|
|
|
117
|
|
|
return $repo->uploadFromStream($fileName, $stream, $options); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function remove(FileInterface $file, $andFlush = false) |
121
|
|
|
{ |
122
|
|
|
$dm = $this->dm; |
123
|
|
|
$events = $dm->getEventManager(); |
124
|
|
|
|
125
|
|
|
$dm->remove($file); |
126
|
|
|
$events->hasListeners('postRemoveEntity') && |
127
|
|
|
$events->dispatchEvent('postRemoveEntity', new LifecycleEventArgs($file, $dm)); |
128
|
|
|
|
129
|
|
|
if($andFlush){ |
130
|
|
|
$dm->flush(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $entityClass |
136
|
|
|
* @return ObjectRepository|GridFSRepository |
137
|
|
|
*/ |
138
|
|
|
private function getRepository(string $entityClass) |
139
|
|
|
{ |
140
|
|
|
return $this->dm->getRepository($entityClass); |
141
|
|
|
} |
142
|
|
|
} |