Skip to content

Commit 800a7a4

Browse files
authored
7.9.0 (#533)
* feat: files helpers * Fix styling --------- Co-authored-by: binaryk <[email protected]>
1 parent 9aa97d6 commit 800a7a4

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

docs-v2/content/en/api/fields.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,17 @@ Field::new('password')->indexCallback(function ($value) {
271271
Transform the value for the following show request:
272272

273273
```php
274-
Field::new('password')->showRequest(function ($value) {
274+
Field::new('password')->showCallback(function ($value) {
275+
return Hash::make($value);
276+
});
277+
```
278+
279+
### Resolve callback
280+
281+
Transform the value for both `show` and `index` requests:
282+
283+
```php
284+
Field::new('password')->showCallback(function ($value) {
275285
return Hash::make($value);
276286
});
277287
```
@@ -561,6 +571,45 @@ As you can see in the example above, the `store` callback is returning an array
561571
pairs are mapped onto your model's instance before it is saved to the database, allowing you to update one or many of the
562572
model's database columns after your file is stored.
563573

574+
### Customizing File Display
575+
576+
By default, Restify will display the file's stored path name. However, you may customize this behavior.
577+
578+
#### Displaying temporary url
579+
580+
For disks such as S3, you may instruct Restify to display a temporary URL to the file instead of the stored path name:
581+
582+
```php
583+
field('path')
584+
->file()
585+
->path("documents/".Auth::id())
586+
->resolveUsingTemporaryUrl()
587+
->disk('s3'),
588+
589+
```
590+
591+
The `resolveUsingTemporaryUrl` accepts 3 arguments:
592+
593+
594+
- `$resolveTemporaryUrl` - a boolean to determine if the temporary url should be resolved. Defaults to `true`.
595+
596+
- `$expiration` - A CarbonInterface to determine the time before the URL expires. Defaults to 5 minutes.
597+
598+
- `$options` - An array of options to pass to the `temporaryUrl` method of the `Illuminate\Contracts\Filesystem\Filesystem` implementation. Defaults to an empty array.
599+
600+
#### Displaying full url
601+
602+
For disks such as `public`, you may instruct Restify to display a full URL to the file instead of the stored path name:
603+
604+
```php
605+
field('path')
606+
->file()
607+
->path("documents/".Auth::id())
608+
->resolveUsingFullUrl()
609+
->disk('public'),
610+
611+
```
612+
564613
#### Storeables
565614

566615
Of course, performing all of your file storage logic within a Closure can cause your resource to become bloated. For

src/Fields/File.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Binaryk\LaravelRestify\Fields\Concerns\FileStorable;
1010
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
1111
use Binaryk\LaravelRestify\Repositories\Storable;
12+
use Carbon\CarbonInterface;
1213
use Closure;
1314
use Illuminate\Http\Request;
1415
use Illuminate\Support\Facades\Storage;
@@ -75,6 +76,56 @@ public function storeAs($storeAs): self
7576
return $this;
7677
}
7778

79+
/**
80+
* Resolve a temporary URL for s3 compatible disks.
81+
*
82+
* @param bool $resolveTemporaryUrl
83+
* @param CarbonInterface|null $expiration
84+
* @param array $options
85+
* @return $this
86+
*/
87+
public function resolveUsingTemporaryUrl(bool $resolveTemporaryUrl = true, CarbonInterface $expiration = null, array $options = []): self
88+
{
89+
if (! $resolveTemporaryUrl) {
90+
return $this;
91+
}
92+
93+
$callback = function ($value) use ($expiration) {
94+
if (! $value) {
95+
return;
96+
}
97+
98+
return Storage::disk($this->getStorageDisk())->temporaryUrl(
99+
$value,
100+
$expiration ?? now()->addMinutes(5)
101+
);
102+
};
103+
104+
$this->resolveCallback($callback);
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* Resolve a full path for the file.
111+
*
112+
* @return $this
113+
*/
114+
public function resolveUsingFullUrl(): self
115+
{
116+
$callback = function ($value) {
117+
if (! $value) {
118+
return;
119+
}
120+
121+
return Storage::disk($this->getStorageDisk())->url($value);
122+
};
123+
124+
$this->resolveCallback($callback);
125+
126+
return $this;
127+
}
128+
78129
/**
79130
* Prepare the storage callback.
80131
*

0 commit comments

Comments
 (0)