Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add s3 support #30

Open
wants to merge 3 commits into
base: feature/enable-webp-by-default
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/filament-media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
'ogg',
],
],
'temporary_directory_path' => storage_path('filament-media-library/tmp'),
];
34 changes: 32 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ return [
'ogg',
],
],
'temporary_directory_path' => storage_path('filament-media-library/tmp'),
];


```

### Extensions
Expand All @@ -111,6 +110,37 @@ This configuration can be adjusted as desired.
The format generation action is a button that will generate all the formats for the given attachment.
This can be used on the Media Library as a bulk action. This action can be disabled by setting the `enable-format-generate-action` to false.

### S3 support

To use S3 as a storage, you can use the `Codedor\MediaLibrary\Conversions\S3Conversion` class.

```php
return [
'conversion' => \Codedor\MediaLibrary\Conversions\S3Conversion::class,
// ...
];
```

Then switch your filesystem to S3 in the `config/filesystems.php` file.

```php
return [
'disks' => [
'public' => [
'visibility' => 'public',
'driver' => 's3',
'endpoint' => env('AWS_ENDPOINT', 'http://127.0.0.1:9000'),
'use_path_style_endpoint' => true,
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
'root' => 'public',
],
],
];
```

## Formats

### Register models
Expand Down
2 changes: 2 additions & 0 deletions resources/views/livewire/formatter-modal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class="py-8 w-full flex-col gap-2"
ready() {
window.cropper.setData(previousData || {})
},
checkCrossOrigin: false,
})
},
submit () {
Expand Down Expand Up @@ -79,6 +80,7 @@ class="py-8 w-full flex-col gap-2"
id="filament-media-library::formatter"
wire:key="filament-media-library::formatter-{{ $attachment->id }}"
style="max-width: 100%; max-height: 68vh"
crossorigin="anonymous"
>
</div>

Expand Down
62 changes: 62 additions & 0 deletions src/Conversions/S3Conversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Codedor\MediaLibrary\Conversions;

use Codedor\MediaLibrary\Formats\Format;
use Codedor\MediaLibrary\Models\Attachment;
use Codedor\MediaLibrary\Support\TemporaryDirectory;
use Illuminate\Support\Str;
use Spatie\Image\Image;

class S3Conversion implements Conversion
{
public function convert(Attachment $attachment, Format $format, bool $force = false): bool
{
if (! is_convertible_image($attachment->extension)) {
return false;
}

$formatName = $format->filename($attachment);
$savePath = $attachment->directory . '/' . $formatName;

if (array_key_exists('format', $format->definition()->toArray()[0])) {
$savePath = Str::replaceLast(
$attachment->extension,
$format->definition()->toArray()[0]['format'],
$savePath
);
}

$formatPath = "$attachment->directory/$formatName";

if (
$force ||
! $attachment->getStorage()->exists($formatPath)
) {
$temporaryDirectory = TemporaryDirectory::create();
$tempPath = $temporaryDirectory->path(Str::random(16) . '.' . $attachment->extension);
$disk = $attachment->getStorage();

file_put_contents($tempPath, $disk->readStream($attachment->file_path));

Image::load($tempPath)
->manipulate($format->definition())
->save();

$file = fopen($tempPath, 'r');

$disk->put(
$formatPath,
$file,
);

if (is_resource($file)) {
fclose($file);
}

$temporaryDirectory->delete();
}

return true;
}
}
12 changes: 10 additions & 2 deletions src/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ public function scopeSearch(Builder $query, ?string $search = ''): Builder

public function getUrlAttribute(): string
{
return $this->getStorage()
->url("{$this->directory}/{$this->filename}");
$disk = $this->getStorage();

if ($disk->providesTemporaryUrls()) {
return $disk->temporaryUrl(
"{$this->directory}/{$this->filename}",
now()->addMinutes(5)
);
}

return $disk->url("{$this->directory}/{$this->filename}");
}

public function getFilenameAttribute(): string
Expand Down
11 changes: 10 additions & 1 deletion src/Models/Traits/HasFormats.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ public function getFormat(string $name, ?string $extension = null): ?string
return null;
}

return $attachment->getStorage()->url(
$disk = $attachment->getStorage();

if ($disk->providesTemporaryUrls()) {
return $disk->temporaryUrl(
"{$attachment->directory}/{$format->filename($attachment)}",
now()->addMinutes(5)
);
}

return $disk->url(
"{$attachment->directory}/{$format->filename($attachment)}"
);
}
Expand Down
21 changes: 21 additions & 0 deletions src/Support/TemporaryDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Codedor\MediaLibrary\Support;

use Illuminate\Support\Str;
use Spatie\TemporaryDirectory\TemporaryDirectory as BaseTemporaryDirectory;

class TemporaryDirectory
{
public static function create(): BaseTemporaryDirectory
{
return new BaseTemporaryDirectory(static::getTemporaryDirectoryPath());
}

protected static function getTemporaryDirectoryPath(): string
{
$path = config('filament-media-library.temporary_directory_path') ?? storage_path('filament-media-library/tmp');

return $path . DIRECTORY_SEPARATOR . Str::random(32);
}
}