Implement attachment backend with a first module for app data file upload
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
197
lib/Service/AttachmentService.php
Normal file
197
lib/Service/AttachmentService.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Service;
|
||||
|
||||
|
||||
use OCA\Deck\AppInfo\Application;
|
||||
use OCA\Deck\Db\Acl;
|
||||
use OCA\Deck\Db\Attachment;
|
||||
use OCA\Deck\Db\AttachmentMapper;
|
||||
use OCA\Deck\Db\CardMapper;
|
||||
use OCA\Deck\InvalidAttachmentType;
|
||||
use OCA\Deck\NotFoundException;
|
||||
use OCP\AppFramework\Http\Response;
|
||||
|
||||
class AttachmentService {
|
||||
|
||||
private $attachmentMapper;
|
||||
private $cardMapper;
|
||||
private $permissionService;
|
||||
private $userId;
|
||||
|
||||
/** @var IAttachmentService[] */
|
||||
private $services = [];
|
||||
private $application;
|
||||
|
||||
/**
|
||||
* AttachmentService constructor.
|
||||
*
|
||||
* @param AttachmentMapper $attachmentMapper
|
||||
* @param CardMapper $cardMapper
|
||||
* @param PermissionService $permissionService
|
||||
* @param $userId
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, PermissionService $permissionService, Application $application, $userId) {
|
||||
$this->attachmentMapper = $attachmentMapper;
|
||||
$this->cardMapper = $cardMapper;
|
||||
$this->permissionService = $permissionService;
|
||||
$this->userId = $userId;
|
||||
$this->application = $application;
|
||||
|
||||
// Register shipped attachment services
|
||||
// TODO: move this to a plugin based approach once we have different types of attachments
|
||||
$this->registerAttachmentService('deck_file', FileService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $class
|
||||
* @throws \OCP\AppFramework\QueryException
|
||||
*/
|
||||
public function registerAttachmentService($type, $class) {
|
||||
$this->services[$type] = $this->application->getContainer()->query($class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @return IAttachmentService
|
||||
* @throws InvalidAttachmentType
|
||||
*/
|
||||
public function getService($type) {
|
||||
if (isset($this->services[$type])) {
|
||||
return $this->services[$type];
|
||||
}
|
||||
throw new InvalidAttachmentType($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cardId
|
||||
* @return array
|
||||
* @throws \OCA\Deck\NoPermissionException
|
||||
*/
|
||||
public function findAll($cardId) {
|
||||
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
|
||||
|
||||
$attachments = $this->attachmentMapper->findAll($cardId);
|
||||
foreach ($attachments as &$attachment) {
|
||||
try {
|
||||
$service = $this->getService($attachment->getType());
|
||||
$service->extendData($attachment);
|
||||
} catch (InvalidAttachmentType $e) {
|
||||
// Ingore invalid attachment types when extending the data
|
||||
}
|
||||
}
|
||||
return $attachments;
|
||||
}
|
||||
|
||||
public function create($cardId, $type, $data) {
|
||||
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
|
||||
|
||||
$attachment = new Attachment();
|
||||
$attachment->setCardId($cardId);
|
||||
$attachment->setType($type);
|
||||
$attachment->setData($data);
|
||||
$attachment->setCreatedBy($this->userId);
|
||||
$attachment->setLastModified(time());
|
||||
$attachment->setCreatedAt(time());
|
||||
|
||||
try {
|
||||
$service = $this->getService($attachment->getType());
|
||||
$service->create($attachment);
|
||||
} catch (InvalidAttachmentType $e) {
|
||||
// just store the data
|
||||
}
|
||||
$attachment = $this->attachmentMapper->insert($attachment);
|
||||
|
||||
// extend data so the frontend can use it properly after creating
|
||||
try {
|
||||
$service = $this->getService($attachment->getType());
|
||||
$service->extendData($attachment);
|
||||
} catch (InvalidAttachmentType $e) {
|
||||
// just store the data
|
||||
}
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display the attachment
|
||||
*
|
||||
* @param $cardId
|
||||
* @param $attachmentId
|
||||
* @return Response
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function display($cardId, $attachmentId) {
|
||||
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
|
||||
|
||||
$attachment = $this->attachmentMapper->find($attachmentId);
|
||||
|
||||
try {
|
||||
$service = $this->getService($attachment->getType());
|
||||
return $service->display($attachment);
|
||||
} catch (InvalidAttachmentType $e) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an attachment with custom data
|
||||
*
|
||||
* @param $cardId
|
||||
* @param $attachmentId
|
||||
* @param $request
|
||||
* @return mixed
|
||||
* @throws \OCA\Deck\NoPermissionException
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
|
||||
*/
|
||||
public function update($cardId, $attachmentId, $data) {
|
||||
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
|
||||
$attachment = $this->attachmentMapper->find($attachmentId);
|
||||
$attachment->setData($data);
|
||||
try {
|
||||
$service = $this->getService($attachment->getType());
|
||||
$service->update($attachment);
|
||||
} catch (InvalidAttachmentType $e) {
|
||||
// just update without further action
|
||||
}
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
public function delete($cardId, $attachmentId) {
|
||||
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
|
||||
|
||||
$attachment = $this->attachmentMapper->find($attachmentId);
|
||||
try {
|
||||
$service = $this->getService($attachment->getType());
|
||||
$service->delete($attachment);
|
||||
} catch (InvalidAttachmentType $e) {
|
||||
// just delete without further action
|
||||
}
|
||||
$this->attachmentMapper->delete($attachment);
|
||||
return $attachment;
|
||||
}
|
||||
}
|
||||
@@ -41,19 +41,22 @@ class CardService {
|
||||
private $boardService;
|
||||
private $assignedUsersMapper;
|
||||
|
||||
public function __construct(CardMapper $cardMapper, StackMapper $stackMapper, PermissionService $permissionService, BoardService $boardService, AssignedUsersMapper $assignedUsersMapper) {
|
||||
public function __construct(CardMapper $cardMapper, StackMapper $stackMapper, PermissionService $permissionService, BoardService $boardService, AssignedUsersMapper $assignedUsersMapper, AttachmentService $attachmentService) {
|
||||
$this->cardMapper = $cardMapper;
|
||||
$this->stackMapper = $stackMapper;
|
||||
$this->permissionService = $permissionService;
|
||||
$this->boardService = $boardService;
|
||||
$this->assignedUsersMapper = $assignedUsersMapper;
|
||||
$this->attachmentService = $attachmentService;
|
||||
}
|
||||
|
||||
public function find($cardId) {
|
||||
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_READ);
|
||||
$card = $this->cardMapper->find($cardId);
|
||||
$assignedUsers = $this->assignedUsersMapper->find($card->getId());
|
||||
$attachments = $this->attachmentService->findAll($cardId);
|
||||
$card->setAssignedUsers($assignedUsers);
|
||||
$card->setAttachments($attachments);
|
||||
return $card;
|
||||
}
|
||||
|
||||
|
||||
152
lib/Service/FileService.php
Normal file
152
lib/Service/FileService.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Service;
|
||||
|
||||
|
||||
use OCA\Deck\Db\Attachment;
|
||||
use OCP\AppFramework\Http\FileDisplayResponse;
|
||||
use OCP\Files\IAppData;
|
||||
use OCP\Files\NotFoundException;
|
||||
use OCP\Files\NotPermittedException;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
use OCP\Files\SimpleFS\ISimpleFolder;
|
||||
use OCP\IL10N;
|
||||
use OCP\IRequest;
|
||||
|
||||
|
||||
class FileService implements IAttachmentService {
|
||||
|
||||
private $l10n;
|
||||
private $appData;
|
||||
private $request;
|
||||
|
||||
public function __construct(
|
||||
IL10N $l10n,
|
||||
IAppData $appData,
|
||||
IRequest $request
|
||||
) {
|
||||
$this->l10n = $l10n;
|
||||
$this->appData = $appData;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attachment $attachment
|
||||
* @return ISimpleFile
|
||||
* @throws NotFoundException
|
||||
* @throws NotPermittedException
|
||||
*/
|
||||
private function getFileForAttachment(Attachment $attachment) {
|
||||
return $this->getFolder($attachment)
|
||||
->getFile($attachment->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attachment $attachment
|
||||
* @return ISimpleFolder
|
||||
* @throws NotPermittedException
|
||||
*/
|
||||
private function getFolder(Attachment $attachment) {
|
||||
$folderName = 'file-card-' . (int)$attachment->getCardId();
|
||||
try {
|
||||
$folder = $this->appData->getFolder($folderName);
|
||||
} catch (NotFoundException $e) {
|
||||
$folder = $this->appData->newFolder($folderName);
|
||||
}
|
||||
return $folder;
|
||||
}
|
||||
|
||||
public function extendData(Attachment $attachment) {
|
||||
try {
|
||||
$file = $this->getFileForAttachment($attachment);
|
||||
} catch (NotFoundException $e) {
|
||||
// TODO: log error
|
||||
return $attachment;
|
||||
} catch (NotPermittedException $e) {
|
||||
return $attachment;
|
||||
}
|
||||
$attachment->setExtendedData([
|
||||
'filesize' => $file->getSize(),
|
||||
'mimetype' => $file->getMimeType(),
|
||||
'info' => pathinfo($file->getName())
|
||||
]);
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
public function create(Attachment $attachment) {
|
||||
$file = $this->request->getUploadedFile('file');
|
||||
$cardId = $attachment->getCardId();
|
||||
$error = null;
|
||||
$phpFileUploadErrors = [
|
||||
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
|
||||
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
|
||||
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
|
||||
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
|
||||
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
|
||||
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
|
||||
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
|
||||
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
|
||||
];
|
||||
|
||||
if (empty($file)) {
|
||||
$error = $this->l10n->t('No file uploaded');
|
||||
}
|
||||
if (!empty($file) && array_key_exists('error', $file) && $file['error'] !== UPLOAD_ERR_OK) {
|
||||
$error = $phpFileUploadErrors[$file['error']];
|
||||
}
|
||||
if ($error !== null) {
|
||||
throw new \RuntimeException($error);
|
||||
}
|
||||
|
||||
$folder = $this->getFolder($attachment);
|
||||
$fileName = $file['name'];
|
||||
if ($folder->fileExists($fileName)) {
|
||||
throw new \Exception('File already exists.');
|
||||
}
|
||||
$target = $folder->newFile($fileName);
|
||||
$target->putContent(file_get_contents($file['tmp_name'], 'r'));
|
||||
|
||||
$attachment->setData($fileName);
|
||||
}
|
||||
|
||||
public function update(Attachment $attachment) {
|
||||
$file = $this->getFileForAttachment($attachment);
|
||||
|
||||
}
|
||||
|
||||
public function delete(Attachment $attachment) {
|
||||
try {
|
||||
$file = $this->getFileForAttachment($attachment);
|
||||
$file->delete();
|
||||
} catch (NotFoundException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
public function display(Attachment $attachment) {
|
||||
$file = $this->getFileForAttachment($attachment);
|
||||
$response = new FileDisplayResponse($file);
|
||||
$response->addHeader('Content-Type', $file->getMimeType());
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
84
lib/Service/IAttachmentService.php
Normal file
84
lib/Service/IAttachmentService.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Service;
|
||||
|
||||
|
||||
use OCA\Deck\Db\Attachment;
|
||||
use OCP\AppFramework\Http\Response;
|
||||
|
||||
/**
|
||||
* Interface IAttachmentService
|
||||
*
|
||||
* Implement this interface to extend the default attachment behaviour
|
||||
* This interface allows to extend/reduce the data stored with an attachment,
|
||||
* as well as rendering a custom output per attachment type
|
||||
*
|
||||
*/
|
||||
interface IAttachmentService {
|
||||
|
||||
/**
|
||||
* Add extended data to the returned data of an attachment
|
||||
*
|
||||
* @param Attachment $attachment
|
||||
* @return mixed
|
||||
*/
|
||||
public function extendData(Attachment $attachment);
|
||||
|
||||
/**
|
||||
* Display the attachment
|
||||
*
|
||||
* TODO: Move to IAttachmentDisplayService for better separation
|
||||
*
|
||||
* @param Attachment $attachment
|
||||
* @return Response
|
||||
*/
|
||||
public function display(Attachment $attachment);
|
||||
|
||||
/**
|
||||
* Create a new attachment
|
||||
*
|
||||
* This method will be called before inserting the attachment entry in the database
|
||||
*
|
||||
* @param Attachment $attachment
|
||||
*/
|
||||
public function create(Attachment $attachment);
|
||||
|
||||
/**
|
||||
* Update an attachment with custom data
|
||||
*
|
||||
* This method will be called before updating the attachment entry in the database
|
||||
*
|
||||
* @param Attachment $attachment
|
||||
*/
|
||||
public function update(Attachment $attachment);
|
||||
|
||||
/**
|
||||
* Delete an attachment
|
||||
*
|
||||
* This method will be called before removing the attachment entry from the database
|
||||
*
|
||||
* @param Attachment $attachment
|
||||
*/
|
||||
public function delete(Attachment $attachment);
|
||||
}
|
||||
Reference in New Issue
Block a user