Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to use optional parameters (i.e. support of folders) #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,24 @@ public function fields(Request $request)
}
```

This field sets the disk in use to `Cloudinary` and ensures the media is stored in the database field with the correct file extension.
This field sets the disk in use to `Cloudinary` and ensures the media is stored in the database field with the correct file extension.

## Configuration

If you want to use any of the [optional parameters](https://cloudinary.com/documentation/image_upload_api_reference#optional_parameters) of the Cloudinary Upload API, you can use the `cloudinary` method to send additional parameters.

The following code uploads images to the images folder instead of the Cloudinary root.

```php
use Silvanite\NovaFieldCloudinary\Fields\CloudinaryImage;

public function fields(Request $request)
{
return [
...
CloudinaryImage::make('Image')->cloudinary([
'folder' => 'images'
]),
]
}
```
16 changes: 11 additions & 5 deletions src-php/Adapters/CloudinaryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ class CloudinaryAdapter extends CloudinaryBaseAdapter
*/
public function writeStream($path, $resource, Config $config)
{

$cloudinary_options = $config->get('cloudinary') ?? [];

$path = pathinfo($path)['filename'];

$resource_metadata = stream_get_meta_data($resource);
$uploaded_metadata = Uploader::upload($resource_metadata['uri'], [
// Create the options object
$options = array_merge([
'public_id' => $path,
'resource_type' => 'auto',
]);

'resource_type' => 'auto'
], $cloudinary_options);

$resource_metadata = stream_get_meta_data($resource);
$uploaded_metadata = Uploader::upload($resource_metadata['uri'], $options);

return $uploaded_metadata;
}

Expand Down
17 changes: 15 additions & 2 deletions src-php/Fields/CloudinaryAudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,27 @@ public function __construct($name, $attribute = null, $disk = 'cloudinary', $sto
{
parent::__construct($name, $attribute, $disk, $storageCallback);

$this->storeAs(function (Request $request) {
$this->store(function(Request $request, $model, $attribute, $requestAttribute){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->store(function(Request $request, $model, $attribute, $requestAttribute){
$this->store(function(Request $request, $requestAttribute) {


$filename = $request->file($requestAttribute)->store($this->getStorageDir(), [
'disk' => $this->getStorageDisk(),
'cloudinary' => $this->cloudinaryOptions
]);

// If a folder is specified we ensure a trailing slash
// If no folder is specified we ensure no beginning slash
$path = array_key_exists('folder',$this->cloudinaryOptions) ? rtrim($this->cloudinaryOptions['folder'], '/') . '/' : '';

return $path . $filename;

})->storeAs(function (Request $request) {
$name = $request->{$this->attribute}->getClientOriginalName();
$ext = '.' . $request->{$this->attribute}->getClientOriginalExtension();

return sha1($name . time()) . $ext;
})->delete(function (Request $request, $model) {
$path = pathinfo($model->{$this->attribute});
Storage::disk($this->disk)->delete($path['filename']);
Storage::disk($this->disk)->delete($path['dirname'] .'/'. $path['filename']);
return $this->columnsThatShouldBeDeleted();
});
}
Expand Down
22 changes: 19 additions & 3 deletions src-php/Fields/CloudinaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class CloudinaryFile extends File
{

use CloudinaryOptions;

/**
* Create a new field.
*
Expand All @@ -20,14 +23,27 @@ public function __construct($name, $attribute = null, $disk = 'cloudinary', $sto
{
parent::__construct($name, $attribute, $disk, $storageCallback);

$this->storeAs(function (Request $request) {
$this->store(function(Request $request, $model, $attribute, $requestAttribute){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->store(function(Request $request, $model, $attribute, $requestAttribute){
$this->store(function(Request $request, $requestAttribute) {


$filename = $request->file($requestAttribute)->store($this->getStorageDir(), [
'disk' => $this->getStorageDisk(),
'cloudinary' => $this->cloudinaryOptions
]);

// If a folder is specified we ensure a trailing slash
// If no folder is specified we ensure no beginning slash
$path = array_key_exists('folder',$this->cloudinaryOptions) ? rtrim($this->cloudinaryOptions['folder'], '/') . '/' : '';

return $path . $filename;

})->storeAs(function (Request $request) {
$name = $request->{$this->attribute}->getClientOriginalName();
$ext = '.' . $request->{$this->attribute}->getClientOriginalExtension();

return sha1($name . time()) . $ext;
})>delete(function (Request $request, $model) {
})->delete(function (Request $request, $model) {
$path = pathinfo($model->{$this->attribute});
Storage::disk($this->disk)->delete($path['filename']);
Storage::disk($this->disk)->delete($path['dirname'] .'/'. $path['filename']);
return $this->columnsThatShouldBeDeleted();
});
}
Expand Down
19 changes: 17 additions & 2 deletions src-php/Fields/CloudinaryImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class CloudinaryImage extends Image
{
use CloudinaryOptions;

/**
* Create a new field.
*
Expand All @@ -21,7 +23,20 @@ public function __construct($name, $attribute = null, $disk = 'cloudinary', $sto
{
parent::__construct($name, $attribute, $disk, $storageCallback);

$this->thumbnail(function () {
$this->store(function(Request $request, $model, $attribute, $requestAttribute){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->store(function(Request $request, $model, $attribute, $requestAttribute){
$this->store(function(Request $request, $requestAttribute) {


$filename = $request->file($requestAttribute)->store($this->getStorageDir(), [
'disk' => $this->getStorageDisk(),
'cloudinary' => $this->cloudinaryOptions
]);

// If a folder is specified we ensure a trailing slash
// If no folder is specified we ensure no beginning slash
$path = array_key_exists('folder',$this->cloudinaryOptions) ? rtrim($this->cloudinaryOptions['folder'], '/') . '/' : '';

return $path . $filename;

})->thumbnail(function () {
return $this->value ? cloudinary_image($this->value, [
'width' => 64,
'height' => 64,
Expand All @@ -40,7 +55,7 @@ public function __construct($name, $attribute = null, $disk = 'cloudinary', $sto
}, $this->value);
})->delete(function (Request $request, $model) {
$path = pathinfo($model->{$this->attribute});
Storage::disk($this->disk)->delete($path['filename']);
Storage::disk($this->disk)->delete($path['dirname'] .'/'. $path['filename']);
return $this->columnsThatShouldBeDeleted();
});
}
Expand Down
16 changes: 16 additions & 0 deletions src-php/Fields/CloudinaryOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Silvanite\NovaFieldCloudinary\Fields;

trait CloudinaryOptions
{

protected $cloudinaryOptions = [];

public function cloudinary($options = []){
$this->cloudinaryOptions = $options;

return $this;
}

}
17 changes: 15 additions & 2 deletions src-php/Fields/CloudinaryVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,27 @@ public function __construct($name, $attribute = null, $disk = 'cloudinary', $sto
{
parent::__construct($name, $attribute, $disk, $storageCallback);

$this->storeAs(function (Request $request) {
$this->store(function(Request $request, $model, $attribute, $requestAttribute){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->store(function(Request $request, $model, $attribute, $requestAttribute){
$this->store(function(Request $request, $requestAttribute) {


$filename = $request->file($requestAttribute)->store($this->getStorageDir(), [
'disk' => $this->getStorageDisk(),
'cloudinary' => $this->cloudinaryOptions
]);

// If a folder is specified we ensure a trailing slash
// If no folder is specified we ensure no beginning slash
$path = array_key_exists('folder',$this->cloudinaryOptions) ? rtrim($this->cloudinaryOptions['folder'], '/') . '/' : '';

return $path . $filename;

})->storeAs(function (Request $request) {
$name = $request->{$this->attribute}->getClientOriginalName();
$ext = '.' . $request->{$this->attribute}->getClientOriginalExtension();

return sha1($name . time()) . $ext;
})->delete(function (Request $request, $model) {
$path = pathinfo($model->{$this->attribute});
Storage::disk($this->disk)->delete($path['filename']);
Storage::disk($this->disk)->delete($path['dirname'] .'/'. $path['filename']);
return $this->columnsThatShouldBeDeleted();
});
}
Expand Down