Completed
Push — master ( c14c36...9b4ff4 )
by Sander
10s
created

FileController::uploadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 4
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
			'file_data' => $data
43 1
		);
44 1
		return new JSONResponse($this->fileService->createFile($file, $this->userId));
45
	}
46
47
	/**
48
	 * @NoAdminRequired
49
	 * @NoCSRFRequired
50
	 */
51 1
	public function getFile($file_id) {
52 1
		return new JSONResponse($this->fileService->getFile($file_id, $this->userId));
53
	}
54
	/**
55
	 * @NoAdminRequired
56
	 * @NoCSRFRequired
57
	 */
58 1
	public function deleteFile($file_id) {
59 1
		return new JSONResponse($this->fileService->deleteFile($file_id, $this->userId));
60
	}
61
62 1
	public function updateFile($file_id, $file_data, $filename){
63
		try{
64 1
			$file = $this->fileService->getFile($file_id, $this->userId);
65 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...
66
67
		}
68 1
		if($file){
69
			if($file_data) {
70
				$file->setFileData($file_data);
71
			}
72
			if($filename) {
73
				$file->setFilename($filename);
74
			}
75
			if($filename || $file_data){
76
				new JSONResponse($this->fileService->updateFile($file));
77
			}
78
		}
79
	}
80
}