Skip to content

A self-hosted media server and file management system

fix video resource duplicate database queries

aminnausin742ec1c9bd77
diff --git a/app/Http/Resources/VideoResource.php b/app/Http/Resources/VideoResource.php
index d3273c8b..6673cc55 100755
--- a/app/Http/Resources/VideoResource.php
+++ b/app/Http/Resources/VideoResource.php
@@ -6,46 +6,45 @@ use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class VideoResource extends JsonResource {
+ protected static $metadataCache = [];
+
+ protected static $videoTagCache = [];
+
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array {
- $metadata = $this->metadata;
- return [
- 'id' => (string) $this->id,
- // 'path' => $this->path,
- // 'name' => $this->name,
- // 'date' => $this->date,
+ $metadata = null;
- // 'folder_id' => (string)$this->folder->id,
- // 'metadata' => new MetadataResource($metadata),
+ $metadataId = $this->metadata_id;
- // 'attributes' => [
+ if (isset(self::$metadataCache[$metadataId])) {
+ $metadata = self::$metadataCache[$metadataId];
+ } else {
+ $metadata = $this->metadata;
+ self::$metadataCache[$metadataId] = $metadata;
+ }
+
+ return [
+ 'id' => (string) $this->id,
'name' => $this->name,
'path' => $this->path,
'date' => $this->date,
- // 'date_raw' => $this->date_raw,
- 'title' => ($metadata ? $metadata->title : $this->title) ?? $this->name,
- 'description' => $metadata ? $metadata->description : $this->description, // ?? $this->folder->series->description
- 'duration' => $metadata ? $metadata->duration : $this->duration,
- 'episode' => $metadata ? $metadata->episode : $this->episode,
- 'season' => $metadata ? $metadata->season : $this->season,
- 'view_count' => $metadata ? $metadata->view_count : $this->view_count,
- 'file_size' => $metadata ? $metadata->file_size : null,
- // 'tags' => $metadata->tags ?? '',
- 'video_tags' => $metadata ? VideoTagResource::collection($metadata->videotags) : [],
- 'date_released' => $metadata ? $metadata->date_released : null,
- 'date_updated' => $metadata ? $metadata->updated_at : null,
- 'date_uploaded' => $metadata ? $metadata->date_uploaded : null,
- // ],
- // 'relationships' => [
- // 'folder_id' => (string)$this->folder->id,
+ 'title' => $metadata?->title ?: $this->title ?: $this->name,
+ 'description' => $metadata?->description ?: $this->description, // ?: $this->folder->series->description
+ 'duration' => $metadata?->duration ?: $this->duration,
+ 'episode' => $metadata?->episode ?: $this->episode,
+ 'season' => $metadata?->season ?: $this->season,
+ 'view_count' => $metadata?->view_count ?: $this->view_count,
+ 'file_size' => $metadata?->file_size ?: null,
+ 'video_tags' => VideoTagResource::collection($metadata?->videotags ?: []),
+ 'date_released' => $metadata?->date_released ?: null,
+ 'date_updated' => $metadata?->updated_at ?: null,
+ 'date_uploaded' => $metadata?->date_uploaded ?: null,
'metadata' => $metadata,
- // 'editor' => $metadata ? $metadata->editor : null
- // ]
];
}
}
diff --git a/app/Http/Resources/VideoTagResource.php b/app/Http/Resources/VideoTagResource.php
index 79ccf150..a4b68e16 100644
--- a/app/Http/Resources/VideoTagResource.php
+++ b/app/Http/Resources/VideoTagResource.php
@@ -6,15 +6,26 @@ use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class VideoTagResource extends JsonResource {
+ protected static $tagCache = [];
+
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array {
+ $tag_id = $this->tag_id;
+
+ if (isset(self::$tagCache[$tag_id])) {
+ $tag = self::$tagCache[$tag_id];
+ } else {
+ $tag = $this->tag;
+ self::$tagCache[$tag_id] = $tag;
+ }
+
return [
'video_tag_id' => $this->id,
- 'name' => $this->tag->name,
+ 'name' => $tag->name,
'id' => $this->tag_id,
];
}