Use Storage::disk() where possible

This commit is contained in:
Bubka 2022-07-19 18:27:09 +02:00
parent bf32b37176
commit e540e2bb26
4 changed files with 24 additions and 13 deletions

View File

@ -22,6 +22,7 @@ public function upload(Request $request)
]);
$path = $request->file('icon')->store('public/icons');
$path = $request->file('icon')->store('', 'icons');
$response['filename'] = pathinfo($path)['basename'];
return response()->json($response, 201);
@ -36,7 +37,7 @@ public function upload(Request $request)
*/
public function delete($icon)
{
Storage::delete('public/icons/' . $icon);
Storage::disk('icons')->delete($icon);
return response()->json(null, 204);
}

File diff suppressed because one or more lines are too long

View File

@ -26,7 +26,7 @@ public function __construct()
*/
public function handle(TwoFAccountDeleted $event)
{
Storage::delete('public/icons/' . $event->twofaccount->icon);
Storage::disk('icons')->delete($event->twofaccount->icon);
Log::info(sprintf('Icon cleaned for deleted TwoFAccount #%d', $event->twofaccount->id));
}
}

View File

@ -28,6 +28,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use ParagonIE\ConstantTime\Base32;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Http;
class TwoFAccount extends Model implements Sortable
{
@ -535,25 +536,34 @@ private function initGenerator()
private function storeImageAsIcon(string $url)
{
try {
$remoteImageURL = $url;
$path_parts = pathinfo($remoteImageURL);
$newFilename = Str::random(40) . '.' . $path_parts['extension'];
$path_parts = pathinfo($url);
$newFilename = Str::random(40).'.'.$path_parts['extension'];
$imageFile = self::IMAGELINK_STORAGE_PATH . $newFilename;
$iconFile = self::ICON_STORAGE_PATH . $newFilename;
Storage::disk('local')->put($imageFile, file_get_contents($remoteImageURL));
try {
$response = Http::retry(3, 100)->get($url);
if ($response->successful()) {
Storage::disk('imagesLink')->put($newFilename, $response->body());
}
}
catch (\Exception $exception) {
Log::error(sprintf('Cannot fetch imageLink at "%s"', $url));
}
if ( in_array(Storage::mimeType($imageFile), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp'])
&& getimagesize(storage_path() . '/app/' . $imageFile) )
{
// Should be a valid image
Storage::move($imageFile, $iconFile);
// Should be a valid image, we move it to the icons disk
if (Storage::disk('icons')->put($newFilename, Storage::disk('imagesLink')->get($newFilename))) {
Storage::disk('imagesLink')->delete($newFilename);
}
Log::info(sprintf('Icon file %s stored', $newFilename));
}
else {
// @codeCoverageIgnoreStart
Storage::delete($imageFile);
Storage::disk('imagesLink')->delete($newFilename);
throw new \Exception('Unsupported mimeType or missing image on storage');
// @codeCoverageIgnoreEnd
}
@ -561,7 +571,7 @@ private function storeImageAsIcon(string $url)
return $newFilename;
}
// @codeCoverageIgnoreStart
catch (\Assert\AssertionFailedException|\Assert\InvalidArgumentException|\Exception|\Throwable $ex) {
catch (\Exception|\Throwable $ex) {
Log::error(sprintf('Icon storage failed: %s', $ex->getMessage()));
return null;
}