1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Http\Controllers\Api; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Config\Routes; |
8
|
|
|
use AbterPhp\Admin\Constant\Env; |
9
|
|
|
use AbterPhp\Framework\Constant\Env as FrameworkEnv; |
10
|
|
|
use Opulence\Environments\Environment; |
11
|
|
|
use Opulence\Http\Responses\JsonResponse; |
12
|
|
|
use Opulence\Http\Responses\Response; |
13
|
|
|
use Opulence\Http\Responses\ResponseHeaders; |
14
|
|
|
use Opulence\Routing\Controller; |
15
|
|
|
|
16
|
|
|
class Editor extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
public function fileUpload(): Response |
22
|
|
|
{ |
23
|
|
|
$actualClientId = explode(' ', $this->request->getHeaders()->get('authorization'))[1]; |
24
|
|
|
|
25
|
|
|
$expectedClientId = Environment::getVar(FrameworkEnv::CRYPTO_CLIENT_ID); |
26
|
|
|
if ($actualClientId != $expectedClientId) { |
27
|
|
|
return $this->sendJson(ResponseHeaders::HTTP_FORBIDDEN); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$image = $this->request->getFiles()->get('image'); |
31
|
|
|
if ($image->hasErrors()) { |
32
|
|
|
return $this->sendJson(ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$pathinfo = pathinfo($image->getTempFilename()); |
36
|
|
|
$basePath = mb_substr(basename($image->getPathname()), 3); |
37
|
|
|
$extension = $pathinfo['extension'] ?? ''; |
38
|
|
|
$filename = sprintf('%s.%s', $basePath, $extension); |
39
|
|
|
|
40
|
|
|
$path = Environment::getVar(FrameworkEnv::DIR_UPLOAD); |
|
|
|
|
41
|
|
|
$url = Environment::getVar(Env::UPLOAD_BASE_URL) . '/' . $filename; |
42
|
|
|
|
43
|
|
|
$image->move($path, $filename); |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $this->sendJson(ResponseHeaders::HTTP_OK, ['url' => $url]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $status |
50
|
|
|
* @param array|null $data |
51
|
|
|
* |
52
|
|
|
* @return Response |
53
|
|
|
*/ |
54
|
|
|
protected function sendJson($status = ResponseHeaders::HTTP_OK, array $data = null): Response |
55
|
|
|
{ |
56
|
|
|
$body = [ |
57
|
|
|
'success' => $status == ResponseHeaders::HTTP_OK, |
58
|
|
|
'status' => $status, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
if ($data) { |
62
|
|
|
$body['data'] = $data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$response = new JsonResponse($body, $status); |
66
|
|
|
|
67
|
|
|
$response->send(); |
68
|
|
|
|
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|