11<?php
2+ declare (strict_types=1 );
23/**
34 * @copyright Copyright (c) 2016, ownCloud, Inc.
45 *
@@ -151,16 +152,16 @@ public function __construct($appName,
151152 * @throws \OutOfBoundsException when no user is given
152153 */
153154 protected function validateParameters ($ filter , $ since , $ limit , $ previews , $ objectType , $ objectId , $ sort ) {
154- $ this ->filter = is_string ($ filter ) ? $ filter : 'all ' ;
155+ $ this ->filter = \ is_string ($ filter ) ? $ filter : 'all ' ;
155156 if ($ this ->filter !== $ this ->data ->validateFilter ($ this ->filter )) {
156- throw new InvalidFilterException ();
157+ throw new InvalidFilterException (' Invalid filter ' );
157158 }
158159 $ this ->since = (int ) $ since ;
159160 $ this ->limit = (int ) $ limit ;
160161 $ this ->loadPreviews = (bool ) $ previews ;
161162 $ this ->objectType = (string ) $ objectType ;
162163 $ this ->objectId = (int ) $ objectId ;
163- $ this ->sort = in_array ($ sort , ['asc ' , 'desc ' ], true ) ? $ sort : 'desc ' ;
164+ $ this ->sort = \ in_array ($ sort , ['asc ' , 'desc ' ], true ) ? $ sort : 'desc ' ;
164165
165166 if (($ this ->objectType !== '' && $ this ->objectId === 0 ) || ($ this ->objectType === '' && $ this ->objectId !== 0 )) {
166167 // Only allowed together
@@ -173,7 +174,7 @@ protected function validateParameters($filter, $since, $limit, $previews, $objec
173174 $ this ->user = $ user ->getUID ();
174175 } else {
175176 // No user logged in
176- throw new \OutOfBoundsException ();
177+ throw new \OutOfBoundsException (' Not logged in ' );
177178 }
178179 }
179180
@@ -188,7 +189,7 @@ protected function validateParameters($filter, $since, $limit, $previews, $objec
188189 * @param string $sort
189190 * @return DataResponse
190191 */
191- public function getDefault ($ since = 0 , $ limit = 50 , $ previews = false , $ object_type = '' , $ object_id = 0 , $ sort = 'desc ' ) {
192+ public function getDefault ($ since = 0 , $ limit = 50 , $ previews = false , $ object_type = '' , $ object_id = 0 , $ sort = 'desc ' ): DataResponse {
192193 return $ this ->get ('all ' , $ since , $ limit , $ previews , $ object_type , $ object_id , $ sort );
193194 }
194195
@@ -204,7 +205,7 @@ public function getDefault($since = 0, $limit = 50, $previews = false, $object_t
204205 * @param string $sort
205206 * @return DataResponse
206207 */
207- public function getFilter ($ filter , $ since = 0 , $ limit = 50 , $ previews = false , $ object_type = '' , $ object_id = 0 , $ sort = 'desc ' ) {
208+ public function getFilter ($ filter , $ since = 0 , $ limit = 50 , $ previews = false , $ object_type = '' , $ object_id = 0 , $ sort = 'desc ' ): DataResponse {
208209 return $ this ->get ($ filter , $ since , $ limit , $ previews , $ object_type , $ object_id , $ sort );
209210 }
210211
@@ -213,7 +214,7 @@ public function getFilter($filter, $since = 0, $limit = 50, $previews = false, $
213214 *
214215 * @return DataResponse
215216 */
216- public function listFilters () {
217+ public function listFilters (): DataResponse {
217218 $ filters = $ this ->activityManager ->getFilters ();
218219
219220 $ filters = array_map (function (IFilter $ filter ) {
@@ -247,7 +248,7 @@ public function listFilters() {
247248 * @param string $sort
248249 * @return DataResponse
249250 */
250- protected function get ($ filter , $ since , $ limit , $ previews , $ filterObjectType , $ filterObjectId , $ sort ) {
251+ protected function get ($ filter , $ since , $ limit , $ previews , $ filterObjectType , $ filterObjectId , $ sort ): DataResponse {
251252 try {
252253 $ this ->validateParameters ($ filter , $ since , $ limit , $ previews , $ filterObjectType , $ filterObjectId , $ sort );
253254 } catch (InvalidFilterException $ e ) {
@@ -293,7 +294,7 @@ protected function get($filter, $since, $limit, $previews, $filterObjectType, $f
293294 if ($ this ->loadPreviews ) {
294295 $ activity ['previews ' ] = [];
295296 if ($ activity ['object_type ' ] === 'files ' ) {
296- if (!empty ($ activity ['objects ' ]) && is_array ($ activity ['objects ' ])) {
297+ if (!empty ($ activity ['objects ' ]) && \ is_array ($ activity ['objects ' ])) {
297298 foreach ($ activity ['objects ' ] as $ objectId => $ objectName ) {
298299 if (((int ) $ objectId ) === 0 || $ objectName === '' ) {
299300 // No file, no preview
@@ -315,13 +316,7 @@ protected function get($filter, $since, $limit, $previews, $filterObjectType, $f
315316 return new DataResponse ($ preparedActivities , Http::STATUS_OK , $ headers );
316317 }
317318
318- /**
319- * @param array $headers
320- * @param bool $hasMoreActivities
321- * @param array $data
322- * @return array
323- */
324- protected function generateHeaders (array $ headers , $ hasMoreActivities , array $ data ) {
319+ protected function generateHeaders (array $ headers , bool $ hasMoreActivities , array $ data ): array {
325320 if ($ hasMoreActivities && isset ($ headers ['X-Activity-Last-Given ' ])) {
326321 // Set the "Link" header for the next page
327322 $ nextPageParameters = [
@@ -354,71 +349,62 @@ protected function generateHeaders(array $headers, $hasMoreActivities, array $da
354349 return $ headers ;
355350 }
356351
357- /**
358- * @param string $owner
359- * @param int $fileId
360- * @param string $filePath
361- * @return array
362- */
363- protected function getPreview ($ owner , $ fileId , $ filePath ) {
352+ protected function getPreview (string $ owner , int $ fileId , string $ filePath ): array {
364353 $ info = $ this ->infoCache ->getInfoById ($ owner , $ fileId , $ filePath );
365354
366355 if (!$ info ['exists ' ] || $ info ['view ' ] !== '' ) {
367- return $ this ->getPreviewFromPath ($ filePath , $ info );
356+ return $ this ->getPreviewFromPath ($ fileId , $ filePath , $ info );
368357 }
369358
370359 $ preview = [
371360 'link ' => $ this ->getPreviewLink ($ info ['path ' ], $ info ['is_dir ' ], $ info ['view ' ]),
372361 'source ' => '' ,
362+ 'mimeType ' => 'application/octet-stream ' ,
373363 'isMimeTypeIcon ' => true ,
364+ 'fileId ' => $ fileId ,
365+ 'view ' => $ info ['view ' ] ?: 'files ' ,
374366 ];
375367
376368 // show a preview image if the file still exists
377369 if ($ info ['is_dir ' ]) {
378370 $ preview ['source ' ] = $ this ->getPreviewPathFromMimeType ('dir ' );
371+ $ preview ['mimeType ' ] = 'dir ' ;
379372 } else {
380373 $ this ->view ->chroot ('/ ' . $ owner . '/files ' );
381374 $ fileInfo = $ this ->view ->getFileInfo ($ info ['path ' ]);
382- if (!($ fileInfo instanceof FileInfo)) {
383- $ pathPreview = $ this ->getPreviewFromPath ($ filePath , $ info );
384- $ preview ['source ' ] = $ pathPreview ['source ' ];
375+ if (!$ fileInfo instanceof FileInfo) {
376+ $ preview = $ this ->getPreviewFromPath ($ fileId , $ filePath , $ info );
385377 } else if ($ this ->preview ->isAvailable ($ fileInfo )) {
386- $ preview ['isMimeTypeIcon ' ] = false ;
387378 $ preview ['source ' ] = $ this ->urlGenerator ->linkToRouteAbsolute ('core.Preview.getPreview ' , [
388379 'file ' => $ info ['path ' ],
389380 'c ' => $ this ->view ->getETag ($ info ['path ' ]),
390381 'x ' => 150 ,
391382 'y ' => 150 ,
392383 ]);
384+ $ preview ['mimeType ' ] = $ fileInfo ->getMimetype ();
385+ $ preview ['isMimeTypeIcon ' ] = false ;
393386 } else {
394387 $ preview ['source ' ] = $ this ->getPreviewPathFromMimeType ($ fileInfo ->getMimetype ());
388+ $ preview ['mimeType ' ] = $ fileInfo ->getMimetype ();
395389 }
396390 }
397391
398392 return $ preview ;
399393 }
400394
401- /**
402- * @param string $filePath
403- * @param array $info
404- * @return array
405- */
406- protected function getPreviewFromPath ($ filePath , $ info ) {
395+ protected function getPreviewFromPath (int $ fileId , string $ filePath , array $ info ): array {
407396 $ mimeType = $ info ['is_dir ' ] ? 'dir ' : $ this ->mimeTypeDetector ->detectPath ($ filePath );
408- $ preview = [
397+ return [
409398 'link ' => $ this ->getPreviewLink ($ info ['path ' ], $ info ['is_dir ' ], $ info ['view ' ]),
410399 'source ' => $ this ->getPreviewPathFromMimeType ($ mimeType ),
400+ 'mimeType ' => $ mimeType ,
411401 'isMimeTypeIcon ' => true ,
402+ 'fileId ' => $ fileId ,
403+ 'view ' => $ info ['view ' ] ?: 'files ' ,
412404 ];
413-
414- return $ preview ;
415405 }
416406
417- /**
418- * @param string $mimeType
419- * @return string
420- */
421- protected function getPreviewPathFromMimeType ($ mimeType ) {
407+ protected function getPreviewPathFromMimeType (string $ mimeType ): string {
422408 $ mimeTypeIcon = $ this ->mimeTypeDetector ->mimeTypeIcon ($ mimeType );
423409 if (substr ($ mimeTypeIcon , -4 ) === '.png ' ) {
424410 $ mimeTypeIcon = substr ($ mimeTypeIcon , 0 , -4 ) . '.svg ' ;
@@ -427,18 +413,12 @@ protected function getPreviewPathFromMimeType($mimeType) {
427413 return $ this ->urlGenerator ->getAbsoluteURL ($ mimeTypeIcon );
428414 }
429415
430- /**
431- * @param string $path
432- * @param bool $isDir
433- * @param string $view
434- * @return string
435- */
436- protected function getPreviewLink ($ path , $ isDir , $ view ) {
416+ protected function getPreviewLink (string $ path , bool $ isDir , string $ view ): string {
437417 $ params = [
438418 'dir ' => $ path ,
439419 ];
440420 if (!$ isDir ) {
441- $ params ['dir ' ] = (substr_count ($ path , '/ ' ) === 1 ) ? '/ ' : dirname ($ path );
421+ $ params ['dir ' ] = (substr_count ($ path , '/ ' ) === 1 ) ? '/ ' : \ dirname ($ path );
442422 $ params ['scrollto ' ] = basename ($ path );
443423 }
444424 if ($ view !== '' ) {
0 commit comments