Skip to content

Commit bab03b0

Browse files
chore: upload file middleware
1 parent 68e7e2f commit bab03b0

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/api/user/User.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class User {
2828
#[ORM\Column]
2929
private string $password;
3030

31+
#[ORM\Column(nullable: true)]
32+
public string $image;
33+
34+
#[ORM\Column(nullable: true)]
35+
public string $logo;
36+
3137
#[ORM\Column(name: "created_at", insertable: false, updatable: false)]
3238
public string $createdAt;
3339

src/api/user/router.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use Slim\Routing\RouteCollectorProxy;
66
use RestJS\Api\User\Controller;
7+
use RestJS\Middleware\Upload;
78

89
class Router {
910
function __invoke(RouteCollectorProxy $router) {
1011
$router->get('/', [Controller::class, "findAll"]);
1112
$router->get('/{id:[0-9]+}/', [Controller::class, "findByColumn"]);
1213
$router->get('/{username:[a-z0-9-]+}/', [Controller::class, "findByColumn"]);
13-
$router->put('/{id:[0-9]+}/', [Controller::class, "update"]);
14-
$router->post('/', [Controller::class, "insert"]);
14+
$router->put('/{id:[0-9]+}/', [Controller::class, "update"])->add(Upload::class);
15+
$router->post('/', [Controller::class, "insert"])->add(Upload::class);
1516
$router->delete('/{id:[0-9]+}/', [Controller::class, "delete"]);
1617
}
1718
}

src/middleware/upload.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace RestJS\Middleware;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface as Request;
7+
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
8+
use Psr\Http\Server\MiddlewareInterface;
9+
use Psr\Http\Message\UploadedFileInterface;
10+
11+
/** File Upload Middleware */
12+
class Upload implements MiddlewareInterface {
13+
14+
function process(Request $req, RequestHandler $handler): ResponseInterface {
15+
16+
/** User Upload File */
17+
$uploadedFile = $req->getUploadedFiles();
18+
19+
foreach ($uploadedFile as $key => $value):
20+
21+
/** Upload File Path */
22+
$file = self::moveUploadedFile('../content/', $key, $value);
23+
24+
/** Added Upload File Value with User Form Data */
25+
$req = $req->withParsedBody([...$req->getParsedBody(), $key => $file]);
26+
endforeach;
27+
28+
return $handler->handle($req);
29+
}
30+
31+
32+
/** File Upload to Server Function */
33+
function moveUploadedFile(string $directory, string $type, UploadedFileInterface $uploadedFile) {
34+
35+
/** File Extension */
36+
$ext = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
37+
38+
/** Upload File Name */
39+
$filename = strtoupper($type) . "_" . time() . ".{$ext}";
40+
41+
// Check and Create Target Directory
42+
!is_dir($directory) && mkdir($directory);
43+
44+
// File Upload to Server
45+
$uploadedFile->moveTo($directory . $filename);
46+
47+
return $directory . $filename;
48+
}
49+
}

0 commit comments

Comments
 (0)