create filesystem abstraction
This commit is contained in:
parent
be2c51bfb7
commit
d59d444952
14 changed files with 44 additions and 64 deletions
59
backend/app/Shared/Files/LaravelFilesystem.php
Normal file
59
backend/app/Shared/Files/LaravelFilesystem.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Shared\Files;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class LaravelFilesystem implements Filesystem
|
||||
{
|
||||
public function upload(FileToUpload $file, string $folder): string
|
||||
{
|
||||
$path = $folder . '/' . Str::random(40) . $this->extensionFor($file);
|
||||
Storage::disk('public')->put($path, $file->getContents());
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
public function url(string $path): string
|
||||
{
|
||||
return Storage::disk('public')->url($path);
|
||||
}
|
||||
|
||||
public function delete(string $path): void
|
||||
{
|
||||
Storage::disk('public')->delete($path);
|
||||
}
|
||||
|
||||
public function read(string $path): ?string
|
||||
{
|
||||
if (!Storage::disk('public')->exists($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Storage::disk('public')->get($path);
|
||||
}
|
||||
|
||||
private function extensionFor(FileToUpload $file): string
|
||||
{
|
||||
$mimeExtensions = [
|
||||
'image/jpeg' => '.jpg',
|
||||
'image/png' => '.png',
|
||||
'image/webp' => '.webp',
|
||||
'application/pdf' => '.pdf',
|
||||
];
|
||||
if (isset($mimeExtensions[$file->getMimeType()])) {
|
||||
return $mimeExtensions[$file->getMimeType()];
|
||||
}
|
||||
|
||||
$originalExtension = pathinfo(
|
||||
$file->getOriginalName(),
|
||||
PATHINFO_EXTENSION,
|
||||
);
|
||||
if ($originalExtension !== '') {
|
||||
return '.' . $originalExtension;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue