Implement paste storage

This commit is contained in:
Les De Ridder 2016-05-29 02:14:29 +02:00
parent f389161b79
commit f8fbce2285
2 changed files with 78 additions and 21 deletions

View File

@ -4,6 +4,9 @@ namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Storage;
class Paste extends Model class Paste extends Model
{ {
public $incrementing = false; public $incrementing = false;
@ -14,14 +17,9 @@ class Paste extends Model
return $this->hasOne(Deletion::class); return $this->hasOne(Deletion::class);
} }
public function isDeleted()
{
return $this->deletion !== null;
}
public function soft_delete($reason, $deleted_by) public function soft_delete($reason, $deleted_by)
{ {
if($this->isDeleted()) if($this->deleted)
{ {
return false; return false;
} }
@ -35,4 +33,72 @@ class Paste extends Model
return true; 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);
}
}
} }

View File

@ -15,7 +15,7 @@ return [
| |
*/ */
'default' => 'local', 'default' => 'null',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -28,7 +28,7 @@ return [
| |
*/ */
'cloud' => 's3', 'cloud' => 'null',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -43,23 +43,14 @@ return [
'disks' => [ 'disks' => [
'local' => [ 'persistent' => [
'driver' => 'local', 'driver' => 'local',
'root' => storage_path('app'), 'root' => storage_path('app/persistent'),
], ],
'public' => [ 'ephemeral' => [
'driver' => 'local', 'driver' => 'local',
'root' => storage_path('app/public'), 'root' => storage_path('app/ephemeral'),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
], ],
], ],