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){ |
|
|
|
|
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
|
|
|
} |