Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/SethSharp/BlogCrud
Browse files Browse the repository at this point in the history
  • Loading branch information
SethSharp committed Apr 13, 2024
2 parents 3665c04 + a6a0b0d commit ec0edea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/Actions/Blogs/StoreBlogCoverAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use SethSharp\BlogCrud\Models\Blog\Blog;
use SethSharp\BlogCrud\Actions\Files\DeleteS3File;

class StoreBlogCoverAction
{
Expand All @@ -15,7 +17,7 @@ class StoreBlogCoverAction
* @param string $path
* @return string
*/
public function __invoke(UploadedFile $file, int $blogId, string $path = '/cover-images/'): string
public function __invoke(UploadedFile $file, Blog $blog, string $path = '/cover-images/'): string
{
$newFile = config('blog-crud.image_driver')->read($file)->scale(500, 500)->encode();

Expand All @@ -24,10 +26,15 @@ public function __invoke(UploadedFile $file, int $blogId, string $path = '/cover

$filename = uniqid() . '_' . $file->getClientOriginalName();

$path = $structure . 'blogs/' . $blogId . '/cover-images/' . $filename;
$path = $structure . 'blogs/' . $blog->id . '/cover-images/' . $filename;

Storage::disk('s3')->put($path, $newFile);

// destory previous cover image
if ($blog->cover_image) {
app(DeleteS3File::class)($blog->cover_image);
}

return $path;
}
}
2 changes: 1 addition & 1 deletion src/Actions/Blogs/UpdateBlogAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function __invoke(Blog $blog, UpdateBlogRequest $updateBlogRequest): Blog
// upload cover image
$coverImagePath = $blog->cover_image;
if ($coverImage = $updateBlogRequest->file('cover_image')) {
$coverImagePath = app(StoreBlogCoverAction::class)($coverImage, $blog->id);
$coverImagePath = app(StoreBlogCoverAction::class)($coverImage, $blog);
}

// dynamically update model with validated data
Expand Down
27 changes: 27 additions & 0 deletions src/Actions/Files/DeleteS3File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace SethSharp\BlogCrud\Actions\Files;

use Exception;
use Illuminate\Support\Facades\Storage;

class DeleteS3File
{
/**
* Deletes S3 file
*
* @param string $path
* @return bool
*/
public function __invoke(string $path): bool
{
try {
Storage::disk('s3')->delete($path);

return true;
} catch (Exception $e) {

return false;
}
}
}
9 changes: 1 addition & 8 deletions src/Actions/Files/DestroyFileAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ class DestroyFileAction
*/
public function __invoke(File $file): bool
{
try {
Storage::disk('s3')->delete($file->path);

return true;
} catch (Exception $e) {

return false;
}
return app(DeleteS3File::class)($file->path);
}
}

0 comments on commit ec0edea

Please sign in to comment.