1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Repository; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Asset; |
10
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
11
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
12
|
|
|
use League\Flysystem\FilesystemOperator; |
13
|
|
|
use PhpZip\ZipFile; |
14
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
15
|
|
|
use Symfony\Component\Routing\RouterInterface; |
16
|
|
|
use Vich\UploaderBundle\Storage\FlysystemStorage; |
17
|
|
|
use Vich\UploaderBundle\Templating\Helper\UploaderHelper; |
18
|
|
|
|
19
|
|
|
class AssetRepository extends ServiceEntityRepository |
20
|
|
|
{ |
21
|
|
|
protected RouterInterface $router; |
22
|
|
|
protected UploaderHelper $uploaderHelper; |
23
|
|
|
protected FilesystemOperator $filesystem; |
24
|
|
|
protected FlysystemStorage $storage; |
25
|
|
|
|
26
|
|
|
public function __construct(ManagerRegistry $registry, RouterInterface $router, UploaderHelper $uploaderHelper, FilesystemOperator $assetFilesystem, FlysystemStorage $storage) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($registry, Asset::class); |
29
|
|
|
$this->router = $router; |
30
|
|
|
$this->uploaderHelper = $uploaderHelper; |
31
|
|
|
// Flysystem mount name is saved in config/packages/oneup_flysystem.yaml |
32
|
|
|
$this->filesystem = $assetFilesystem; |
33
|
|
|
$this->storage = $storage; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getStorage() |
37
|
|
|
{ |
38
|
|
|
return $this->storage; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getFileSystem() |
42
|
|
|
{ |
43
|
|
|
return $this->filesystem; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/*public function getUploaderHelper(): UploaderHelper |
47
|
|
|
{ |
48
|
|
|
return $this->uploaderHelper; |
49
|
|
|
}*/ |
50
|
|
|
|
51
|
|
|
public function unZipFile(Asset $asset, ZipFile $zipFile): void |
52
|
|
|
{ |
53
|
|
|
$folder = '/'.$asset->getCategory().'/'.$asset->getTitle(); |
54
|
|
|
|
55
|
|
|
$fs = $this->getFileSystem(); |
56
|
|
|
|
57
|
|
|
if ($fs->fileExists($folder)) { |
58
|
|
|
$list = $zipFile->getEntries(); |
59
|
|
|
foreach ($list as $item) { |
60
|
|
|
$name = $item->getName(); |
61
|
|
|
if ($fs->fileExists($folder.'/'.$name)) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($item->isDirectory()) { |
66
|
|
|
$fs->createDirectory($folder.'/'.$name); |
67
|
|
|
|
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$content = $zipFile->getEntryContents($name); |
72
|
|
|
$fs->write($folder.'/'.$name, $content); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getFolder(Asset $asset): ?string |
78
|
|
|
{ |
79
|
|
|
if ($asset->hasFile()) { |
80
|
|
|
$file = $asset->getTitle(); |
81
|
|
|
|
82
|
|
|
return '/'.$asset->getCategory().'/'.$file.'/'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getAssetUrl(Asset $asset) |
89
|
|
|
{ |
90
|
|
|
if (Asset::SCORM === $asset->getCategory()) { |
91
|
|
|
$params = [ |
92
|
|
|
'category' => $asset->getCategory(), |
93
|
|
|
'path' => $asset->getTitle(), |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
return $this->router->generate('chamilo_core_asset_showfile', $params); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Classic. |
100
|
|
|
$helper = $this->uploaderHelper; |
101
|
|
|
|
102
|
|
|
$cropFilter = ''; |
103
|
|
|
$crop = $asset->getCrop(); |
104
|
|
|
if (!empty($crop)) { |
105
|
|
|
$cropFilter = '?crop='.$crop; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return '/assets'.$helper->asset($asset).$cropFilter; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function createFromRequest(Asset $asset, $file): Asset |
112
|
|
|
{ |
113
|
|
|
if (isset($file['tmp_name']) && !empty($file['tmp_name'])) { |
114
|
|
|
$mimeType = mime_content_type($file['tmp_name']); |
115
|
|
|
$file = new UploadedFile($file['tmp_name'], $asset->getTitle(), $mimeType, null, true); |
116
|
|
|
$asset->setFile($file); |
117
|
|
|
$this->getEntityManager()->persist($asset); |
118
|
|
|
$this->getEntityManager()->flush(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $asset; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/*public function getFileContent(Asset $asset): string |
125
|
|
|
{ |
126
|
|
|
try { |
127
|
|
|
if ($asset->hasFile()) { |
128
|
|
|
$file = $asset->getFile(); |
129
|
|
|
$fileName = $this->getFilename($file); |
130
|
|
|
|
131
|
|
|
return $this->getFileSystem()->read($fileName); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return ''; |
135
|
|
|
} catch (\Throwable $exception) { |
136
|
|
|
throw new FileNotFoundException($asset); |
137
|
|
|
} |
138
|
|
|
}*/ |
139
|
|
|
|
140
|
|
|
/*public function getFileStream(Asset $asset) |
141
|
|
|
{ |
142
|
|
|
try { |
143
|
|
|
if ($asset->hasFile()) { |
144
|
|
|
$file = $asset->getFile(); |
145
|
|
|
$fileName = $this->getFilename($file); |
146
|
|
|
|
147
|
|
|
return $this->getFileSystem()->readStream($fileName); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return ''; |
151
|
|
|
} catch (\Throwable $exception) { |
152
|
|
|
throw new FileNotFoundException($asset); |
153
|
|
|
} |
154
|
|
|
}*/ |
155
|
|
|
|
156
|
|
|
public function delete(Asset $asset = null): void |
157
|
|
|
{ |
158
|
|
|
if (null !== $asset) { |
159
|
|
|
$this->getEntityManager()->remove($asset); |
160
|
|
|
$this->getEntityManager()->flush(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|