Skip to content

Commit

Permalink
support url and otherfiles in conversion and main file
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Dec 3, 2023
1 parent 2387d5f commit add2eb2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 28 deletions.
89 changes: 70 additions & 19 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File as SupportFile;
use Illuminate\Support\Str;
use Spatie\TemporaryDirectory\TemporaryDirectory;

Expand Down Expand Up @@ -218,49 +217,103 @@ public function storeFileFromUrl(
return $this;
}

/**
* @param (string|UploadedFile|HttpFile)[] $otherFiles any other file to store in the same directory
*/
public function storeFile(
string|UploadedFile|HttpFile $file,
string $collection_name = null,
string $basePath = null,
string $name = null,
string $disk = null
string $disk = null,
array $otherFiles = []
): static {
if ($file instanceof UploadedFile || $file instanceof HttpFile) {
return $this->storeFileFromHttpFile($file, $collection_name, $basePath, $name, $disk);
$this->storeFileFromHttpFile($file, $collection_name, $basePath, $name, $disk);
} elseif (filter_var($file, FILTER_VALIDATE_URL)) {
$this->storeFileFromUrl($file, $collection_name, $basePath, $name, $disk);
} else {
$this->storeFileFromHttpFile(new HttpFile($file), $collection_name, $basePath, $name, $disk);
}

if (filter_var($file, FILTER_VALIDATE_URL)) {
return $this->storeFileFromUrl($file, $collection_name, $basePath, $name, $disk);
foreach ($otherFiles as $otherFile) {
$this->putFile($otherFile);
}

return $this;
}

/**
* @param (string|HttpFile)[] $otherFiles
* @param (string|UploadedFile|HttpFile)[] $otherFiles any other file to store in the same directory
*/
public function storeConversion(
HttpFile|string $file,
string|UploadedFile|HttpFile $file,
string $conversion,
string $name = null,
string $basePath = null,
string $state = 'success',
array $otherFiles = []
): GeneratedConversion {
$file = is_string($file) ? new HttpFile($file) : $file;
$name = File::sanitizeFilename($name ?? SupportFile::name($file->getPathname()));

$extension = $file->guessExtension();
if ($file instanceof UploadedFile || $file instanceof HttpFile) {
$generatedConversion = $this->storeConversionFromHttpFile($file, $conversion, $name, $basePath, $state);
} elseif (filter_var($file, FILTER_VALIDATE_URL)) {
$generatedConversion = $this->storeConversionFromUrl($file, $conversion, $name, $basePath, $state);
} else {
$generatedConversion = $this->storeConversionFromHttpFile(new HttpFile($file), $conversion, $name, $basePath, $state);
}

foreach ($otherFiles as $otherFile) {
$this->putFile($otherFile);
}

return $generatedConversion;
}

public function storeConversionFromUrl(
string $url,
string $conversion,
string $name = null,
string $basePath = null,
string $state = 'success',
): GeneratedConversion {
$temporaryDirectory = (new TemporaryDirectory())
->location(storage_path('media-tmp'))
->create();

$path = FileDownloader::getTemporaryFile($url, $temporaryDirectory);

$generatedConversion = $this->storeConversionFromHttpFile(new HttpFile($path), $conversion, $name, $basePath, $state);

$temporaryDirectory->delete();

return $generatedConversion;
}

public function storeConversionFromHttpFile(
UploadedFile|HttpFile $file,
string $conversion,
string $name = null,
string $basePath = null,
string $state = 'success',
): GeneratedConversion {
$name = File::sanitizeFilename($name ?? File::name($file->getPathname()));

$extension = File::extension($file);
$file_name = "{$name}.{$extension}";
$mime_type = $file->getMimeType();
$mime_type = File::mimeType($file);
$type = MediaType::tryFromMimeType($mime_type);
$dimension = File::dimension($file->getPathname(), type: $type);

$existingConversion = $this->getGeneratedConversion($name);

$existingConversion?->delete();

$generatedConversion = new GeneratedConversion(
name: $name,
extension: $extension,
file_name: $file_name,
path: ($basePath ?? $this->generateBasePath($conversion)).$file_name,
path: Str::finish($basePath ?? $this->generateBasePath($conversion), '/').$file_name,
mime_type: $mime_type,
type: $type,
state: $state,
Expand All @@ -269,28 +322,26 @@ public function storeConversion(
width: $dimension->getWidth(),
aspect_ratio: $dimension?->getRatio(forceStandards: false)->getValue(),
size: $file->getSize(),
created_at: $existingConversion?->created_at
);

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

$generatedConversion->putFile($file, fileName: $generatedConversion->file_name);

foreach ($otherFiles as $otherFile) {
$generatedConversion->putFile($otherFile);
}

$this->save();

return $generatedConversion;
}

public function deleteGeneratedConversion(string $converion): static
public function deleteGeneratedConversion(string $converion): GeneratedConversion
{
$this->getGeneratedConversion($converion)?->delete();
$generatedConversion = $this->getGeneratedConversion($converion);
$generatedConversion?->delete();
$this->forgetGeneratedConversion($converion);
$this->save();

return $this;
return $generatedConversion;
}

public function deleteGeneratedConversions(): static
Expand Down
18 changes: 11 additions & 7 deletions src/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function media(): MorphMany
*/
public function getMedia(string $collection_name = null): EloquentCollection
{
return $this->media->where('collection_name', $collection_name ?? config('media.default_collection_name'));
return $this->media
->when($collection_name, fn ($collection) => $collection->where('collection_name', $collection_name));
}

/**
Expand Down Expand Up @@ -85,17 +86,18 @@ public function hasMediaCollection(string $collection_name): bool
}

/**
* @param int[] $except
* @param int[] $except Array of Media Ids
* @return Collection<int, Media> The deleted media list
*/
public function clearMediaCollection(string $collection_name, array $except = []): static
public function clearMediaCollection(string $collection_name, array $except = []): Collection
{
$this->getMedia($collection_name)
$media = $this->getMedia($collection_name)
->except($except)
->each(function (Media $media) {
$media->delete();
});

return $this;
return $media;
}

public function addMedia(string|UploadedFile $file, string $collection_name = null, string $name = null, string $disk = null): Media
Expand All @@ -106,10 +108,12 @@ public function addMedia(string|UploadedFile $file, string $collection_name = nu

if (! $collection) {
$class = static::class;
throw new Exception("The media collection {$collection_name} is not registered for {$class}");
throw new Exception("The media collection {$collection_name} is not registered for the model {$class}.");
}

$media = new Media();
$model = config('media.model');
/** @var Media $media */
$media = new $model();

$media->model()->associate($this);

Expand Down
10 changes: 8 additions & 2 deletions src/Traits/InteractsWithMediaFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Finller\Media\Traits;

use Exception;
use Finller\Media\FileDownloaders\FileDownloader;
use Finller\Media\Helpers\File;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Http\File as HttpFile;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File as SupportFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Number;
Expand Down Expand Up @@ -76,15 +78,19 @@ public function getDirname(): ?string
* Put a file in the same directory than the main file
*/
public function putFile(
HttpFile|string $file,
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");
}

$file = $file instanceof HttpFile ? $file : new HttpFile($file);
if (is_string($file) && filter_var($file, FILTER_VALIDATE_URL)) {
$file = new HttpFile(FileDownloader::getTemporaryFile($file));
} elseif (is_string($file)) {
$file = new HttpFile($file);
}

$fileName ??= File::extractFilename($file, $name);

Expand Down

0 comments on commit add2eb2

Please sign in to comment.