Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.95% |
17 / 21 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| InlineStorage | |
80.95% |
17 / 21 |
|
85.71% |
6 / 7 |
9.56 | |
0.00% |
0 / 1 |
| createNewUpload | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getProxy | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| loadFromStorage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| resourceToPath | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| pathToResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| psrToPath | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| pathToPsr | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\FileStorage; |
| 3 | |
| 4 | use Psr\Http\Message\UploadedFileInterface; |
| 5 | |
| 6 | /** |
| 7 | * Store files by just returning a path containing mime type, original filename and contents |
| 8 | */ |
| 9 | class InlineStorage implements PsrAwareStorageInterface, ResourceAwareStorageInterface |
| 10 | { |
| 11 | public function createNewUpload( |
| 12 | UploadedFileInterface $fileUpload, |
| 13 | string $className = StoredFile::class |
| 14 | ): StoredFile { |
| 15 | $storagePath = $this->psrToPath($fileUpload); |
| 16 | return $this->loadFromStorage($storagePath, $className); |
| 17 | } |
| 18 | |
| 19 | public function getProxy( |
| 20 | string $storagePath, |
| 21 | string $className = StoredFile::class |
| 22 | ): StoredFile { |
| 23 | return $className::createFromStorage( |
| 24 | $this, |
| 25 | $storagePath |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | public function loadFromStorage( |
| 30 | string $storagePath, |
| 31 | string $className = StoredFile::class |
| 32 | ): StoredFile { |
| 33 | list($mimeType, $originalName, $contents) = explode('|', $storagePath, 3); |
| 34 | return $className::createFromString(base64_decode($contents), $mimeType, $originalName) |
| 35 | ->markBeingStored($this, $storagePath); |
| 36 | } |
| 37 | |
| 38 | public function resourceToPath(mixed $resource): string |
| 39 | { |
| 40 | assert(is_resource($resource)); |
| 41 | fseek($resource, 0); |
| 42 | return base64_encode(stream_get_contents($resource)); |
| 43 | } |
| 44 | |
| 45 | public function pathToResource(string $path): mixed |
| 46 | { |
| 47 | return fopen('data://text/plain,' . base64_decode($path), 'r'); |
| 48 | } |
| 49 | |
| 50 | public function psrToPath(UploadedFileInterface $uploadedFile): string |
| 51 | { |
| 52 | return sprintf( |
| 53 | '%s|%s|%s', |
| 54 | str_replace('|', '', $uploadedFile->getClientMediaType() ?? ''), |
| 55 | str_replace('|', '', $uploadedFile->getClientFilename() ?? ''), |
| 56 | base64_encode($uploadedFile->getStream()->__toString()) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | public function pathToPsr(string $path): UploadedFileInterface |
| 61 | { |
| 62 | list($mimeType, $originalName, $contents) = explode('|', $path, 3); |
| 63 | return StoredFile::createFromString(base64_decode($contents), $mimeType ? : null, $originalName ? : null); |
| 64 | } |
| 65 | } |