Skip to content

Commit

Permalink
add event
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jan 23, 2024
1 parent 4759fd7 commit 2fd8b0a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/Events/MediaFileStoredEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Finller\Media\Events;

use Finller\Media\Models\Media;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

/**
* Disptached when any new file
*/
class MediaFileStoredEvent
{
use Dispatchable, SerializesModels;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(public Media $media, public string $path)
{
//
}
}
13 changes: 9 additions & 4 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Finller\Media\Casts\GeneratedConversion;
use Finller\Media\Casts\GeneratedConversions;
use Finller\Media\Enums\MediaType;
use Finller\Media\Events\MediaFileStoredEvent;
use Finller\Media\FileDownloaders\FileDownloader;
use Finller\Media\Helpers\File;
use Finller\Media\Support\ResponsiveImagesConversionsPreset;
Expand Down Expand Up @@ -300,7 +301,8 @@ public function storeFileFromHttpFile(
$this->file_name = "{$this->name}.{$this->extension}";
$this->path = Str::finish($basePath ?? $this->generateBasePath(), '/').$this->file_name;

$this->putFile($file, fileName: $this->file_name);
$path = $this->putFile($file, fileName: $this->file_name);
event(new MediaFileStoredEvent($this, $path));

$this->save();

Expand Down Expand Up @@ -382,7 +384,8 @@ public function storeFile(
}

foreach ($otherFiles as $otherFile) {
$this->putFile($otherFile);
$path = $this->putFile($otherFile);
event(new MediaFileStoredEvent($this, $path));
}

return $this;
Expand All @@ -409,7 +412,8 @@ public function storeConversion(
}

foreach ($otherFiles as $otherFile) {
$this->putFile($otherFile);
$path = $this->putFile($otherFile);
event(new MediaFileStoredEvent($this, $path));
}

return $generatedConversion;
Expand Down Expand Up @@ -475,7 +479,8 @@ public function storeConversionFromHttpFile(

$this->putGeneratedConversion($conversion, $generatedConversion);

$generatedConversion->putFile($file, fileName: $generatedConversion->file_name);
$path = $generatedConversion->putFile($file, fileName: $generatedConversion->file_name);
event(new MediaFileStoredEvent($this, $path));

$this->save();

Expand Down
5 changes: 1 addition & 4 deletions src/Traits/InteractsWithMediaFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ public function putFile(
string|UploadedFile|HttpFile $file,
?string $name = null,
?string $fileName = null,
): string|false {
if (! $this->path) {
throw new Exception('['.static::class.']'."Can't put a file to the instance because the main path is not defined");
}
): string {

if (is_string($file) && filter_var($file, FILTER_VALIDATE_URL)) {
$file = new HttpFile(FileDownloader::getTemporaryFile($file));
Expand Down

0 comments on commit 2fd8b0a

Please sign in to comment.