229 lines
7.0 KiB
PHP
229 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\GroupConfigKey;
|
|
use App\Enums\ImagePermission;
|
|
use App\Enums\StrategyKey;
|
|
use App\Services\ImageService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use League\Flysystem\Filesystem;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property int $album_id
|
|
* @property int $group_id
|
|
* @property int $strategy_id
|
|
* @property string $key
|
|
* @property string $path
|
|
* @property string $name
|
|
* @property string $pathname
|
|
* @property string $origin_name
|
|
* @property string $alias_name
|
|
* @property string $filename
|
|
* @property float $size
|
|
* @property string $mimetype
|
|
* @property string $extension
|
|
* @property string $md5
|
|
* @property string $sha1
|
|
* @property integer $width
|
|
* @property integer $height
|
|
* @property string $url
|
|
* @property string $thumb_url
|
|
* @property Collection $links
|
|
* @property int $permission
|
|
* @property boolean $is_unhealthy
|
|
* @property string $uploaded_ip
|
|
* @property Carbon $updated_at
|
|
* @property Carbon $created_at
|
|
* @property-read User $user
|
|
* @property-read Album $album
|
|
* @property-read Group $group
|
|
* @property-read Strategy $strategy
|
|
*/
|
|
class Image extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'key',
|
|
'path',
|
|
'name',
|
|
'origin_name',
|
|
'alias_name',
|
|
'size',
|
|
'mimetype',
|
|
'extension',
|
|
'md5',
|
|
'sha1',
|
|
'width',
|
|
'height',
|
|
'permission',
|
|
'is_unhealthy',
|
|
'uploaded_ip',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'user_id',
|
|
'album_id',
|
|
'group_id',
|
|
'strategy_id',
|
|
];
|
|
|
|
protected $casts = [
|
|
'width' => 'integer',
|
|
'height' => 'integer',
|
|
'size' => 'float',
|
|
'is_unhealthy' => 'bool',
|
|
];
|
|
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $image) {
|
|
$image->key = $image->generateKey();
|
|
});
|
|
|
|
static::deleting(function (self $image) {
|
|
// TODO 检测是否启用了队列,放置队列中异步删除
|
|
// 在当前图片所属的策略中是否存在其他相同 md5 和 sha1 的记录,没有则可以删除物理文件
|
|
if (!static::query()
|
|
->where('strategy_id', $image->strategy_id)
|
|
->where('id', '<>', $image->id)
|
|
->where('md5', $image->md5)
|
|
->where('sha1', $image->sha1)
|
|
->exists()
|
|
) {
|
|
// 删除本地缓存文件
|
|
Cache::forget("image_thumb_{$image->key}");
|
|
// 删除物理文件
|
|
$image->filesystem()->delete($image->pathname);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function scopeFilter(Builder $builder, Request $request)
|
|
{
|
|
return $builder->when($request->query('order') ?: 'newest', function (Builder $builder, $order) {
|
|
switch ($order) {
|
|
case 'earliest':
|
|
$builder->orderBy('created_at');
|
|
break;
|
|
case 'utmost':
|
|
$builder->orderByDesc('size');
|
|
break;
|
|
case 'least':
|
|
$builder->orderBy('size');
|
|
break;
|
|
default:
|
|
$builder->latest();
|
|
}
|
|
})->when($request->query('permission') ?: 'all', function (Builder $builder, $permission) {
|
|
switch ($permission) {
|
|
case 'public':
|
|
$builder->where('permission', ImagePermission::Public);
|
|
break;
|
|
case 'private':
|
|
$builder->where('permission', ImagePermission::Private);
|
|
break;
|
|
}
|
|
})->when($request->query('keyword'), function (Builder $builder, $keyword) {
|
|
$builder->whereRaw("concat(origin_name,alias_name) like ?", ["%{$keyword}%"]);
|
|
})->when((int) $request->query('album_id'), function (Builder $builder, $albumId) {
|
|
$builder->where('album_id', $albumId);
|
|
}, function (Builder $builder) {
|
|
$builder->whereNull('album_id');
|
|
});
|
|
}
|
|
|
|
public function filename(): Attribute
|
|
{
|
|
return new Attribute(fn() => $this->alias_name ?: $this->origin_name);
|
|
}
|
|
|
|
public function pathname(): Attribute
|
|
{
|
|
return new Attribute(fn() => "{$this->path}/{$this->name}");
|
|
}
|
|
|
|
public function url(): Attribute
|
|
{
|
|
return new Attribute(function () {
|
|
// 是否启用原图保护功能
|
|
if ($this->group->configs->get(GroupConfigKey::IsEnableOriginalProtection)) {
|
|
return asset("{$this->key}.{$this->extension}");
|
|
} else {
|
|
return rtrim($this->strategy->configs->get('url'), '/').'/'.$this->pathname;
|
|
}
|
|
});
|
|
}
|
|
|
|
public function thumbUrl(): Attribute
|
|
{
|
|
return new Attribute(function () {
|
|
return asset("{$this->key}.{$this->extension}!thumbnail");
|
|
});
|
|
}
|
|
|
|
public function links(): Attribute
|
|
{
|
|
return new Attribute(fn() => collect([
|
|
'url' => $this->url,
|
|
'html' => "<img src=\"{$this->url}\" alt=\"{$this->origin_name}\" title=\"{$this->origin_name}\" />",
|
|
'bbcode' => "[img]{$this->url}[/img]",
|
|
'markdown' => "",
|
|
'markdown_with_link' => "[]({$this->url})",
|
|
'thumbnail_url' => $this->thumb_url,
|
|
]));
|
|
}
|
|
|
|
public function filesystem(): Filesystem
|
|
{
|
|
return new Filesystem((new ImageService())->getAdapter($this->strategy->key, $this->strategy->configs));
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id', 'id');
|
|
}
|
|
|
|
public function album(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Album::class, 'album_id', 'id');
|
|
}
|
|
|
|
public function group(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Group::class, 'group_id', 'id')->withDefault(function (Group $group) {
|
|
$group->name = '系统默认组';
|
|
$group->configs = $group::getGuestConfigs();
|
|
});
|
|
}
|
|
|
|
public function strategy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Strategy::class, 'strategy_id', 'id')->withDefault(function (Strategy $strategy) {
|
|
$strategy->key = StrategyKey::Local;
|
|
$strategy->configs = collect(config('filesystems.disks.uploads'));
|
|
});
|
|
}
|
|
|
|
private function generateKey($length = 6): string
|
|
{
|
|
$key = Str::random($length);
|
|
if (self::query()->where('key', $key)->exists()) {
|
|
return $this->generateKey(++$length);
|
|
}
|
|
return $key;
|
|
}
|
|
}
|