43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Inventory\Infrastructure\Files;
|
|
|
|
use App\Modules\Inventory\Application\Contracts\StockItemImages;
|
|
use App\Modules\Inventory\Domain\Exceptions\InvalidStockImage;
|
|
use App\Modules\Shared\Application\Contracts\ImageResizer;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Stock item images on the private local disk under stock-items/: an
|
|
* upright JPEG without metadata and a thumbnail for touch-screen buttons.
|
|
*/
|
|
class StockItemImageFiles implements StockItemImages
|
|
{
|
|
public const string DISK = 'local';
|
|
|
|
public const int MAX_SIZE = 1280;
|
|
|
|
public const int THUMBNAIL_SIZE = 240;
|
|
|
|
public function __construct(private ImageResizer $resizer) {}
|
|
|
|
public function store(string $uploadedFilePath, int $itemId): array
|
|
{
|
|
$versions = $this->resizer->jpegVersions($uploadedFilePath, [self::MAX_SIZE, self::THUMBNAIL_SIZE]) ?? throw new InvalidStockImage;
|
|
$name = 'stock-items/'.$itemId.'/'.Str::uuid()->toString();
|
|
|
|
Storage::disk(self::DISK)->put($name.'.jpg', $versions[0]['bytes']);
|
|
Storage::disk(self::DISK)->put($name.'-thumb.jpg', $versions[1]['bytes']);
|
|
|
|
return ['path' => $name.'.jpg', 'thumbnailPath' => $name.'-thumb.jpg'];
|
|
}
|
|
|
|
public function delete(array $paths): void
|
|
{
|
|
if ($paths !== []) {
|
|
Storage::disk(self::DISK)->delete($paths);
|
|
}
|
|
}
|
|
}
|