'integer', 'user_id' => 'integer', 'album_id' => 'integer', 'group_id' => 'integer', 'strategy_id' => 'integer', 'width' => 'integer', 'height' => 'integer', 'size' => 'float', 'is_unhealthy' => 'bool', 'permission' => 'integer', ]; protected ?Filesystem $filesystem = null; 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}"); try { // 删除物理文件 $image->filesystem()->delete($image->pathname); } catch (\Throwable $e) { Utils::e($e, '删除物理文件时发生异常'); } } }); } 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->where('origin_name', 'like', "%{$keyword}%")->orWhere('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 () { $path = trim(env('THUMBNAIL_PATH', 'thumbnails'), '/'); $pathname = $path."/{$this->md5}.png"; // 没有缩略图则返回原图 if (! file_exists(public_path($pathname))) { return $this->url; } return asset($pathname); }); } 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' => "![{$this->origin_name}]({$this->url})", 'markdown_with_link' => "[![{$this->origin_name}]({$this->url})]({$this->url})", 'thumbnail_url' => $this->thumb_url, ])); } public function filesystem(): Filesystem { if (is_null($this->filesystem)) { $this->filesystem = new Filesystem((new ImageService())->getAdapter($this->strategy)); } return $this->filesystem; } 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'); } public function strategy(): BelongsTo { return $this->belongsTo(Strategy::class, 'strategy_id', 'id'); } private function generateKey($length = 6): string { $key = Str::random($length); if (self::query()->where('key', $key)->exists()) { return $this->generateKey(++$length); } return $key; } }