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