create filesystem abstraction
This commit is contained in:
parent
be2c51bfb7
commit
d59d444952
14 changed files with 44 additions and 64 deletions
|
|
@ -1,50 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Shared\Files;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class LaravelFileUploader implements FileUploader
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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