|
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 Chamilo\CoreBundle\Entity\ResourceFile; |
|
9
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
10
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
11
|
|
|
use League\Flysystem\FilesystemInterface; |
|
12
|
|
|
use League\Flysystem\MountManager; |
|
13
|
|
|
use League\Flysystem\ZipArchive\ZipArchiveAdapter; |
|
14
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
|
15
|
|
|
use Vich\UploaderBundle\Storage\FlysystemStorage; |
|
16
|
|
|
|
|
17
|
|
|
class AssetRepository extends ServiceEntityRepository |
|
18
|
|
|
{ |
|
19
|
|
|
protected $mountManager; |
|
20
|
|
|
protected $storage; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(ManagerRegistry $registry, FlysystemStorage $storage, MountManager $mountManager) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct($registry, Asset::class); |
|
25
|
|
|
$this->storage = $storage; |
|
26
|
|
|
$this->mountManager = $mountManager; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getFilename(ResourceFile $resourceFile) |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->storage->resolveUri($resourceFile); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return FilesystemInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getFileSystem() |
|
38
|
|
|
{ |
|
39
|
|
|
// Flysystem mount name is saved in config/packages/oneup_flysystem.yaml |
|
40
|
|
|
return $this->mountManager->getFilesystem('assets_fs'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function unZipFile(Asset $asset, ZipArchiveAdapter $zipArchiveAdapter) |
|
44
|
|
|
{ |
|
45
|
|
|
$folder = '/'.$asset->getCategory().'/'.$asset->getTitle(); |
|
46
|
|
|
|
|
47
|
|
|
$fs = $this->getFileSystem(); |
|
48
|
|
|
if ($fs->has($folder)) { |
|
49
|
|
|
$contents = $zipArchiveAdapter->listContents(); |
|
50
|
|
|
foreach ($contents as $data) { |
|
51
|
|
|
if ($fs->has($folder.'/'.$data['path'])) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ('dir' === $data['type']) { |
|
56
|
|
|
$fs->createDir($folder.'/'.$data['path']); |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$fs->write($folder.'/'.$data['path'], $zipArchiveAdapter->read($data['path'])); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getFileContent(Asset $asset): string |
|
66
|
|
|
{ |
|
67
|
|
|
try { |
|
68
|
|
|
if ($asset->hasFile()) { |
|
69
|
|
|
$file = $asset->getFile(); |
|
70
|
|
|
$fileName = $this->getFilename($file); |
|
71
|
|
|
|
|
72
|
|
|
return $this->getFileSystem()->read($fileName); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return ''; |
|
76
|
|
|
} catch (\Throwable $exception) { |
|
77
|
|
|
throw new FileNotFoundException($asset); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getFileStream(Asset $asset) |
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
|
|
if ($asset->hasFile()) { |
|
85
|
|
|
$file = $asset->getFile(); |
|
86
|
|
|
$fileName = $this->getFilename($file); |
|
87
|
|
|
|
|
88
|
|
|
return $this->getFileSystem()->readStream($fileName); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return ''; |
|
92
|
|
|
} catch (\Throwable $exception) { |
|
93
|
|
|
throw new FileNotFoundException($asset); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|