Created
February 21, 2019 09:29
-
-
Save einnar82/34d4e60635718ec692d64a58266af3db to your computer and use it in GitHub Desktop.
Upload multiple photos in a polymorphic relation
This file contains 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 | |
/** | |
* Created by PhpStorm. | |
* User: rannie | |
* Date: 2/21/19 | |
* Time: 11:33 AM | |
*/ | |
namespace App\Modules\Photos\Traits; | |
use App\Modules\Photos\Models\Photo; | |
use Carbon\Carbon; | |
trait CanProcessPhotos | |
{ | |
/** | |
* Generate file name from base 64 | |
* @param $item | |
* @return string | |
*/ | |
public function getFileNameFromBase64($item) | |
{ | |
return Carbon::now()->timestamp . '_' . uniqid() . '.' . explode('/', explode(':', substr($item, 0, strpos($item, ';')))[1])[1]; | |
} | |
/** | |
* Moving to storage | |
* @param $item | |
* @param $fileName | |
* @param $storageName | |
*/ | |
public function moveToStorage($item, $fileName, $storageName) | |
{ | |
$exploded = explode(',', $item); | |
$decoded = base64_decode($exploded[1]); | |
\Storage::disk($storageName)->put($fileName, $decoded); | |
} | |
/** | |
* Add photo details | |
* @param $user | |
* @param $fileName | |
* @param $path | |
* @param $isDefault | |
*/ | |
public function savePhotoDetails($user, $fileName, $path, $isDefault) | |
{ | |
if (!is_null($fileName)) { | |
$user->photos()->save(new Photo([ | |
'url' => url("/storage/$path/$fileName"), | |
'is_default' => $isDefault | |
])); | |
} | |
} | |
/** | |
* Check if the item is array or not | |
* @param $items | |
* @param $disk | |
* @param $user | |
* @param $isDefault | |
*/ | |
public function checkIfIsArray($items, $disk, $user, $isDefault) | |
{ | |
if (is_array($items)) { | |
foreach ($items as $index => $value) { | |
$this->uploadTheImage($items[$index], $disk, $user, $isDefault); | |
} | |
} else { | |
$this->uploadTheImage($items, $disk, $user, $isDefault); | |
} | |
} | |
/** | |
* Upload image process | |
* @param $item | |
* @param $disk | |
* @param $user | |
* @param $isDefault | |
*/ | |
public function uploadTheImage($item, $disk, $user, $isDefault) | |
{ | |
$fileName = $this->getFileNameFromBase64($item); | |
$this->savePhotoDetails($user, $fileName, $disk, $isDefault); | |
$this->moveToStorage($item, $fileName, $disk); | |
} | |
/** | |
* Check the photo status if saving or updating | |
* @param $status | |
* @param $item | |
* @param $disk | |
* @param $user | |
* @param $placeHolder | |
* @param $isDefault | |
*/ | |
public function checkPhotoStatus($status, $item, $disk, $user, $placeHolder, $isDefault) | |
{ | |
switch ($status) { | |
case 'save': | |
$this->isHavePhotos($item, $disk, $user, $placeHolder, $isDefault); | |
break; | |
case 'update': | |
$user->photos()->delete(); | |
$this->isHavePhotos($item, $disk, $user, $placeHolder, $isDefault); | |
break; | |
} | |
} | |
/** | |
* Check if there are photos to upload. | |
* @param $item | |
* @param $disk | |
* @param $user | |
* @param $placeHolder | |
* @param $isDefault | |
*/ | |
public function isHavePhotos($item, $disk, $user, $placeHolder, $isDefault) | |
{ | |
if (isset($item)) { | |
$this->checkIfIsArray($item, $disk, $user, $isDefault); | |
} else { | |
$this->savePhotoDetails($user, $placeHolder, $disk, $isDefault); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment