Refactored Image And Video Repository

This commit is contained in:
Devansh 2022-02-03 16:42:37 +05:30
parent fb1fbfb873
commit 757dc9a8ed
3 changed files with 119 additions and 130 deletions

View File

@ -3,12 +3,9 @@
namespace Webkul\Product\Repositories;
use Illuminate\Container\Container as App;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Webkul\Core\Eloquent\Repository;
use Webkul\Product\Repositories\ProductRepository;
class ProductImageRepository extends Repository
class ProductImageRepository extends ProductMediaRepository
{
/**
* Product repository object.
@ -43,17 +40,6 @@ class ProductImageRepository extends Repository
return \Webkul\Product\Contracts\ProductImage::class;
}
/**
* Get product directory.
*
* @param \Webkul\Product\Contracts\Product $product
* @return string
*/
public function getProductDirectory($product): string
{
return 'product/' . $product->id;
}
/**
* Upload images.
*
@ -63,66 +49,13 @@ class ProductImageRepository extends Repository
*/
public function uploadImages($data, $product): void
{
$this->upload($product, $data['images'] ?? null);
$this->upload($data, $product, 'images');
if (isset($data['variants'])) {
$this->uploadVariantImages($data['variants']);
}
}
/**
* Upload.
*
* @param \Webkul\Product\Contracts\Product $product
* @param array
* @return void
*/
public function upload($product, $images): void
{
/**
* Previous images ids for filtering.
*/
$previousImageIds = $product->images()->pluck('id');
if (
isset($images['files']) && $images['files']
&& isset($images['positions']) && $images['positions']
) {
/**
* Filter out existing images because new image positions are already setuped by index.
*/
$imagePositions = collect($images['positions'])->keys()->filter(function ($imagePosition) {
return is_numeric($imagePosition);
});
foreach ($images['files'] as $indexOrImageId => $image) {
if ($image instanceof UploadedFile) {
$this->create([
'path' => $image->store($this->getProductDirectory($product)),
'product_id' => $product->id,
'position' => $indexOrImageId,
]);
} else {
$this->update([
'position' => $imagePositions->search($indexOrImageId),
], $indexOrImageId);
if (is_numeric($index = $previousImageIds->search($indexOrImageId))) {
$previousImageIds->forget($index);
}
}
}
}
foreach ($previousImageIds as $indexOrImageId) {
if ($image = $this->find($indexOrImageId)) {
Storage::delete($image->path);
$this->delete($indexOrImageId);
}
}
}
/**
* Upload variant images.
*
@ -131,14 +64,14 @@ class ProductImageRepository extends Repository
*/
public function uploadVariantImages($variants): void
{
foreach ($variants as $variantsId => $variant) {
foreach ($variants as $variantsId => $variantData) {
$product = $this->productRepository->find($variantsId);
if (! $product) {
break;
}
$this->upload($product, $variant['images'] ?? null);
$this->upload($variantData, $product, 'images');
}
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace Webkul\Product\Repositories;
use Exception;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Webkul\Core\Eloquent\Repository;
class ProductMediaRepository extends Repository
{
/**
* Specify model class name.
*
* @return string
*/
public function model()
{
/**
* This repository is extended to `ProductImageRepository` and `ProductVideoRepository`
* repository.
*
* And currently no model is assigned to this repo.
*/
}
/**
* Get product directory.
*
* @param \Webkul\Product\Contracts\Product $product
* @return string
*/
public function getProductDirectory($product): string
{
return 'product/' . $product->id;
}
/**
* Upload.
*
* @param \Webkul\Product\Contracts\Product $product
* @param array
* @return void
*/
public function upload($data, $product, $uploadFileType): void
{
/**
* Previous model ids for filtering.
*/
$previousIds = $this->resolveFileTypeQueryBuilder($product, $uploadFileType)->pluck('id');
if (
isset($data[$uploadFileType]['files']) && $data[$uploadFileType]['files']
&& isset($data[$uploadFileType]['positions']) && $data[$uploadFileType]['positions']
) {
/**
* Filter out existing models because new model positions are already setuped by index.
*/
$positions = collect($data[$uploadFileType]['positions'])->keys()->filter(function ($position) {
return is_numeric($position);
});
foreach ($data[$uploadFileType]['files'] as $indexOrModelId => $file) {
if ($file instanceof UploadedFile) {
$this->create([
'type' => $uploadFileType,
'path' => $file->store($this->getProductDirectory($product)),
'product_id' => $product->id,
'position' => $indexOrModelId,
]);
} else {
$this->update([
'position' => $positions->search($indexOrModelId),
], $indexOrModelId);
if (is_numeric($index = $previousIds->search($indexOrModelId))) {
$previousIds->forget($index);
}
}
}
}
foreach ($previousIds as $indexOrModelId) {
if ($model = $this->find($indexOrModelId)) {
Storage::delete($model->path);
$this->delete($indexOrModelId);
}
}
}
/**
* Resolve file type query builder.
*
* @param \Webkul\Product\Contracts\Product $product
* @param string $uploadFileType
* @return mixed
*
* @throws \Exception
*/
private function resolveFileTypeQueryBuilder($product, string $uploadFileType)
{
if ($uploadFileType === 'images') {
return $product->images();
}
if ($uploadFileType === 'videos') {
return $product->videos();
}
throw new Exception('Unsupported file type.');
}
}

View File

@ -2,11 +2,7 @@
namespace Webkul\Product\Repositories;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Webkul\Core\Eloquent\Repository;
class ProductVideoRepository extends Repository
class ProductVideoRepository extends ProductMediaRepository
{
/**
* Specify model class name.
@ -18,17 +14,6 @@ class ProductVideoRepository extends Repository
return \Webkul\Product\Contracts\ProductVideo::class;
}
/**
* Get product directory.
*
* @param \Webkul\Product\Contracts\Product $product
* @return string
*/
public function getProductDirectory($product): string
{
return 'product/' . $product->id;
}
/**
* Upload videos.
*
@ -38,48 +23,6 @@ class ProductVideoRepository extends Repository
*/
public function uploadVideos($data, $product)
{
/**
* Previous images ids for filtering.
*/
$previousVideoIds = $product->videos()->pluck('id');
if (
isset($data['videos']['files']) && $data['videos']['files']
&& isset($data['videos']['positions']) && $data['videos']['positions']
) {
/**
* Filter out existing videos because new video positions are already setuped by index.
*/
$videoPositions = collect($data['videos']['positions'])->keys()->filter(function ($videoPosition) {
return is_numeric($videoPosition);
});
foreach ($data['videos']['files'] as $indexOrVideoId => $video) {
if ($video instanceof UploadedFile) {
$this->create([
'path' => $video->store($this->getProductDirectory($product)),
'product_id' => $product->id,
'position' => $indexOrVideoId,
'type' => 'video',
]);
} else {
$this->update([
'position' => $videoPositions->search($indexOrVideoId),
], $indexOrVideoId);
if (is_numeric($index = $previousVideoIds->search($indexOrVideoId))) {
$previousVideoIds->forget($index);
}
}
}
}
foreach ($previousVideoIds as $indexOrVideoId) {
if ($video = $this->find($indexOrVideoId)) {
Storage::delete($video->path);
$this->delete($indexOrVideoId);
}
}
$this->upload($data, $product, 'videos');
}
}