-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from Silvanite/add-default-field
make cloudinaryfield.php
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Silvanite\NovaFieldCloudinary\Fields; | ||
|
||
use Illuminate\Http\Request; | ||
use Laravel\Nova\Fields\File; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
class CloudinaryFile extends Field | ||
{ | ||
/** | ||
* Create a new field. | ||
* | ||
* @param string $name | ||
* @param string|null $attribute | ||
* @param string|null $disk | ||
* @param callable|null $storageCallback | ||
* @return void | ||
*/ | ||
public function __construct($name, $attribute = null, $disk = 'cloudinary', $storageCallback = null) | ||
{ | ||
parent::__construct($name, $attribute, $disk, $storageCallback); | ||
|
||
$this->storeAs(function (Request $request) { | ||
$name = $request->{$this->attribute}->getClientOriginalName(); | ||
$ext = '.' . $request->{$this->attribute}->getClientOriginalExtension(); | ||
|
||
return sha1($name . time()) . $ext; | ||
}); | ||
} | ||
|
||
public function preview(callable $previewUrlCallback) | ||
{ | ||
$this->previewUrlCallback = function () { | ||
return $this->value | ||
? cloudinary_file($this->value, [], $this->disk) | ||
: null; | ||
}; | ||
|
||
return $this; | ||
} | ||
} |
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