|
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
|
|
|
); |
|
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
|
|
|
} catch (\Exception $doesNotExistException){ |
|
|
|
|
|
|
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
|
|
|
} |