1 | <?php |
||
23 | class SearchMediaService extends FilesService { |
||
24 | |||
25 | /** @var null|array<string,string|int> */ |
||
26 | private $images = []; |
||
27 | /** @var null|array<string,string|int> */ |
||
28 | private $albums = []; |
||
29 | /** @var string[] */ |
||
30 | private $supportedMediaTypes; |
||
31 | |||
32 | /** |
||
33 | * This returns the list of all media files which can be shown starting from the given folder |
||
34 | * |
||
35 | * @param Folder $folderNode the current album |
||
36 | * @param string[] $supportedMediaTypes the list of supported media types |
||
37 | * @param array $features the list of supported features |
||
38 | * |
||
39 | * @return array<null|array<string,string|int>> all the images we could find |
||
40 | */ |
||
41 | public function getMediaFiles($folderNode, $supportedMediaTypes, $features) { |
||
42 | $this->supportedMediaTypes = $supportedMediaTypes; |
||
43 | 12 | $this->features = $features; |
|
44 | 12 | $this->searchFolder($folderNode); |
|
45 | 12 | ||
46 | 12 | return [$this->images, $this->albums]; |
|
47 | } |
||
48 | 12 | ||
49 | /** |
||
50 | * Look for media files and folders in the given folder |
||
51 | * |
||
52 | * @param Folder $folder |
||
53 | * @param int $subDepth |
||
54 | * |
||
55 | * @return int |
||
56 | */ |
||
57 | private function searchFolder($folder, $subDepth = 0) { |
||
58 | $albumImageCounter = 0; |
||
59 | 12 | $subFolders = []; |
|
60 | 12 | $this->addFolderToAlbumsArray($folder); |
|
61 | 12 | $nodes = $this->getNodes($folder, $subDepth); |
|
62 | 12 | foreach ($nodes as $node) { |
|
63 | 12 | if (!$this->isAllowedAndAvailable($node)) { |
|
64 | 11 | continue; |
|
65 | 1 | } |
|
66 | $nodeType = $this->getNodeType($node); |
||
67 | $subFolders = array_merge($subFolders, $this->getAllowedSubFolder($node, $nodeType)); |
||
68 | 11 | $albumImageCounter = $this->addMediaFile($node, $nodeType, $albumImageCounter); |
|
69 | 11 | if ($this->haveEnoughPictures($albumImageCounter, $subDepth)) { |
|
70 | 11 | break; |
|
71 | 11 | } |
|
72 | 1 | } |
|
73 | $albumImageCounter = $this->searchSubFolders($subFolders, $subDepth, $albumImageCounter); |
||
74 | 12 | ||
75 | 12 | return $albumImageCounter; |
|
76 | } |
||
77 | 12 | ||
78 | /** |
||
79 | * Adds the node to the list of images if it's a file and we can generate a preview of it |
||
80 | * |
||
81 | * @param File|Folder $node |
||
82 | * @param string $nodeType |
||
83 | * @param int $albumImageCounter |
||
84 | * |
||
85 | * @return int |
||
86 | */ |
||
87 | private function addMediaFile($node, $nodeType, $albumImageCounter) { |
||
94 | 11 | ||
95 | /** |
||
96 | * Checks if we've collected enough pictures to be able to build the view |
||
97 | * |
||
98 | * An album is full when we find max 4 pictures at the same level |
||
99 | * |
||
100 | * @param int $albumImageCounter |
||
101 | * @param int $subDepth |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | private function haveEnoughPictures($albumImageCounter, $subDepth) { |
||
112 | 10 | ||
113 | /** |
||
114 | * Looks for pictures in sub-folders |
||
115 | * |
||
116 | * If we're at level 0, we need to look for pictures in sub-folders no matter what |
||
117 | * If we're at deeper levels, we only need to go further if we haven't managed to find one |
||
118 | * picture in the current folder |
||
119 | * |
||
120 | * @param array <Folder> $subFolders |
||
121 | * @param int $subDepth |
||
122 | * @param int $albumImageCounter |
||
123 | * |
||
124 | * @return int |
||
125 | */ |
||
126 | private function searchSubFolders($subFolders, $subDepth, $albumImageCounter) { |
||
140 | 12 | ||
141 | /** |
||
142 | * Checks if we need to look for media files in the specified folder |
||
143 | * |
||
144 | * @param array <Folder> $subFolders |
||
145 | * @param int $subDepth |
||
146 | * @param int $albumImageCounter |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | private function folderNeedsToBeSearched($subFolders, $subDepth, $albumImageCounter) { |
||
153 | 12 | ||
154 | /** |
||
155 | * Returns true if there is no need to check any other sub-folder at the same depth level |
||
156 | * |
||
157 | * @param int $subDepth |
||
158 | * @param int $count |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | private function abortSearch($subDepth, $count) { |
||
165 | 10 | ||
166 | /** |
||
167 | * Returns true if the file is of a supported media type and adds it to the array of items to |
||
168 | * return |
||
169 | * |
||
170 | * @todo We could potentially check if the file is readable ($file->stat() maybe) in order to |
||
171 | * only return valid files, but this may slow down operations |
||
172 | * |
||
173 | * @param File $file the file to test |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | private function isPreviewAvailable($file) { |
||
191 | 5 | ||
192 | /** |
||
193 | * Adds a folder to the albums array |
||
194 | * |
||
195 | * @param Folder $folder the folder to add to the albums array |
||
196 | */ |
||
197 | private function addFolderToAlbumsArray($folder) { |
||
201 | 10 | ||
202 | 10 | /** |
|
203 | 10 | * Adds a file to the images array |
|
204 | 10 | * |
|
205 | * @param string $mimeType the media type of the file to add to the images array |
||
206 | * @param File $file the file to add to the images array |
||
207 | 10 | */ |
|
208 | 10 | private function addFileToImagesArray($mimeType, $file) { |
|
213 | |||
214 | } |
||
215 |