Skip to content

Commit

Permalink
Merge pull request #13 from Silvanite/add-default-field
Browse files Browse the repository at this point in the history
make cloudinaryfield.php
  • Loading branch information
crumb1e authored Aug 9, 2019
2 parents 77e78f0 + cc30697 commit 72b81a2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src-php/Fields/CloudinaryFile.php
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;
}
}
17 changes: 17 additions & 0 deletions src-php/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,20 @@ function cloudinary_audio(string $path, array $options = [], string $disk = 'clo
return cloudinary_fetch($path, $options, $disk);
}
}

if (!function_exists('cloudinary_file')) {
/**
* Get an optimised public url for a file by it's public id
*
* @param string $path
* @param array $options
* @param string $disk
* @return string
*/
function cloudinary_file(string $path, array $options = [], string $disk = 'cloudinary')
{
$options = array_merge(['resource_type' => 'auto'], $options);

return cloudinary_fetch($path, $options, $disk);
}
}

0 comments on commit 72b81a2

Please sign in to comment.