-
-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathFrameController.php
More file actions
110 lines (93 loc) · 2.84 KB
/
FrameController.php
File metadata and controls
110 lines (93 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/
namespace App\Http\Controllers\Gallery;
use App\Contracts\Models\AbstractAlbum;
use App\Exceptions\PhotoCollectionEmptyException;
use App\Http\Requests\Frame\FrameRequest;
use App\Http\Resources\Frame\FrameData;
use App\Http\Resources\Models\PhotoResource;
use App\Models\Configs;
use App\Models\Photo;
use App\Policies\PhotoQueryPolicy;
use Illuminate\Routing\Controller;
class FrameController extends Controller
{
public function __construct(protected PhotoQueryPolicy $photo_query_policy)
{
}
/**
* Return an image and the timeout if the frame is supported.
*
* @param FrameRequest $request
*
* @return FrameData
*/
public function get(FrameRequest $request): FrameData
{
$timeout = Configs::getValueAsInt('mod_frame_refresh');
$photo = $this->loadPhoto($request->album(), 5);
if ($photo === null) {
return new FrameData($timeout, '', '');
}
$src = $photo->size_variants->getMedium()?->url ?? $photo->size_variants->getOriginal()?->url;
if ($photo->size_variants->getMedium() !== null && $photo->size_variants->getMedium2x() !== null) {
$srcset = $photo->size_variants->getMedium()->url . ' ' . $photo->size_variants->getMedium()->width . 'w,';
$srcset .= $photo->size_variants->getMedium2x()->url . ' ' . $photo->size_variants->getMedium2x()->width . 'w';
} else {
$srcset = '';
}
return new FrameData($timeout, $src, $srcset);
}
/**
* Return the full random image data instead of just the URLs.
*
* @param FrameRequest $request
*
* @return PhotoResource
*/
public function random(FrameRequest $request): PhotoResource
{
$photo = $this->loadPhoto($request->album(), 5);
return PhotoResource::fromModel($photo);
}
/**
* Recursively search for a photo to display.
*
* @param AbstractAlbum|null $album
* @param int $retries
*
* @return Photo|null
*/
private function loadPhoto(AbstractAlbum|null $album, int $retries = 5): ?Photo
{
// avoid infinite recursion
if ($retries === 0) {
return null;
}
$query = null;
// default query
if ($album === null) {
$query = $this->photo_query_policy->applySearchabilityFilter(
query: Photo::query()->with(['album', 'size_variants', 'palette']),
origin: null,
include_nsfw: !Configs::getValueAsBool('hide_nsfw_in_frame')
);
} else {
$query = $album->photos()->with(['album', 'size_variants', 'palette']);
}
/** @var ?Photo $photo */
$photo = $query->inRandomOrder()->first();
if ($photo === null) {
$album === null ? throw new PhotoCollectionEmptyException() : throw new PhotoCollectionEmptyException('Photo collection of ' . $album->get_title() . ' is empty');
}
// retry
if ($photo->isVideo()) {
return $this->loadPhoto($album, $retries - 1);
}
return $photo;
}
}