|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Entity\Asset; |
|
8
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
9
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
10
|
|
|
use League\Flysystem\FilesystemInterface; |
|
11
|
|
|
use League\Flysystem\MountManager; |
|
12
|
|
|
use League\Flysystem\ZipArchive\ZipArchiveAdapter; |
|
13
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
|
14
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
15
|
|
|
|
|
16
|
|
|
class AssetRepository extends ServiceEntityRepository |
|
17
|
|
|
{ |
|
18
|
|
|
protected $mountManager; |
|
19
|
|
|
protected $storage; |
|
20
|
|
|
protected $router; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(ManagerRegistry $registry, RouterInterface $router, MountManager $mountManager) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct($registry, Asset::class); |
|
25
|
|
|
$this->router = $router; |
|
26
|
|
|
$this->mountManager = $mountManager; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return FilesystemInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getFileSystem() |
|
33
|
|
|
{ |
|
34
|
|
|
// Flysystem mount name is saved in config/packages/oneup_flysystem.yaml |
|
35
|
|
|
return $this->mountManager->getFilesystem('assets_fs'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function unZipFile(Asset $asset, ZipArchiveAdapter $zipArchiveAdapter) |
|
39
|
|
|
{ |
|
40
|
|
|
$folder = '/'.$asset->getCategory().'/'.$asset->getTitle(); |
|
41
|
|
|
|
|
42
|
|
|
$fs = $this->getFileSystem(); |
|
43
|
|
|
if ($fs->has($folder)) { |
|
44
|
|
|
$contents = $zipArchiveAdapter->listContents(); |
|
45
|
|
|
foreach ($contents as $data) { |
|
46
|
|
|
if ($fs->has($folder.'/'.$data['path'])) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ('dir' === $data['type']) { |
|
51
|
|
|
$fs->createDir($folder.'/'.$data['path']); |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$fs->write($folder.'/'.$data['path'], $zipArchiveAdapter->read($data['path'])); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getFolder(Asset $asset): ? string |
|
61
|
|
|
{ |
|
62
|
|
|
if ($asset->hasFile()) { |
|
63
|
|
|
$file = $asset->getTitle(); |
|
64
|
|
|
|
|
65
|
|
|
return '/'.$asset->getCategory().'/'.$file.'/'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getAssetUrl(Asset $asset) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->router->generate( |
|
74
|
|
|
'chamilo_core_asset_showfile', |
|
75
|
|
|
['category' => $asset->getCategory(), 'path' => $asset->getTitle()] |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getFileContent(Asset $asset): string |
|
80
|
|
|
{ |
|
81
|
|
|
try { |
|
82
|
|
|
if ($asset->hasFile()) { |
|
83
|
|
|
$file = $asset->getFile(); |
|
84
|
|
|
$fileName = $this->getFilename($file); |
|
85
|
|
|
|
|
86
|
|
|
return $this->getFileSystem()->read($fileName); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return ''; |
|
90
|
|
|
} catch (\Throwable $exception) { |
|
91
|
|
|
throw new FileNotFoundException($asset); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getFileStream(Asset $asset) |
|
96
|
|
|
{ |
|
97
|
|
|
try { |
|
98
|
|
|
if ($asset->hasFile()) { |
|
99
|
|
|
$file = $asset->getFile(); |
|
100
|
|
|
$fileName = $this->getFilename($file); |
|
101
|
|
|
|
|
102
|
|
|
return $this->getFileSystem()->readStream($fileName); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return ''; |
|
106
|
|
|
} catch (\Throwable $exception) { |
|
107
|
|
|
throw new FileNotFoundException($asset); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|