From f8fbce22852d9966dec5c97438aab9871700d53f Mon Sep 17 00:00:00 2001 From: Les De Ridder Date: Sun, 29 May 2016 02:14:29 +0200 Subject: [PATCH] Implement paste storage --- app/Paste.php | 78 ++++++++++++++++++++++++++++++++++++++---- config/filesystems.php | 21 ++++-------- 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/app/Paste.php b/app/Paste.php index c698f16..5e2b134 100644 --- a/app/Paste.php +++ b/app/Paste.php @@ -4,6 +4,9 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use Carbon\Carbon; +use Storage; + class Paste extends Model { public $incrementing = false; @@ -14,14 +17,9 @@ class Paste extends Model return $this->hasOne(Deletion::class); } - public function isDeleted() - { - return $this->deletion !== null; - } - public function soft_delete($reason, $deleted_by) { - if($this->isDeleted()) + if($this->deleted) { return false; } @@ -35,4 +33,72 @@ class Paste extends Model return true; } + + public function try_hard_delete() + { + if(Storage::disk('ephemeral')->exists($this->id) && $this->has_expired) + { + Storage::disk('ephemeral')->delete($this->id); + } + } + + public function getDeletedAttribute() + { + return $this->deletion !== null; + } + + public function getExpiresAttribute() + { + return $this->expires_at !== null; + } + + public function getHasExpiredAttribute() + { + return $this->expires && $this->expires_at < Carbon::now(); + } + + public function getContentAttribute() + { + if($this->deleted) + { + return null; + } + + if($this->expires) + { + if($this->has_expired) + { + $this->try_hard_delete(); + + return null; + } + else + { + return Storage::disk('ephemeral')->get($this->id); + } + } + else + { + return Storage::disk('persistent')->get($this->id); + } + } + + public function setContentAttribute($content) + { + if($this->deleted || $this->has_expired) + { + //TODO: Throw an exception + + return; + } + + if($this->expires) + { + Storage::disk('ephemeral')->put($this->id, $content); + } + else + { + Storage::disk('persistent')->put($this->id, $content); + } + } } diff --git a/config/filesystems.php b/config/filesystems.php index 75b5002..791ec27 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -15,7 +15,7 @@ return [ | */ - 'default' => 'local', + 'default' => 'null', /* |-------------------------------------------------------------------------- @@ -28,7 +28,7 @@ return [ | */ - 'cloud' => 's3', + 'cloud' => 'null', /* |-------------------------------------------------------------------------- @@ -43,23 +43,14 @@ return [ 'disks' => [ - 'local' => [ + 'persistent' => [ 'driver' => 'local', - 'root' => storage_path('app'), + 'root' => storage_path('app/persistent'), ], - 'public' => [ + 'ephemeral' => [ 'driver' => 'local', - 'root' => storage_path('app/public'), - 'visibility' => 'public', - ], - - 's3' => [ - 'driver' => 's3', - 'key' => 'your-key', - 'secret' => 'your-secret', - 'region' => 'your-region', - 'bucket' => 'your-bucket', + 'root' => storage_path('app/ephemeral'), ], ],