Completed
Pull Request — master (#426)
by
unknown
02:16
created

Files::exif()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
/**
3
 * Nextcloud - 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 2017
12
 * @copyright Robin Appelman 2017
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
35
	use PathManipulation;
36
37
	/** @var SearchFolderService */
38
	private $searchFolderService;
39
	/** @var ConfigService */
40
	private $configService;
41
	/** @var SearchMediaService */
42
	private $searchMediaService;
43
	/** @var DownloadService */
44
	private $downloadService;
45
	/** @var ILogger */
46
	private $logger;
47
48
        /**
49
         * @NoAdminRequired
50
         *
51
         * Returns a exif of picture
52
         *
53
         * @param string $location a path picture
0 ignored issues
show
Bug introduced by
There is no parameter named $location. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
         * @return array
55
         */
56
        public function exif($fileId) {
57
	    $file = $this->downloadService->getFile($fileId);
58
	    $path=$file->getInternalPath();
59
	    $storage=$file->getStorage();
60
	    $filename=$storage->getLocalFile($path);
61
	    $exif = false;
62
	    $iptc = false;
63
	    if (is_callable('exif_read_data')) {
64
		$exif=@exif_read_data($filename);
65
	    }
66
	    getimagesize($filename,$blocks);
67
	    if (is_array($blocks)) {
68
		foreach($blocks as $key => $block) {
69
		    $iptc[$key]=iptcparse($block);
70
		}
71
	    }
72
	    return ['exif'=>$exif,'iptc'=>$iptc];
73
	}
74
75
	/**
76
	 * @NoAdminRequired
77
	 *
78
	 * Returns a list of all media files and albums available to the authenticated user
79
	 *
80
	 *    * Authentication can be via a login/password or a token/(password)
81
	 *    * For private galleries, it returns all media files, with the full path from the root
82
	 *     folder For public galleries, the path starts from the folder the link gives access to
83
	 *     (virtual root)
84
	 *    * An exception is only caught in case something really wrong happens. As we don't test
85
	 *     files before including them in the list, we may return some bad apples
86
	 *
87
	 * @param string $location a path representing the current album in the app
88
	 * @param array $features the list of supported features
89
	 * @param string $etag the last known etag in the client
90
	 * @param array $mediatypes the list of supported media types
91
	 *
92
	 * @return array <string,array<string,string|int>>|Http\JSONResponse
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array|string|boolean>.

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.

Loading history...
93
	 */
94
	private function getFilesAndAlbums($location, $features, $etag, $mediatypes) {
95
		$files = [];
96
		$albums = [];
97
		$updated = true;
98
		/** @var Folder $folderNode */
99
		list($folderPathFromRoot, $folderNode) =
100
			$this->searchFolderService->getCurrentFolder(rawurldecode($location), $features);
101
		$albumConfig = $this->configService->getConfig($folderNode, $features);
102
		if ($folderNode->getEtag() !== $etag) {
103
			list($files, $albums) = $this->searchMediaService->getMediaFiles(
104
				$folderNode, $mediatypes, $features
105
			);
106
		} else {
107
			$updated = false;
108
		}
109
		$files = $this->fixPaths($files, $folderPathFromRoot);
110
111
		return $this->formatResults($files, $albums, $albumConfig, $folderPathFromRoot, $updated);
112
	}
113
114
	/**
115
	 * Generates shortened paths to the media files
116
	 *
117
	 * We only want to keep one folder between the current folder and the found media file
118
	 * /root/folder/sub1/sub2/file.ext
119
	 * becomes
120
	 * /root/folder/file.ext
121
	 *
122
	 * @param array $files
123
	 * @param string $folderPathFromRoot
124
	 *
125
	 * @return array
126
	 */
127
	private function fixPaths($files, $folderPathFromRoot) {
128
		if (!empty($files)) {
129
			foreach ($files as &$file) {
130
				$file['path'] = $this->getReducedPath($file['path'], $folderPathFromRoot);
131
			}
132
		}
133
134
		return $files;
135
	}
136
137
	/**
138
	 * Simply builds and returns an array containing the list of files, the album information and
139
	 * whether the location has changed or not
140
	 *
141
	 * @param array $files
142
	 * @param array $albums
143
	 * @param array $albumConfig
144
	 * @param string $folderPathFromRoot
145
	 * @param bool $updated
146
	 *
147
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array|string|boolean>.

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.

Loading history...
148
	 * @internal param $array <string,string|int> $files
149
	 */
150
	private function formatResults($files, $albums, $albumConfig, $folderPathFromRoot, $updated) {
151
		return [
152
			'files'       => $files,
153
			'albums'      => $albums,
154
			'albumconfig' => $albumConfig,
155
			'albumpath'   => $folderPathFromRoot,
156
			'updated'     => $updated
157
		];
158
	}
159
160
	/**
161
	 * Generates the download data
162
	 *
163
	 * @param int $fileId the ID of the file of which we need a large preview of
164
	 * @param string|null $filename
165
	 *
166
	 * @return array|false
167
	 */
168
	private function getDownload($fileId, $filename) {
169
		/** @type File $file */
170
		$file = $this->downloadService->getFile($fileId);
171
		$this->configService->validateMimeType($file->getMimeType());
172
		$download = $this->downloadService->downloadFile($file);
173
		if (is_null($filename)) {
174
			$filename = $file->getName();
175
		}
176
		$download['name'] = $filename;
177
178
		return $download;
179
	}
180
181
}
182