Welcome to mirror list, hosted at ThFree Co, Russian Federation.

searchmediaservice.php « service - github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 974aa53f3b3ea47390e943b45364edc0630958b3 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
 * ownCloud - gallery
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Olivier Paroz <owncloud@interfasys.ch>
 *
 * @copyright Olivier Paroz 2014-2015
 */

namespace OCA\Gallery\Service;

use OCP\Files\Folder;
use OCP\Files\File;

/**
 * Searches the instance for media files which can be shown
 *
 * @package OCA\Gallery\Service
 */
class SearchMediaService extends FilesService {

	/**
	 * @var null|array<string,string|int>
	 */
	private $images = [];
	/**
	 * @var string[]
	 */
	private $supportedMediaTypes;

	/**
	 * This returns the list of all media files which can be shown starting from the given folder
	 *
	 * @param Folder $folder
	 * @param string[] $supportedMediaTypes
	 * @param array $features
	 *
	 * @return array<string,string|int>|null all the images we could find
	 */
	public function getMediaFiles($folder, $supportedMediaTypes, $features) {
		$this->supportedMediaTypes = $supportedMediaTypes;
		$this->features = $features;
		$this->searchFolder($folder);

		return $this->images;
	}

	/**
	 * Look for media files and folders in the given folder
	 *
	 * @param Folder $folder
	 * @param int $subDepth
	 *
	 * @return int
	 */
	private function searchFolder($folder, $subDepth = 0) {
		$albumImageCounter = 0;
		$subFolders = [];
		$nodes = $this->getNodes($folder, $subDepth);
		foreach ($nodes as $node) {
			if (!$this->isAllowedAndAvailable($node)) {
				continue;
			}
			//$this->logger->debug("Sub-Node path : {path}", ['path' => $node->getPath()]);
			$nodeType = $this->getNodeType($node);
			$subFolders = array_merge($subFolders, $this->getAllowedSubFolder($node, $nodeType));
			$albumImageCounter = $this->addMediaFile($node, $nodeType, $albumImageCounter);
			if ($this->haveEnoughPictures($albumImageCounter, $subDepth)) {
				break;
			}
		}
		$albumImageCounter = $this->searchSubFolders($subFolders, $subDepth, $albumImageCounter);

		return $albumImageCounter;
	}

	/**
	 * Adds the node to the list of images if it's a file and we can generate a preview of it
	 *
	 * @param File|Folder $node
	 * @param string $nodeType
	 * @param int $albumImageCounter
	 *
	 * @return int
	 */
	private function addMediaFile($node, $nodeType, $albumImageCounter) {
		if ($nodeType === 'file') {
			$albumImageCounter = $albumImageCounter + (int)$this->isPreviewAvailable($node);
		}

		return $albumImageCounter;
	}

	/**
	 * Checks if we've collected enough pictures to be able to build the view
	 *
	 * An album is full when we find max 4 pictures at the same level
	 *
	 * @param int $albumImageCounter
	 * @param int $subDepth
	 *
	 * @return bool
	 */
	private function haveEnoughPictures($albumImageCounter, $subDepth) {
		if ($subDepth === 0) {
			return false;
		}

		return $albumImageCounter === 4;
	}

	/**
	 * Looks for pictures in sub-folders
	 *
	 * If we're at level 0, we need to look for pictures in sub-folders no matter what
	 * If we're at deeper levels, we only need to go further if we haven't managed to find one
	 * picture in the current folder
	 *
	 * @param array <Folder> $subFolders
	 * @param int $subDepth
	 * @param int $albumImageCounter
	 *
	 * @return int
	 */
	private function searchSubFolders($subFolders, $subDepth, $albumImageCounter) {
		if ($this->folderNeedsToBeSearched($subFolders, $subDepth, $albumImageCounter)) {
			$subDepth++;
			foreach ($subFolders as $subFolder) {
				//$this->logger->debug("Sub-Node path : {path}", ['path' => $subFolder->getPath()]);
				$albumImageCounter = $this->searchFolder($subFolder, $subDepth);
				if ($this->abortSearch($subDepth, $albumImageCounter)) {
					break;
				}
			}
		}

		return $albumImageCounter;
	}

	/**
	 * Checks if we need to look for media files in the specified folder
	 *
	 * @param array <Folder> $subFolders
	 * @param int $subDepth
	 * @param int $albumImageCounter
	 *
	 * @return bool
	 */
	private function folderNeedsToBeSearched($subFolders, $subDepth, $albumImageCounter) {
		return !empty($subFolders) && ($subDepth === 0 || $albumImageCounter === 0);
	}

	/**
	 * Returns true if there is no need to check any other sub-folder at the same depth level
	 *
	 * @param int $subDepth
	 * @param int $count
	 *
	 * @return bool
	 */
	private function abortSearch($subDepth, $count) {
		return $subDepth > 1 && $count > 0;
	}

	/**
	 * Returns true if the file is of a supported media type and adds it to the array of items to
	 * return
	 *
	 * @todo We could potentially check if the file is readable ($file->stat() maybe) in order to
	 *     only return valid files, but this may slow down operations
	 *
	 * @param File $file the file to test
	 *
	 * @return bool
	 */
	private function isPreviewAvailable($file) {
		try {
			$mimeType = $file->getMimetype();
			if (in_array($mimeType, $this->supportedMediaTypes)) {
				$this->addFileToResults($file);

				return true;
			}
		} catch (\Exception $exception) {
			return false;
		}

		return false;
	}

	/**
	 * Adds various information about a file to the list of results
	 *
	 * @param File $file
	 */
	private function addFileToResults($file) {
		$imagePath = $this->environment->getPathFromVirtualRoot($file);
		$imageId = $file->getId();
		$mimeType = $file->getMimetype();
		$mTime = $file->getMTime();
		$etag = $file->getEtag();

		$imageData = [
			'path'     => $imagePath,
			'fileid'   => $imageId,
			'mimetype' => $mimeType,
			'mtime'    => $mTime,
			'etag'     => $etag
		];

		$this->images[] = $imageData;

		//$this->logger->debug("Image path : {path}", ['path' => $imagePath]);
	}

}