1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Http\Controllers\Api; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Http\Controllers\ApiAbstract; |
8
|
|
|
use AbterPhp\Files\Domain\Entities\File as Entity; |
9
|
|
|
use AbterPhp\Files\Service\Execute\Api\File as RepoService; |
10
|
|
|
use AbterPhp\Framework\Config\EnvReader; |
11
|
|
|
use AbterPhp\Framework\Databases\Queries\FoundRows; |
12
|
|
|
use League\Flysystem\Filesystem; |
13
|
|
|
use League\Flysystem\FilesystemException; |
14
|
|
|
use Opulence\Http\Responses\Response; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
|
17
|
|
|
class File extends ApiAbstract |
18
|
|
|
{ |
19
|
|
|
const FILENAMESYSTEM_NAME_LENGTH = 6; |
20
|
|
|
|
21
|
|
|
const ENTITY_SINGULAR = 'file'; |
22
|
|
|
const ENTITY_PLURAL = 'files'; |
23
|
|
|
|
24
|
|
|
/** @var Filesystem */ |
25
|
|
|
protected $filesystem; |
26
|
|
|
|
27
|
|
|
/** @var RepoService */ |
28
|
|
|
protected $repoService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* File constructor. |
32
|
|
|
* |
33
|
|
|
* @param LoggerInterface $logger |
34
|
|
|
* @param RepoService $repoService |
35
|
|
|
* @param FoundRows $foundRows |
36
|
|
|
* @param EnvReader $envReader |
37
|
|
|
* @param Filesystem $filesystem |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
LoggerInterface $logger, |
41
|
|
|
RepoService $repoService, |
42
|
|
|
FoundRows $foundRows, |
43
|
|
|
EnvReader $envReader, |
44
|
|
|
Filesystem $filesystem |
45
|
|
|
) { |
46
|
|
|
parent::__construct($logger, $repoService, $foundRows, $envReader); |
47
|
|
|
|
48
|
|
|
$this->filesystem = $filesystem; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $entityId |
53
|
|
|
* |
54
|
|
|
* @return Response |
55
|
|
|
* @throws FilesystemException |
56
|
|
|
*/ |
57
|
|
|
public function get(string $entityId): Response |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$entity = $this->repoService->retrieveEntity($entityId); |
61
|
|
|
} catch (\Exception $e) { |
62
|
|
|
$msg = sprintf(static::LOG_MSG_GET_FAILURE, static::ENTITY_SINGULAR); |
63
|
|
|
|
64
|
|
|
return $this->handleException($msg, $e); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!($entity instanceof Entity)) { |
68
|
|
|
throw new \RuntimeException('Invalid entity'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($this->request->getQuery()->get('embed') === 'data') { |
72
|
|
|
$content = $this->filesystem->read($entity->getFilesystemName()); |
73
|
|
|
|
74
|
|
|
$entity->setContent(base64_encode($content)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->handleGetSuccess($entity); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
* @throws FilesystemException |
83
|
|
|
*/ |
84
|
|
|
public function getSharedData(): array |
85
|
|
|
{ |
86
|
|
|
$data = $this->request->getJsonBody(); |
87
|
|
|
|
88
|
|
|
$path = \bin2hex(\random_bytes(static::FILENAMESYSTEM_NAME_LENGTH)); |
89
|
|
|
$content = base64_decode($data['data'], true); |
90
|
|
|
|
91
|
|
|
$this->filesystem->write($path, $content); |
92
|
|
|
|
93
|
|
|
$data['filesystem_name'] = $path; |
94
|
|
|
$data['public_name'] = $data['name']; |
95
|
|
|
|
96
|
|
|
return $data; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|