Welcome to mirror list, hosted at ThFree Co, Russian Federation.

filecontroller.php « controller - github.com/nextcloud/passman.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd5cef402d62feb03ed46df6095e101f26df5c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
 * Nextcloud - passman
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Sander Brand <brantje@gmail.com>
 * @copyright Sander Brand 2016
 */

namespace OCA\Passman\Controller;

use OCA\Passman\Service\FileService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

class FileController extends ApiController {
	private $userId;
	private $fileService;
	private $logger;

	public function __construct($AppName,
	                            IRequest $request,
								$UserId,
		                        FileService $fileService,
		                        LoggerInterface $logger) {
		parent::__construct(
			$AppName,
			$request,
			'GET, POST, DELETE, PUT, PATCH, OPTIONS',
			'Authorization, Content-Type, Accept',
			86400);
		$this->userId = $UserId;
		$this->fileService = $fileService;
		$this->logger = $logger;
	}


	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function uploadFile($data, $filename, $mimetype, $size) {
		$file = array(
			'filename' => $filename,
			'size' => $size,
			'mimetype' => $mimetype,
			'file_data' => $data,
			'user_id' => $this->userId
		);
		return new JSONResponse($this->fileService->createFile($file, $this->userId));
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function getFile($file_id) {
		return new JSONResponse($this->fileService->getFile($file_id, $this->userId));
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function deleteFile($file_id) {
		return new JSONResponse($this->fileService->deleteFile($file_id, $this->userId));
	}

	/**
	 * @NoAdminRequired
	 * @NoCSRFRequired
	 */
	public function deleteFiles($file_ids) {
		$failed_file_ids = [];
		if ($file_ids != null && !empty($file_ids)) {
			$decoded_file_ids = json_decode($file_ids);
			foreach ($decoded_file_ids as $file_id) {
				try {
					$this->fileService->deleteFile($file_id, $this->userId);
				} catch (\Exception $e) {
					$this->logger->error('Error deleting file (' . $file_id . ') in filecontroller:deleteFiles()',
						['exception' => $e->getTrace(), 'message' => $e->getMessage()]);
					$failed_file_ids[] = $file_id;
					continue;
				}
			}
		}
		return new JSONResponse(array('ok' => empty($failed_file_ids), 'failed' => $failed_file_ids));
	}

	public function updateFile($file_id, $file_data, $filename) {
		try {
			$file = $this->fileService->getFile($file_id, $this->userId);
		} catch (\Exception $doesNotExistException) {

		}
		if ($file) {
			if ($file_data) {
				$file->setFileData($file_data);
			}
			if ($filename) {
				$file->setFilename($filename);
			}
			if ($filename || $file_data) {
				new JSONResponse($this->fileService->updateFile($file));
			}
		}
	}
}