Created
April 17, 2020 18:59
-
-
Save mmsesay/8e9f4bd4685060070e7e48896d105c71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Utils; | |
use Gedmo\Sluggable\Util\Urlizer; | |
use League\Flysystem\AdapterInterface; | |
use League\Flysystem\FileNotFoundException; | |
use League\Flysystem\FilesystemInterface; | |
use League\Glide\Filesystem\FilesystemException; | |
use Psr\Log\LoggerInterface; | |
use Symfony\Component\HttpFoundation\File\File; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
class UploaderHelper | |
{ | |
private $filesystem; | |
private $logger; | |
public function __construct(FilesystemInterface $uploadsFilesystem, LoggerInterface $logger) | |
{ | |
$this->filesystem = $uploadsFilesystem; | |
$this->logger = $logger; | |
} | |
public function uploadImage(File $file, ?string $existingFilename): string | |
{ | |
$newFilename = $this->uploadFile($file, true); | |
if ($existingFilename) { | |
try { | |
$result = $this->filesystem->delete($existingFilename); | |
if ($result === false) { | |
throw new FilesystemException(sprintf('Could not delete old uploaded file "%s"', $existingFilename)); | |
} | |
} catch (FileNotFoundException $e) { | |
$this->logger->alert(sprintf('Old uploaded file "%s" was missing when trying to delete', $existingFilename)); | |
} | |
} | |
return $newFilename; | |
} | |
public function uploadDocument(File $file): string | |
{ | |
return $this->uploadFile($file, false); | |
} | |
/** | |
* @return resource | |
*/ | |
public function readStream(string $path) | |
{ | |
$resource = $this->filesystem->readStream($path); | |
if ($resource === false) { | |
throw new FilesystemException(sprintf('Error opening stream for "%s"', $path)); | |
} | |
return $resource; | |
} | |
public function deleteFile(string $path) | |
{ | |
$result = $this->filesystem->delete($path); | |
if ($result === false) { | |
throw new FilesystemException(sprintf('Error deleting "%s"', $path)); | |
} | |
} | |
private function uploadFile(File $file, bool $isPublic): string | |
{ | |
if ($file instanceof UploadedFile) { | |
$originalFilename = $file->getClientOriginalName(); | |
} else { | |
$originalFilename = $file->getFilename(); | |
} | |
$newFilename = Urlizer::urlize(pathinfo($originalFilename, PATHINFO_FILENAME)).'-'.uniqid().'.'.$file->guessExtension(); | |
$stream = fopen($file->getPathname(), 'r'); | |
$result = $this->filesystem->writeStream( | |
$newFilename, | |
$stream, | |
[ | |
'visibility' => $isPublic ? AdapterInterface::VISIBILITY_PUBLIC : AdapterInterface::VISIBILITY_PRIVATE | |
] | |
); | |
if ($result === false) { | |
throw new FilesystemException(sprintf('Could not write uploaded file "%s"', $newFilename)); | |
} | |
if (is_resource($stream)) { | |
fclose($stream); | |
} | |
return $newFilename; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment