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
|
|
|
* |
10
|
|
|
* @copyright Olivier Paroz 2016 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Gallery\Controller; |
14
|
|
|
|
15
|
|
|
use OCP\IRequest; |
16
|
|
|
use OCP\IURLGenerator; |
17
|
|
|
use OCP\ILogger; |
18
|
|
|
|
19
|
|
|
use OCP\AppFramework\Controller; |
20
|
|
|
use OCP\AppFramework\Http; |
21
|
|
|
use OCP\AppFramework\Http\RedirectResponse; |
22
|
|
|
|
23
|
|
|
use OCA\Gallery\Http\ImageResponse; |
24
|
|
|
use OCA\Gallery\Service\SearchFolderService; |
25
|
|
|
use OCA\Gallery\Service\ConfigService; |
26
|
|
|
use OCA\Gallery\Service\SearchMediaService; |
27
|
|
|
use OCA\Gallery\Service\DownloadService; |
28
|
|
|
use OCA\Gallery\Service\ServiceException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class FilesController |
32
|
|
|
* |
33
|
|
|
* @package OCA\Gallery\Controller |
34
|
|
|
*/ |
35
|
|
|
class FilesController extends Controller { |
36
|
|
|
use Files; |
37
|
|
|
use HttpError; |
38
|
|
|
|
39
|
|
|
/** @var IURLGenerator */ |
40
|
|
|
private $urlGenerator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor |
44
|
|
|
* |
45
|
|
|
* @param string $appName |
46
|
|
|
* @param IRequest $request |
47
|
|
|
* @param IURLGenerator $urlGenerator |
48
|
|
|
* @param SearchFolderService $searchFolderService |
49
|
|
|
* @param ConfigService $configService |
50
|
|
|
* @param SearchMediaService $searchMediaService |
51
|
|
|
* @param DownloadService $downloadService |
52
|
|
|
* @param ILogger $logger |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
55
|
|
|
$appName, |
56
|
|
|
IRequest $request, |
57
|
|
|
IURLGenerator $urlGenerator, |
58
|
|
|
SearchFolderService $searchFolderService, |
59
|
|
|
ConfigService $configService, |
60
|
|
|
SearchMediaService $searchMediaService, |
61
|
|
|
DownloadService $downloadService, |
62
|
|
|
ILogger $logger |
63
|
|
|
) { |
64
|
|
|
parent::__construct($appName, $request); |
65
|
|
|
|
66
|
|
|
$this->urlGenerator = $urlGenerator; |
67
|
|
|
$this->searchFolderService = $searchFolderService; |
68
|
|
|
$this->configService = $configService; |
69
|
|
|
$this->searchMediaService = $searchMediaService; |
70
|
|
|
$this->downloadService = $downloadService; |
71
|
|
|
$this->logger = $logger; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @NoAdminRequired |
76
|
|
|
* |
77
|
|
|
* Returns a list of all media files available to the authenticated user |
78
|
|
|
* |
79
|
|
|
* * Authentication can be via a login/password or a token/(password) |
80
|
|
|
* * For private galleries, it returns all media files, with the full path from the root |
81
|
|
|
* folder For public galleries, the path starts from the folder the link gives access to |
82
|
|
|
* (virtual root) |
83
|
|
|
* * An exception is only caught in case something really wrong happens. As we don't test |
84
|
|
|
* files before including them in the list, we may return some bad apples |
85
|
|
|
* |
86
|
|
|
* @param string $location a path representing the current album in the app |
87
|
|
|
* @param string $features the list of supported features |
88
|
|
|
* @param string $etag the last known etag in the client |
89
|
|
|
* @param string $mediatypes the list of supported media types |
90
|
|
|
* |
91
|
|
|
* @return array <string,array<string,string|int>>|Http\JSONResponse |
|
|
|
|
92
|
|
|
*/ |
93
|
|
|
public function getList($location, $features, $etag, $mediatypes) { |
94
|
|
|
$featuresArray = \explode(';', $features); |
95
|
|
|
$mediaTypesArray = \explode(';', $mediatypes); |
96
|
|
|
try { |
97
|
|
|
return $this->getFilesAndAlbums($location, $featuresArray, $etag, $mediaTypesArray); |
98
|
|
|
} catch (\Exception $exception) { |
99
|
|
|
return $this->jsonError($exception, $this->request, $this->logger); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @NoAdminRequired |
105
|
|
|
* |
106
|
|
|
* Sends the file matching the fileId |
107
|
|
|
* |
108
|
|
|
* @param int $fileId the ID of the file we want to download |
109
|
|
|
* @param string|null $filename |
110
|
|
|
* |
111
|
|
|
* @return ImageResponse |
112
|
|
|
*/ |
113
|
|
|
public function download($fileId, $filename = null) { |
114
|
|
|
try { |
115
|
|
|
$download = $this->getDownload($fileId, $filename); |
116
|
|
|
} catch (ServiceException $exception) { |
117
|
|
|
$code = $this->getHttpStatusCode($exception); |
118
|
|
|
$url = $this->urlGenerator->linkToRoute( |
119
|
|
|
$this->appName . '.page.error_page', ['code' => $code] |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$response = new RedirectResponse($url); |
123
|
|
|
$response->addCookie('galleryErrorMessage', $exception->getMessage()); |
124
|
|
|
|
125
|
|
|
return $response; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// That's the only exception out of all the image media types we serve |
129
|
|
|
if ($download['mimetype'] === 'image/svg+xml') { |
130
|
|
|
$download['mimetype'] = 'text/plain'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return new ImageResponse($download); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.