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 | 13 | public function getMediaFiles($folderNode, $supportedMediaTypes, $features) { |
|
48 | |||
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 | 13 | private function searchFolder($folder, $subDepth = 0) { |
|
77 | |||
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 | 13 | private function addMediaFile($node, $nodeType, $albumImageCounter) { |
|
88 | 13 | if ($nodeType === 'file') { |
|
89 | 13 | $albumImageCounter = $albumImageCounter + (int)$this->isPreviewAvailable($node); |
|
90 | } |
||
91 | |||
92 | 13 | return $albumImageCounter; |
|
93 | } |
||
94 | |||
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 | 13 | private function haveEnoughPictures($albumImageCounter, $subDepth) { |
|
106 | 13 | if ($subDepth === 0) { |
|
107 | 13 | return false; |
|
108 | } |
||
109 | |||
110 | 11 | return $albumImageCounter === 4; |
|
111 | } |
||
112 | |||
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 | 13 | private function searchSubFolders($subFolders, $subDepth, $albumImageCounter) { |
|
127 | 13 | if ($this->folderNeedsToBeSearched($subFolders, $subDepth, $albumImageCounter)) { |
|
128 | 11 | $subDepth++; |
|
129 | 11 | foreach ($subFolders as $subFolder) { |
|
130 | //$this->logger->debug("Sub-Node path : {path}", ['path' => $subFolder->getPath()]); |
||
131 | 11 | $albumImageCounter = $this->searchFolder($subFolder, $subDepth); |
|
132 | 11 | if ($this->abortSearch($subDepth, $albumImageCounter)) { |
|
133 | 11 | break; |
|
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | 13 | return $albumImageCounter; |
|
139 | } |
||
140 | |||
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 | 13 | private function folderNeedsToBeSearched($subFolders, $subDepth, $albumImageCounter) { |
|
151 | 13 | return !empty($subFolders) && ($subDepth === 0 || $albumImageCounter === 0); |
|
152 | } |
||
153 | |||
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 | 11 | private function abortSearch($subDepth, $count) { |
|
163 | 11 | return $subDepth > 1 && $count > 0; |
|
164 | } |
||
165 | |||
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 | 14 | private function isPreviewAvailable($file) { |
|
191 | |||
192 | /** |
||
193 | * Adds a folder to the albums array |
||
194 | * |
||
195 | * @param Folder $folder the folder to add to the albums array |
||
196 | */ |
||
197 | 13 | private function addFolderToAlbumsArray($folder) { |
|
201 | |||
202 | /** |
||
203 | * Adds a file to the images array |
||
204 | * |
||
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 | */ |
||
208 | 12 | private function addFileToImagesArray($mimeType, $file) { |
|
213 | |||
214 | } |
||
215 |