1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gallery |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Olivier Paroz <[email protected]> |
9
|
|
|
* @author Robin Appelman <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @copyright Olivier Paroz 2014-2016 |
12
|
|
|
* @copyright Robin Appelman 2012-2014 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace OCA\Gallery\Controller; |
16
|
|
|
|
17
|
|
|
use OCP\Files\File; |
18
|
|
|
use OCP\Files\Folder; |
19
|
|
|
use OCP\ILogger; |
20
|
|
|
|
21
|
|
|
use OCP\AppFramework\Http; |
22
|
|
|
|
23
|
|
|
use OCA\Gallery\Service\SearchFolderService; |
24
|
|
|
use OCA\Gallery\Service\ConfigService; |
25
|
|
|
use OCA\Gallery\Service\SearchMediaService; |
26
|
|
|
use OCA\Gallery\Service\DownloadService; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Trait Files |
30
|
|
|
* |
31
|
|
|
* @package OCA\Gallery\Controller |
32
|
|
|
*/ |
33
|
|
|
trait Files { |
34
|
|
|
use PathManipulation; |
35
|
|
|
|
36
|
|
|
/** @var SearchFolderService */ |
37
|
|
|
private $searchFolderService; |
38
|
|
|
/** @var ConfigService */ |
39
|
|
|
private $configService; |
40
|
|
|
/** @var SearchMediaService */ |
41
|
|
|
private $searchMediaService; |
42
|
|
|
/** @var DownloadService */ |
43
|
|
|
private $downloadService; |
44
|
|
|
/** @var ILogger */ |
45
|
|
|
private $logger; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @NoAdminRequired |
49
|
|
|
* |
50
|
|
|
* Returns a list of all media files and albums available to the authenticated user |
51
|
|
|
* |
52
|
|
|
* * Authentication can be via a login/password or a token/(password) |
53
|
|
|
* * For private galleries, it returns all media files, with the full path from the root |
54
|
|
|
* folder For public galleries, the path starts from the folder the link gives access to |
55
|
|
|
* (virtual root) |
56
|
|
|
* * An exception is only caught in case something really wrong happens. As we don't test |
57
|
|
|
* files before including them in the list, we may return some bad apples |
58
|
|
|
* |
59
|
|
|
* @param string $location a path representing the current album in the app |
60
|
|
|
* @param array $features the list of supported features |
61
|
|
|
* @param string $etag the last known etag in the client |
62
|
|
|
* @param array $mediatypes the list of supported media types |
63
|
|
|
* |
64
|
|
|
* @return array <string,array<string,string|int>>|Http\JSONResponse |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
private function getFilesAndAlbums($location, $features, $etag, $mediatypes) { |
67
|
|
|
$files = []; |
68
|
|
|
$albums = []; |
69
|
|
|
$updated = true; |
70
|
|
|
/** @var Folder $folderNode */ |
71
|
|
|
list($folderPathFromRoot, $folderNode) = |
72
|
|
|
$this->searchFolderService->getCurrentFolder(\rawurldecode($location), $features); |
73
|
|
|
$albumConfig = $this->configService->getConfig($folderNode, $features); |
74
|
|
|
if ($folderNode->getEtag() !== $etag) { |
75
|
|
|
list($files, $albums) = $this->searchMediaService->getMediaFiles( |
76
|
|
|
$folderNode, $mediatypes, $features |
77
|
|
|
); |
78
|
|
|
} else { |
79
|
|
|
$updated = false; |
80
|
|
|
} |
81
|
|
|
$files = $this->fixPaths($files, $folderPathFromRoot); |
82
|
|
|
|
83
|
|
|
return $this->formatResults($files, $albums, $albumConfig, $folderPathFromRoot, $updated); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generates shortened paths to the media files |
88
|
|
|
* |
89
|
|
|
* We only want to keep one folder between the current folder and the found media file |
90
|
|
|
* /root/folder/sub1/sub2/file.ext |
91
|
|
|
* becomes |
92
|
|
|
* /root/folder/file.ext |
93
|
|
|
* |
94
|
|
|
* @param array $files |
95
|
|
|
* @param string $folderPathFromRoot |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
private function fixPaths($files, $folderPathFromRoot) { |
100
|
|
|
if (!empty($files)) { |
101
|
|
|
foreach ($files as &$file) { |
102
|
|
|
$file['path'] = $this->getReducedPath($file['path'], $folderPathFromRoot); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $files; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Simply builds and returns an array containing the list of files, the album information and |
111
|
|
|
* whether the location has changed or not |
112
|
|
|
* |
113
|
|
|
* @param array $files |
114
|
|
|
* @param array $albums |
115
|
|
|
* @param array $albumConfig |
116
|
|
|
* @param string $folderPathFromRoot |
117
|
|
|
* @param bool $updated |
118
|
|
|
* |
119
|
|
|
* @return array |
|
|
|
|
120
|
|
|
* @internal param $array <string,string|int> $files |
121
|
|
|
*/ |
122
|
|
|
private function formatResults($files, $albums, $albumConfig, $folderPathFromRoot, $updated) { |
123
|
|
|
return [ |
124
|
|
|
'files' => $files, |
125
|
|
|
'albums' => $albums, |
126
|
|
|
'albumconfig' => $albumConfig, |
127
|
|
|
'albumpath' => $folderPathFromRoot, |
128
|
|
|
'updated' => $updated |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Generates the download data |
134
|
|
|
* |
135
|
|
|
* @param int $fileId the ID of the file of which we need a large preview of |
136
|
|
|
* @param string|null $filename |
137
|
|
|
* |
138
|
|
|
* @return array|false |
139
|
|
|
*/ |
140
|
|
|
private function getDownload($fileId, $filename) { |
141
|
|
|
/** @type File $file */ |
142
|
|
|
$file = $this->downloadService->getFile($fileId); |
143
|
|
|
$this->configService->validateMimeType($file->getMimeType()); |
144
|
|
|
$download = $this->downloadService->downloadFile($file); |
145
|
|
|
if ($filename === null) { |
146
|
|
|
$filename = $file->getName(); |
147
|
|
|
} |
148
|
|
|
$download['name'] = $filename; |
149
|
|
|
|
150
|
|
|
return $download; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.