This commit is contained in:
2023-07-29 23:04:14 +10:00
parent d65bf5bcd5
commit a2384f4b35
12 changed files with 252 additions and 52 deletions

View File

@@ -5,7 +5,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Cache;
class Attachment extends Model
{
@@ -31,16 +31,6 @@ class Attachment extends Model
];
/**
* Get attachments attachable
*
* @return MorphTo
*/
public function attachable(): MorphTo
{
return $this->morphTo();
}
/**
* Get the media for this attachment.
*
@@ -50,4 +40,16 @@ class Attachment extends Model
{
return $this->belongsTo(Media::class);
}
/**
* Get associated Media object.
*
* @return null|Media
*/
public function getMedia(): ?Media
{
return Cache::remember("attachment:{$this->id}:media", now()->addDays(28), function () {
return $this->media()->first();
});
}
}

View File

@@ -3,10 +3,12 @@
namespace App\Models;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Cache;
class Gallery extends Model
{
@@ -23,6 +25,26 @@ class Gallery extends Model
];
/**
* Boot the model.
*
* @return void
*/
protected static function boot(): void
{
parent::boot();
$clearCache = function ($gallery) {
$cacheKeys = [
"gallery:{$gallery->id}:media",
];
Cache::forget($cacheKeys);
};
static::saving($clearCache);
static::deleting($clearCache);
}
/**
* Get gallery addendum model.
*
@@ -42,4 +64,35 @@ class Gallery extends Model
{
return $this->belongsTo(Media::class);
}
/**
* Get the media for this item.
*
* @return null|Media The media model.
*/
public function getMedia(): ?Media
{
$mediaId = '0';
$media = null;
if (Cache::has("gallery:{$this->id}:media") === true) {
$mediaId = Cache::get("gallery:{$this->id}:media");
} else {
$media = $this->media()->first();
if ($media === null) {
return null;
}
$mediaId = $media->id;
Cache::put("gallery:{$this->id}:media", $mediaId, now()->addDays(28));
}
return Cache::remember("media:{$mediaId}", now()->addDays(28), function () use ($media) {
if ($media !== null) {
return $media;
}
return $this->media()->first();
});
}
}

View File

@@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
@@ -101,12 +102,23 @@ class Media extends Model
/**
* Model Boot
*
* @return void
*/
protected static function boot(): void
{
parent::boot();
static::updating(function ($media) {
$clearCache = function ($media) {
$cacheKeys = [
"media:{$media->id}",
];
Cache::forget($cacheKeys);
};
static::updating(function ($media) use ($clearCache) {
$clearCache($media);
if (array_key_exists('permission', $media->getChanges()) === true) {
$origPermission = $media->getOriginal()['permission'];
$newPermission = $media->permission;
@@ -123,12 +135,12 @@ class Media extends Model
}
});
static::deleting(function ($media) {
static::deleting(function ($media) use ($clearCache) {
$clearCache($media);
$media->deleteFile();
});
}
/**
* Get Type Variants.
*
@@ -163,6 +175,7 @@ class Media extends Model
* Variants Set Mutator.
*
* @param mixed $value The value to mutate.
* @return void
*/
public function setVariantsAttribute(mixed $value): void
{
@@ -249,11 +262,10 @@ class Media extends Model
return '';
}
/**
* Delete file and associated files with the modal.
*
* @return void
*/
public function deleteFile(): void
{
@@ -275,7 +287,7 @@ class Media extends Model
/**
* Invalidate Cloudflare Cache.
*
* @throws InvalidArgumentException Exception.
* @return void
*/
private function invalidateCFCache(): void
{
@@ -306,6 +318,8 @@ class Media extends Model
/**
* Get URL path
*
* @return string
*/
public function getUrlPath(): string
{
@@ -315,6 +329,8 @@ class Media extends Model
/**
* Return the file URL
*
* @return string
*/
public function getUrlAttribute(): string
{
@@ -327,6 +343,8 @@ class Media extends Model
/**
* Return the file owner
*
* @return BelongsTo
*/
public function user(): BelongsTo
{
@@ -337,6 +355,7 @@ class Media extends Model
* Move files to new storage device.
*
* @param string $storage The storage ID to move to.
* @return void
*/
public function moveToStorage(string $storage): void
{
@@ -482,6 +501,8 @@ class Media extends Model
/**
* Get the server maximum upload size
*
* @return integer
*/
public static function getMaxUploadSize(): int
{
@@ -606,6 +627,7 @@ class Media extends Model
* Sanitize fileName for upload
*
* @param string $fileName Filename to sanitize.
* @return string
*/
private static function sanitizeFilename(string $fileName): string
{

View File

@@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;
use Laravel\Sanctum\HasApiTokens;
use OwenIt\Auditing\Contracts\Auditable;
@@ -80,13 +81,37 @@ class User extends Authenticatable implements Auditable
/**
* Get the list of files of the user
* Boot the model.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
* @return void
*/
public function permissions(): HasMany
protected static function boot(): void
{
return $this->hasMany(Permission::class);
parent::boot();
$clearCache = function ($user) {
$cacheKeys = [
"user:{$user->id}",
"user:{$user->id}:permissions",
];
Cache::forget($cacheKeys);
};
static::saving($clearCache);
static::deleting($clearCache);
}
/**
* Get the list of permissions of the user
*
* @return Illuminate\Database\Eloquent\Collection
*/
public function permissions(): array
{
$cacheKey = "user:{$this->id}:permissions";
return Cache::remember($cacheKey, now()->addDays(28), function () {
return $this->hasMany(Permission::class)->pluck('permission')->toArray();
});
}
/**
@@ -96,7 +121,7 @@ class User extends Authenticatable implements Auditable
*/
public function getPermissionsAttribute(): array
{
return $this->permissions()->pluck('permission')->toArray();
return $this->permissions();
}
/**
@@ -107,7 +132,7 @@ class User extends Authenticatable implements Auditable
*/
public function hasPermission(string $permission): bool
{
return ($this->permissions()->where('permission', $permission)->first() !== null);
return in_array($permission, $this->permissions());
}
/**
@@ -131,6 +156,9 @@ class User extends Authenticatable implements Auditable
return $existingPermissions->contains('permission', $permission['permission']);
});
$cacheKey = "user:{$this->id}:permissions";
Cache::forget($cacheKey);
return $this->permissions()->createMany($newPermissions->toArray());
}
@@ -139,6 +167,7 @@ class User extends Authenticatable implements Auditable
* Revoke permissions from the user
*
* @param string|array $permissions The permission(s) to revoke.
* @return integer
*/
public function revokePermission($permissions): int
{
@@ -146,6 +175,9 @@ class User extends Authenticatable implements Auditable
$permissions = [$permissions];
}
$cacheKey = "user:{$this->id}:permissions";
Cache::forget($cacheKey);
return $this->permissions()
->whereIn('permission', $permissions)
->delete();
@@ -153,6 +185,8 @@ class User extends Authenticatable implements Auditable
/**
* Get the list of files of the user
*
* @return HasMany
*/
public function media(): HasMany
{
@@ -161,6 +195,8 @@ class User extends Authenticatable implements Auditable
/**
* Get the list of files of the user
*
* @return HasMany
*/
public function articles(): HasMany
{
@@ -169,6 +205,8 @@ class User extends Authenticatable implements Auditable
/**
* Get associated user codes
*
* @return HasMany
*/
public function codes(): HasMany
{
@@ -177,6 +215,8 @@ class User extends Authenticatable implements Auditable
/**
* Get the list of logins of the user
*
* @return HasMany
*/
public function logins(): HasMany
{
@@ -185,6 +225,8 @@ class User extends Authenticatable implements Auditable
/**
* Get the events associated with the user.
*
* @return BelongsToMany
*/
public function events(): BelongsToMany
{