Skip to content

Commit

Permalink
# added readme for existing media
Browse files Browse the repository at this point in the history
# added configuration to enable existing media feature
  • Loading branch information
ebess committed Sep 3, 2019
1 parent afe8a2d commit d72c23c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 1 deletion.
Binary file added docs/existing-media-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/existing-media.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ images and order them by drag and drop.
* [Generic file management](#generic-file-management)
* [Single image upload](#single-image-upload)
* [Multiple image upload](#multiple-image-upload)
* [Selecting existing media](#selecting-existing-media)
* [Names of uploaded images](#names-of-uploaded-images)
* [Image cropping](#image-cropping)
* [Custom properties](#custom-properties)
Expand All @@ -28,6 +29,10 @@ images and order them by drag and drop.
composer require ebess/advanced-nova-media-library
```

```bash
artisan vendor:publish --tag=nova-media-library
```

## Model media configuration

Let's assume you configured your model to use the media library like following:
Expand Down Expand Up @@ -103,6 +108,32 @@ public function fields(Request $request)
}
```

## Selecting existing media

![Selecting existing media](https://raw.githubusercontent.com/ebess/advanced-nova-media-library/master/docs/existing-media.png)
![Selecting existing media 2](https://raw.githubusercontent.com/ebess/advanced-nova-media-library/master/docs/existing-media-2.png)

If you upload the same media files to multiple models and you do not want to select it from the file system
all over again, use this feature. Selecting an already existing media will **copy it**.

**Attention**: This feature will expose an endpoint to every user of your application to search existing media.
If your media upload / custom properties on the media models are confidential, **do not enable this feature!**

* Publish the config files if you did not yet
```bash
artisan vendor:publish --tag=nova-media-library
```
* Enable this feature in config file *config/nova-media-library*
```php
return [
'enable-existing-media' => true,
];
```
* Enable the selection of existing media field
```php
Images::make('Image')->enableExistingMedia(),
```

## Names of uploaded images

The default filename of the new uploaded file is the original filename. You can change this with the help of the function `setFileName`, which takes a callback function as the only param. This callback function has three params: `$originalFilename` (the original filename like `Fotolia 4711.jpg`), `$extension` (file extension like `jpg`), `$model` (the current model). Here are just 2 examples of what you can do:
Expand Down
4 changes: 4 additions & 0 deletions src/AdvancedNovaMediaLibraryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class AdvancedNovaMediaLibraryServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__ . '/config/nova-media-library.php' => config_path('nova-media-library.php'),
], 'nova-media-library');

$this->app->booted(function () {
$this->routes();
});
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/HandlesExistingMediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait HandlesExistingMediaTrait
{
public function enableExistingMedia(): self
{
return $this->withMeta(['existingMedia' => true]);
return $this->withMeta(['existingMedia' => (bool) config('nova-media-library.enable-existing-media')]);
}

private function addExistingMedia(NovaRequest $request, $data, HasMedia $model, string $collection, Collection $medias): Collection
Expand Down
5 changes: 5 additions & 0 deletions src/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use Ebess\AdvancedNovaMediaLibrary\Http\Requests\MediaRequest;
use Ebess\AdvancedNovaMediaLibrary\Http\Resources\MediaResource;
use Exception;

class MediaController extends Controller
{
public function index(MediaRequest $request)
{
if (!config('nova-media-library.enable-existing-media')) {
throw new Exception('You need to enable the `existing media` feature via config.');
}

$mediaClass = config('medialibrary.media_model');
$mediaClassIsSearchable = method_exists($mediaClass, 'search');

Expand Down
5 changes: 5 additions & 0 deletions src/config/nova-media-library.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'enable-existing-media' => false,
];

0 comments on commit d72c23c

Please sign in to comment.