|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) Florian Krämer (https://florian-kraemer.net) |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
|
10
|
|
|
* @author Florian Krämer |
|
11
|
|
|
* @link https://github.com/Phauthentic |
|
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types=1); |
|
16
|
|
|
|
|
17
|
|
|
namespace Phauthentic\Infrastructure\Storage; |
|
18
|
|
|
|
|
19
|
|
|
use Phauthentic\Infrastructure\Storage\Exception\FileDoesNotExistException; |
|
20
|
|
|
use Phauthentic\Infrastructure\Storage\Exception\FileNotReadableException; |
|
21
|
|
|
use Phauthentic\Infrastructure\Storage\Utility\MimeType; |
|
22
|
|
|
use Phauthentic\Infrastructure\Storage\Utility\PathInfo; |
|
23
|
|
|
use Phauthentic\Infrastructure\Storage\Utility\StreamWrapper; |
|
24
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
25
|
|
|
use RuntimeException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* File Factory |
|
29
|
|
|
*/ |
|
30
|
|
|
class FileFactory implements FileFactoryInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritDoc |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public static function fromUploadedFile( |
|
36
|
|
|
UploadedFileInterface $uploadedFile, |
|
37
|
|
|
string $storage |
|
38
|
|
|
): FileInterface { |
|
39
|
1 |
|
static::checkUploadedFile($uploadedFile); |
|
40
|
|
|
|
|
41
|
|
|
$file = File::create( |
|
42
|
|
|
(string)$uploadedFile->getClientFilename(), |
|
43
|
|
|
(int)$uploadedFile->getSize(), |
|
44
|
|
|
(string)$uploadedFile->getClientMediaType(), |
|
45
|
|
|
$storage |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
return $file->withResource( |
|
49
|
|
|
StreamWrapper::getResource($uploadedFile->getStream()) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
9 |
|
public static function fromDisk(string $path, string $storage): FileInterface |
|
57
|
|
|
{ |
|
58
|
9 |
|
static::checkFile($path); |
|
59
|
|
|
|
|
60
|
9 |
|
$info = PathInfo::for($path); |
|
61
|
9 |
|
$filesize = filesize($path); |
|
62
|
9 |
|
$mimeType = MimeType::byExtension($info->extension()); |
|
63
|
|
|
|
|
64
|
9 |
|
$file = File::create( |
|
65
|
9 |
|
$info->basename(), |
|
66
|
|
|
$filesize, |
|
67
|
|
|
$mimeType, |
|
68
|
|
|
$storage, |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
9 |
|
$resource = fopen($path, 'rb'); |
|
72
|
|
|
|
|
73
|
9 |
|
return $file->withResource($resource); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Checks if the uploaded file is a valid upload |
|
78
|
|
|
* |
|
79
|
|
|
* @param \Psr\Http\Message\UploadedFileInterface $uploadedFile Uploaded File |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
1 |
|
protected static function checkUploadedFile(UploadedFileInterface $uploadedFile): void |
|
83
|
|
|
{ |
|
84
|
1 |
|
if ($uploadedFile->getError() !== UPLOAD_ERR_OK) { |
|
85
|
1 |
|
throw new RuntimeException(sprintf( |
|
86
|
1 |
|
'Can\'t create storage object from upload with error code: %d', |
|
87
|
1 |
|
$uploadedFile->getError() |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $path Path |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
9 |
|
protected static function checkFile(string $path): void |
|
97
|
|
|
{ |
|
98
|
9 |
|
if (!file_exists($path)) { |
|
99
|
|
|
throw FileDoesNotExistException::filename($path); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
9 |
|
if (!is_readable($path)) { |
|
103
|
|
|
throw FileNotReadableException::filename($path); |
|
104
|
|
|
} |
|
105
|
9 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|