Last active
January 7, 2020 20:39
-
-
Save LegitDongo/3d58dac43d4450d78fe2f8797cacaa71 to your computer and use it in GitHub Desktop.
I wanted a good way to not include the folder name in the database entry for Nova images, and also be able to track a file on any filesystem. Image name is based on the actual filename. Use at own risk, I made this for a personal project.
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 Dongo\NovaTrackedExternalImage; | |
use Illuminate\Http\Request; | |
use Laravel\Nova\Fields\Image; | |
use Storage; | |
class NovaTrackedExternalImage extends Image | |
{ | |
private $prefix = false; | |
private $download_disk; | |
public function __construct($name, $attribute = null, $disk = 'public', $storageCallback = null) | |
{ | |
parent::__construct($name, $attribute, $disk, $storageCallback); | |
$this->download_disk; | |
$this->preview(function ($file) { | |
return $this->prefix . $file; | |
}) | |
->thumbnail(function ($file) { | |
return $this->prefix . $file; | |
}) | |
->download(function ($request, $model) { | |
$name = $this->originalNameColumn ? $model->{$this->originalNameColumn} : null; | |
return Storage::disk($this->download_disk ?? $this->disk) | |
->download($this->storagePath . '/' . $model->{$this->attribute}, $name); | |
}) | |
->storeAs(function ($request) { | |
/** | |
* @var Request $request | |
*/ | |
return optional($request->file($this->attribute))->getClientOriginalName(); | |
}) | |
->store(function ($request, $model, $attribute, $requestAttribute, $disk, $storagePath) { | |
/** | |
* @var Request $request | |
*/ | |
$originalName = optional($request->file($this->attribute))->getClientOriginalName(); | |
optional($request->file($requestAttribute))->storeAs($storagePath, $originalName, $disk); | |
return [ | |
$this->attribute => $originalName | |
]; | |
}) | |
->delete(function ($request, $model, $disk, $path) { | |
if (!$path) { | |
return; | |
} | |
Storage::disk($disk)->delete($this->storagePath . '/' . $path); | |
}); | |
} | |
public function prefix(string $prefix) | |
{ | |
$this->prefix = $prefix; | |
return $this; | |
} | |
public function downloadDisk(string $disk) | |
{ | |
$this->download_disk = $disk; | |
return $this; | |
} | |
} |
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\Nova\Resources\Dongos; | |
use Dongo\NovaTrackedExternalImage\NovaTrackedExternalImage; | |
class Dongos extends Resource | |
{ | |
public function fields(Request $request) | |
{ | |
NovaTrackedExternalImage::make('Image (filename is the image name)', 'Image') | |
->disk('dongo_local_public') | |
->downloadDisk('dongo_public') | |
->prunable() | |
->path($this->storageLocation) | |
->prefix('https://www.dongo.com/images/subdirectory/'), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment