Skip to content

Commit

Permalink
Merge pull request #16 from YasserElgammal/develop
Browse files Browse the repository at this point in the history
Improve Storage Management
  • Loading branch information
YasserElgammal authored Oct 12, 2024
2 parents ae84026 + 2269319 commit 7af0dc9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
12 changes: 4 additions & 8 deletions app/Http/Controllers/Admin/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ public function index()


/**
* Update the specified resource in storage.
* Update the authenticated user's account
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
* @param \App\Http\Requests\Admin\UpdateAccountRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(UpdateAccountRequest $request)
{
$user = auth()->user();
$data = $request->safe()->except('avatar');

if ($request->hasfile('avatar')) {
if ($user->avatar != null) {
Storage::delete($user->avatar);
}
$get_file = $request->file('avatar')->store('images/profiles');
$user->avatar = $get_file;
$data['avatar'] = $get_file;
}

$user->update($data);
Expand Down
4 changes: 0 additions & 4 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public function update(PostRequest $request, Post $post)
$post_data = $request->safe()->except('image');

if ($request->hasfile('image')) {
Storage::delete($post->image);
$get_file = $request->file('image')->store('images/posts');
$post_data['image'] = $get_file;
}
Expand All @@ -113,9 +112,6 @@ public function update(PostRequest $request, Post $post)
*/
public function destroy(Post $post)
{
if ($post->image != null) {
Storage::delete($post->image);
}
$post->delete();

return back()->with('message', trans('admin.post_deleted'));
Expand Down
24 changes: 18 additions & 6 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\Storage;

class Post extends Model
{
Expand All @@ -15,7 +16,7 @@ class Post extends Model
protected function createdAt(): Attribute
{
return Attribute::make(
get: fn ($value) => Carbon::parse($value)->diffForHumans()
get: fn($value) => Carbon::parse($value)->diffForHumans()
);
}

Expand All @@ -36,11 +37,7 @@ public function getPreviousAttribute()

public function getImageAttribute($value)
{
if (!$value) {
return asset('import/assets/post-pic-dummy.png');
}

return asset("storage/$value");
return $value ? asset("storage/$value") : asset('import/assets/post-pic-dummy.png');
}

public function category()
Expand All @@ -65,4 +62,19 @@ public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}

public static function boot()
{
parent::boot();
static::updating(function ($post) {
if ($post->isDirty('image') && !is_null($post->getRawOriginal('image'))) {
Storage::delete($post->getRawOriginal('image'));
}
});
static::deleting(function ($post) {
if (!is_null($post->getRawOriginal('image'))) {
Storage::delete($post->getRawOriginal('image'));
}
});
}
}
18 changes: 13 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
Expand Down Expand Up @@ -52,11 +53,7 @@ class User extends Authenticatable

public function getAvatarAttribute($value)
{
if (!$value) {
return asset('import/assets/profile-pic-dummy.png');
}

return asset("storage/$value");
return $value ? asset("storage/$value") : asset('import/assets/profile-pic-dummy.png');
}

public function posts()
Expand Down Expand Up @@ -88,4 +85,15 @@ public function comments()
{
return $this->hasMany(Comment::class);
}


public static function boot()
{
parent::boot();
static::updating(function ($user) {
if ($user->isDirty('avatar') && !is_null($user->getRawOriginal('avatar'))) {
Storage::delete($user->getRawOriginal('avatar'));
}
});
}
}

0 comments on commit 7af0dc9

Please sign in to comment.