Completed
Push — master ( 5c52af...d1103e )
by Sander
16s
created

FileController::updateFile()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 21.5206

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 18
nop 3
dl 0
loc 18
ccs 5
cts 15
cp 0.3333
crap 21.5206
rs 8.2222
c 0
b 0
f 0
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(
28
			$AppName,
29
			$request,
30
			'GET, POST, DELETE, PUT, PATCH, OPTIONS',
31
			'Authorization, Content-Type, Accept',
32
			86400);
33
		$this->userId = $UserId;
34
		$this->fileService = $fileService;
35
	}
36
37
38
	/**
39
	 * @NoAdminRequired
40
	 * @NoCSRFRequired
41
	 */
42 1
	public function uploadFile($data, $filename, $mimetype, $size) {
43
		$file = array(
44 1
			'filename' => $filename,
45 1
			'size' => $size,
46 1
			'mimetype' => $mimetype,
47 1
			'file_data' => $data,
48 1
			'user_id' => $this->userId
49 1
		);
50 1
		return new JSONResponse($this->fileService->createFile($file, $this->userId));
51
	}
52
53
	/**
54
	 * @NoAdminRequired
55
	 * @NoCSRFRequired
56
	 */
57 1
	public function getFile($file_id) {
58 1
		return new JSONResponse($this->fileService->getFile($file_id, $this->userId));
59
	}
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 */
64 1
	public function deleteFile($file_id) {
65 1
		return new JSONResponse($this->fileService->deleteFile($file_id, $this->userId));
66
	}
67
68 1
	public function updateFile($file_id, $file_data, $filename){
69
		try{
70 1
			$file = $this->fileService->getFile($file_id, $this->userId);
71 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...
72
73
		}
74 1
		if($file){
75
			if($file_data) {
76
				$file->setFileData($file_data);
77
			}
78
			if($filename) {
79
				$file->setFilename($filename);
80
			}
81
			if($filename || $file_data){
82
				new JSONResponse($this->fileService->updateFile($file));
83
			}
84
		}
85
	}
86
}