Completed
Push — master ( 946999...5f7661 )
by Marcos
04:12
created

FileController::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Sander Brand <[email protected]>
9
 * @copyright Sander Brand 2016
10
 */
11
12
namespace OCA\Passman\Controller;
13
14
use OCP\AppFramework\Db\DoesNotExistException;
15
use OCP\IRequest;
16
use OCP\AppFramework\Http\JSONResponse;
17
use OCP\AppFramework\ApiController;
18
use OCA\Passman\Service\FileService;
19
20
class FileController extends ApiController {
21
	private $userId;
22
	private $fileService;
23
	public function __construct($AppName,
24
								IRequest $request,
25
								$UserId,
26
								FileService $fileService){
27
		parent::__construct($AppName, $request);
28
		$this->userId = $UserId;
29
		$this->fileService = $fileService;
30
	}
31
32
33
	/**
34
	 * @NoAdminRequired
35
	 * @NoCSRFRequired
36
	 */
37 1
	public function uploadFile($data, $filename, $mimetype, $size) {
38
		$file = array(
39 1
			'filename' => $filename,
40 1
			'size' => $size,
41 1
			'mimetype' => $mimetype,
42 1
			'file_data' => $data,
43 1
			'user_id' => $this->userId
44 1
		);
45 1
		return new JSONResponse($this->fileService->createFile($file, $this->userId));
46
	}
47
48
	/**
49
	 * @NoAdminRequired
50
	 * @NoCSRFRequired
51
	 */
52 1
	public function getFile($file_id) {
53 1
		return new JSONResponse($this->fileService->getFile($file_id, $this->userId));
54
	}
55
	/**
56
	 * @NoAdminRequired
57
	 * @NoCSRFRequired
58
	 */
59 1
	public function deleteFile($file_id) {
60 1
		return new JSONResponse($this->fileService->deleteFile($file_id, $this->userId));
61
	}
62
63 1
	public function updateFile($file_id, $file_data, $filename){
64
		try{
65 1
			$file = $this->fileService->getFile($file_id, $this->userId);
66 1
		} catch (\Exception $doesNotExistException){
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
67
68
		}
69 1
		if($file){
70
			if($file_data) {
71
				$file->setFileData($file_data);
72
			}
73
			if($filename) {
74
				$file->setFilename($filename);
75
			}
76
			if($filename || $file_data){
77
				new JSONResponse($this->fileService->updateFile($file));
78
			}
79
		}
80
	}
81
}