passman/controller/filecontroller.php

86 lines
1.9 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;
2016-10-05 20:09:15 +08:00
use OCP\AppFramework\Db\DoesNotExistException;
2016-09-15 00:57:38 +08:00
use OCP\IRequest;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\ApiController;
use OCA\Passman\Service\FileService;
class FileController extends ApiController {
private $userId;
private $fileService;
public function __construct($AppName,
IRequest $request,
$UserId,
FileService $fileService){
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;
}
/**
* @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
2017-01-04 04:12:24 +08:00
public function updateFile($file_id, $file_data, $filename){
2016-10-05 20:09:15 +08:00
try{
$file = $this->fileService->getFile($file_id, $this->userId);
} catch (\Exception $doesNotExistException){
2016-10-05 20:09:15 +08:00
}
if($file){
2016-10-05 22:58:19 +08:00
if($file_data) {
$file->setFileData($file_data);
2016-10-05 20:09:15 +08:00
}
if($filename) {
$file->setFilename($filename);
}
2016-10-05 22:58:19 +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
}
}
}
2016-09-15 00:57:38 +08:00
}