2016-09-15 00:57:38 +08:00
|
|
|
<?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;
|
|
|
|
|
2021-10-24 01:59:13 +08:00
|
|
|
use OCA\Passman\Service\FileService;
|
|
|
|
use OCP\AppFramework\ApiController;
|
2016-10-05 20:09:15 +08:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2016-09-15 00:57:38 +08:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2021-10-24 01:59:13 +08:00
|
|
|
use OCP\IRequest;
|
2021-10-29 22:35:43 +08:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-09-15 00:57:38 +08:00
|
|
|
|
|
|
|
class FileController extends ApiController {
|
|
|
|
private $userId;
|
|
|
|
private $fileService;
|
2021-10-29 22:35:43 +08:00
|
|
|
private $logger;
|
2021-10-24 01:59:13 +08:00
|
|
|
|
2016-09-15 00:57:38 +08:00
|
|
|
public function __construct($AppName,
|
2021-10-24 01:59:13 +08:00
|
|
|
IRequest $request,
|
2021-12-16 03:18:51 +08:00
|
|
|
$UserId,
|
2021-10-29 22:35:43 +08:00
|
|
|
FileService $fileService,
|
|
|
|
LoggerInterface $logger) {
|
2017-01-19 20:39:01 +08:00
|
|
|
parent::__construct(
|
|
|
|
$AppName,
|
|
|
|
$request,
|
|
|
|
'GET, POST, DELETE, PUT, PATCH, OPTIONS',
|
|
|
|
'Authorization, Content-Type, Accept',
|
|
|
|
86400);
|
2016-09-15 00:57:38 +08:00
|
|
|
$this->userId = $UserId;
|
|
|
|
$this->fileService = $fileService;
|
2021-10-29 22:35:43 +08:00
|
|
|
$this->logger = $logger;
|
2016-09-15 00:57:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-15 00:57:38 +08:00
|
|
|
*/
|
|
|
|
public function uploadFile($data, $filename, $mimetype, $size) {
|
|
|
|
$file = array(
|
|
|
|
'filename' => $filename,
|
|
|
|
'size' => $size,
|
|
|
|
'mimetype' => $mimetype,
|
2017-01-02 22:25:41 +08:00
|
|
|
'file_data' => $data,
|
|
|
|
'user_id' => $this->userId
|
2016-09-15 00:57:38 +08:00
|
|
|
);
|
2016-10-08 03:33:23 +08:00
|
|
|
return new JSONResponse($this->fileService->createFile($file, $this->userId));
|
2016-09-15 00:57:38 +08:00
|
|
|
}
|
|
|
|
|
2016-09-16 03:21:34 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-16 03:21:34 +08:00
|
|
|
*/
|
|
|
|
public function getFile($file_id) {
|
2016-10-08 03:33:23 +08:00
|
|
|
return new JSONResponse($this->fileService->getFile($file_id, $this->userId));
|
2016-09-16 03:21:34 +08:00
|
|
|
}
|
2021-10-24 01:59:13 +08:00
|
|
|
|
2016-09-15 00:57:38 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2016-12-23 20:46:43 +08:00
|
|
|
* @NoCSRFRequired
|
2016-09-15 00:57:38 +08:00
|
|
|
*/
|
|
|
|
public function deleteFile($file_id) {
|
2016-10-08 03:33:23 +08:00
|
|
|
return new JSONResponse($this->fileService->deleteFile($file_id, $this->userId));
|
2016-09-15 00:57:38 +08:00
|
|
|
}
|
2016-10-05 20:09:15 +08:00
|
|
|
|
2021-10-24 01:59:13 +08:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function deleteFiles($file_ids) {
|
2021-10-25 20:42:40 +08:00
|
|
|
$failed_file_ids = [];
|
2021-10-24 01:59:13 +08:00
|
|
|
if ($file_ids != null && !empty($file_ids)) {
|
2021-10-25 20:42:40 +08:00
|
|
|
$decoded_file_ids = json_decode($file_ids);
|
|
|
|
foreach ($decoded_file_ids as $file_id) {
|
2021-10-24 01:59:13 +08:00
|
|
|
try {
|
|
|
|
$this->fileService->deleteFile($file_id, $this->userId);
|
|
|
|
} catch (\Exception $e) {
|
2021-10-25 20:25:20 +08:00
|
|
|
$this->logger->error('Error deleting file (' . $file_id . ') in filecontroller:deleteFiles()',
|
|
|
|
['exception' => $e->getTrace(), 'message' => $e->getMessage()]);
|
2021-10-25 20:42:40 +08:00
|
|
|
$failed_file_ids[] = $file_id;
|
2021-10-24 01:59:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 20:42:40 +08:00
|
|
|
return new JSONResponse(array('ok' => empty($failed_file_ids), 'failed' => $failed_file_ids));
|
2021-10-24 01:59:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function updateFile($file_id, $file_data, $filename) {
|
|
|
|
try {
|
2016-10-05 20:09:15 +08:00
|
|
|
$file = $this->fileService->getFile($file_id, $this->userId);
|
2021-10-24 01:59:13 +08:00
|
|
|
} catch (\Exception $doesNotExistException) {
|
2016-10-05 20:09:15 +08:00
|
|
|
|
|
|
|
}
|
2021-10-24 01:59:13 +08:00
|
|
|
if ($file) {
|
|
|
|
if ($file_data) {
|
2016-10-05 22:58:19 +08:00
|
|
|
$file->setFileData($file_data);
|
2016-10-05 20:09:15 +08:00
|
|
|
}
|
2021-10-24 01:59:13 +08:00
|
|
|
if ($filename) {
|
2016-10-05 20:09:15 +08:00
|
|
|
$file->setFilename($filename);
|
|
|
|
}
|
2021-10-24 01:59:13 +08:00
|
|
|
if ($filename || $file_data) {
|
2016-10-08 03:33:23 +08:00
|
|
|
new JSONResponse($this->fileService->updateFile($file));
|
2016-10-05 20:09:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 01:59:13 +08:00
|
|
|
}
|