passman/controller/filecontroller.php

115 lines
2.8 KiB
PHP
Raw Normal View History

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;
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;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
2016-09-15 00:57:38 +08:00
class FileController extends ApiController {
private $userId;
private $fileService;
private $logger;
2016-09-15 00:57:38 +08:00
public function __construct($AppName,
IRequest $request,
$UserId,
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;
$this->logger = $logger;
2016-09-15 00:57:38 +08:00
}
/**
* @NoAdminRequired
* @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
* @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
}
2016-09-15 00:57:38 +08:00
/**
* @NoAdminRequired
* @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
/**
* @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 {
2016-10-05 20:09:15 +08:00
$file = $this->fileService->getFile($file_id, $this->userId);
} catch (\Exception $doesNotExistException) {
2016-10-05 20:09:15 +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
}
if ($filename) {
2016-10-05 20:09:15 +08:00
$file->setFilename($filename);
}
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
}
}
}
}