Skip to content

Commit

Permalink
7.9.0 (#533)
Browse files Browse the repository at this point in the history
* feat: files helpers

* Fix styling

---------

Co-authored-by: binaryk <[email protected]>
  • Loading branch information
binaryk and binaryk authored Feb 3, 2023
1 parent 9aa97d6 commit 800a7a4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
51 changes: 50 additions & 1 deletion docs-v2/content/en/api/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
```
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 800a7a4

Please sign in to comment.