From 800a7a47aabbe32066e0d190733ea26ab1e366d3 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Fri, 3 Feb 2023 17:50:02 +0200 Subject: [PATCH] 7.9.0 (#533) * feat: files helpers * Fix styling --------- Co-authored-by: binaryk --- docs-v2/content/en/api/fields.md | 51 +++++++++++++++++++++++++++++++- src/Fields/File.php | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/docs-v2/content/en/api/fields.md b/docs-v2/content/en/api/fields.md index c72eb195..cb894e8e 100644 --- a/docs-v2/content/en/api/fields.md +++ b/docs-v2/content/en/api/fields.md @@ -271,7 +271,17 @@ Field::new('password')->indexCallback(function ($value) { Transform the value for the following show request: ```php -Field::new('password')->showRequest(function ($value) { +Field::new('password')->showCallback(function ($value) { + return Hash::make($value); +}); +``` + +### Resolve callback + +Transform the value for both `show` and `index` requests: + +```php +Field::new('password')->showCallback(function ($value) { return Hash::make($value); }); ``` @@ -561,6 +571,45 @@ As you can see in the example above, the `store` callback is returning an array pairs are mapped onto your model's instance before it is saved to the database, allowing you to update one or many of the model's database columns after your file is stored. +### Customizing File Display + +By default, Restify will display the file's stored path name. However, you may customize this behavior. + +#### Displaying temporary url + +For disks such as S3, you may instruct Restify to display a temporary URL to the file instead of the stored path name: + +```php + field('path') + ->file() + ->path("documents/".Auth::id()) + ->resolveUsingTemporaryUrl() + ->disk('s3'), + +``` + +The `resolveUsingTemporaryUrl` accepts 3 arguments: + + +- `$resolveTemporaryUrl` - a boolean to determine if the temporary url should be resolved. Defaults to `true`. + +- `$expiration` - A CarbonInterface to determine the time before the URL expires. Defaults to 5 minutes. + +- `$options` - An array of options to pass to the `temporaryUrl` method of the `Illuminate\Contracts\Filesystem\Filesystem` implementation. Defaults to an empty array. + +#### Displaying full url + +For disks such as `public`, you may instruct Restify to display a full URL to the file instead of the stored path name: + +```php + field('path') + ->file() + ->path("documents/".Auth::id()) + ->resolveUsingFullUrl() + ->disk('public'), + +``` + #### Storeables Of course, performing all of your file storage logic within a Closure can cause your resource to become bloated. For diff --git a/src/Fields/File.php b/src/Fields/File.php index 8cfba08e..7b422516 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -9,6 +9,7 @@ use Binaryk\LaravelRestify\Fields\Concerns\FileStorable; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; use Binaryk\LaravelRestify\Repositories\Storable; +use Carbon\CarbonInterface; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; @@ -75,6 +76,56 @@ public function storeAs($storeAs): self return $this; } + /** + * Resolve a temporary URL for s3 compatible disks. + * + * @param bool $resolveTemporaryUrl + * @param CarbonInterface|null $expiration + * @param array $options + * @return $this + */ + public function resolveUsingTemporaryUrl(bool $resolveTemporaryUrl = true, CarbonInterface $expiration = null, array $options = []): self + { + if (! $resolveTemporaryUrl) { + return $this; + } + + $callback = function ($value) use ($expiration) { + if (! $value) { + return; + } + + return Storage::disk($this->getStorageDisk())->temporaryUrl( + $value, + $expiration ?? now()->addMinutes(5) + ); + }; + + $this->resolveCallback($callback); + + return $this; + } + + /** + * Resolve a full path for the file. + * + * @return $this + */ + public function resolveUsingFullUrl(): self + { + $callback = function ($value) { + if (! $value) { + return; + } + + return Storage::disk($this->getStorageDisk())->url($value); + }; + + $this->resolveCallback($callback); + + return $this; + } + /** * Prepare the storage callback. *