Passed
Push — feature/playlist_improvements ( 740c42...e85336 )
by Pauli
11:29
created

ShareController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 48
c 3
b 0
f 0
dl 0
loc 91
ccs 0
cts 21
cp 0
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fileInfo() 0 25 5
A parsePlaylist() 0 31 4
A __construct() 0 11 1
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2018 - 2020
11
 */
12
13
namespace OCA\Music\Controller;
14
15
use \OCP\AppFramework\Controller;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use \OCP\AppFramework\Http;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use \OCP\AppFramework\Http\JSONResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\JSONResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use \OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
use \OCA\Music\AppFramework\Core\Logger;
21
use \OCA\Music\Http\ErrorResponse;
22
use \OCA\Music\Utility\PlaylistFileService;
23
use \OCA\Music\Utility\Scanner;
24
25
/**
26
 * End-points for shared audio file handling. Methods of this class may be
27
 * used while there is no user logged in.
28
 */
29
class ShareController extends Controller {
30
31
	/** @var \OCP\Share\IManager */
0 ignored issues
show
Bug introduced by
The type OCP\Share\IManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
	private $shareManager;
33
	/** @var Scanner */
34
	private $scanner;
35
	/** @var PlaylistFileService */
36
	private $playlistFileService;
37
	/** @var Logger */
38
	private $logger;
39
40
	public function __construct($appname,
41
								IRequest $request,
42
								Scanner $scanner,
43
								PlaylistFileService $playlistFileService,
44
								Logger $logger,
45
								\OCP\Share\IManager $shareManager) {
46
		parent::__construct($appname, $request);
47
		$this->shareManager = $shareManager;
48
		$this->scanner = $scanner;
49
		$this->playlistFileService = $playlistFileService;
50
		$this->logger = $logger;
51
	}
52
53
	/**
54
	 * @PublicPage
55
	 * @NoCSRFRequired
56
	 */
57
	public function fileInfo($token, $fileId) {
58
		$share = $this->shareManager->getShareByToken($token);
59
		$fileOwner = $share->getShareOwner();
60
		$fileOwnerHome = $this->scanner->resolveUserFolder($fileOwner);
61
62
		// If non-zero fileId is given, the $share identified by the token should
63
		// be the file's parent directory. Otherwise the share is the target file.
64
		if ($fileId == 0) {
65
			$fileId = $share->getNodeId();
66
		} else {
67
			$folderId = $share->getNodeId();
68
			$matchingFolders = $fileOwnerHome->getById($folderId);
69
			if (empty($matchingFolders)
70
			|| empty($matchingFolders[0]->getById($fileId))) {
71
				// no such shared folder or the folder does not contain the given file
72
				$fileId = null;
73
			}
74
		}
75
76
		$info = $this->scanner->getFileInfo($fileId, $fileOwner, $fileOwnerHome);
77
78
		if ($info) {
79
			return new JSONResponse($info);
80
		} else {
81
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
82
		}
83
	}
84
85
	/**
86
	 * @PublicPage
87
	 * @NoCSRFRequired
88
	 */
89
	public function parsePlaylist($token, $fileId) {
90
		$share = $this->shareManager->getShareByToken($token);
91
		$fileOwner = $share->getShareOwner();
92
		$fileOwnerHome = $this->scanner->resolveUserFolder($fileOwner);
93
94
		$matchingFolders = $fileOwnerHome->getById($share->getNodeId());
95
96
		try {
97
			if (empty($matchingFolders)) {
98
				throw new \OCP\Files\NotFoundException();
0 ignored issues
show
Bug introduced by
The type OCP\Files\NotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
			}
100
			$sharedFolder = $matchingFolders[0];
101
			$result = $this->playlistFileService->parseFile($fileId, $sharedFolder);
102
103
			// compose the final result
104
			$result['files'] = \array_map(function($fileAndCaption) use ($sharedFolder) {
105
				$file = $fileAndCaption['file'];
106
				return [
107
					'id' => $file->getId(),
108
					'name' => $file->getName(),
109
					'path' => $sharedFolder->getRelativePath($file->getParent()->getPath()),
110
					'mimetype' => $file->getMimeType()
111
				];
112
			}, $result['files']);
113
			return new JSONResponse($result);
114
		}
115
		catch (\OCP\Files\NotFoundException $ex) {
116
			return new ErrorResponse(Http::STATUS_NOT_FOUND, 'playlist file not found');
117
		}
118
		catch (\UnexpectedValueException $ex) {
119
			return new ErrorResponse(Http::STATUS_UNSUPPORTED_MEDIA_TYPE, $ex->getMessage());
120
		}
121
	}
122
}
123