Throw proper StatusException
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
@@ -32,9 +32,11 @@ use OCA\Deck\Db\CardMapper;
|
|||||||
use OCA\Deck\InvalidAttachmentType;
|
use OCA\Deck\InvalidAttachmentType;
|
||||||
use OCA\Deck\NoPermissionException;
|
use OCA\Deck\NoPermissionException;
|
||||||
use OCA\Deck\NotFoundException;
|
use OCA\Deck\NotFoundException;
|
||||||
|
use OCA\Deck\StatusException;
|
||||||
use OCP\AppFramework\Http\Response;
|
use OCP\AppFramework\Http\Response;
|
||||||
use OCP\ICache;
|
use OCP\ICache;
|
||||||
use OCP\ICacheFactory;
|
use OCP\ICacheFactory;
|
||||||
|
use OCP\IL10N;
|
||||||
|
|
||||||
class AttachmentService {
|
class AttachmentService {
|
||||||
|
|
||||||
@@ -48,6 +50,8 @@ class AttachmentService {
|
|||||||
private $application;
|
private $application;
|
||||||
/** @var ICache */
|
/** @var ICache */
|
||||||
private $cache;
|
private $cache;
|
||||||
|
/** @var IL10N */
|
||||||
|
private $l10n;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AttachmentService constructor.
|
* AttachmentService constructor.
|
||||||
@@ -58,16 +62,17 @@ class AttachmentService {
|
|||||||
* @param Application $application
|
* @param Application $application
|
||||||
* @param ICacheFactory $cacheFactory
|
* @param ICacheFactory $cacheFactory
|
||||||
* @param $userId
|
* @param $userId
|
||||||
|
* @param IL10N $l10n
|
||||||
* @throws \OCP\AppFramework\QueryException
|
* @throws \OCP\AppFramework\QueryException
|
||||||
*/
|
*/
|
||||||
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId) {
|
public function __construct(AttachmentMapper $attachmentMapper, CardMapper $cardMapper, PermissionService $permissionService, Application $application, ICacheFactory $cacheFactory, $userId, IL10N $l10n) {
|
||||||
$this->attachmentMapper = $attachmentMapper;
|
$this->attachmentMapper = $attachmentMapper;
|
||||||
$this->cardMapper = $cardMapper;
|
$this->cardMapper = $cardMapper;
|
||||||
$this->permissionService = $permissionService;
|
$this->permissionService = $permissionService;
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
$this->application = $application;
|
$this->application = $application;
|
||||||
$this->cache = $cacheFactory->createDistributed('deck-card-attachments-');
|
$this->cache = $cacheFactory->createDistributed('deck-card-attachments-');
|
||||||
|
$this->l10n = $l10n;
|
||||||
|
|
||||||
// Register shipped attachment services
|
// Register shipped attachment services
|
||||||
// TODO: move this to a plugin based approach once we have different types of attachments
|
// TODO: move this to a plugin based approach once we have different types of attachments
|
||||||
@@ -145,6 +150,9 @@ class AttachmentService {
|
|||||||
} catch (InvalidAttachmentType $e) {
|
} catch (InvalidAttachmentType $e) {
|
||||||
// just store the data
|
// just store the data
|
||||||
}
|
}
|
||||||
|
if ($attachment->getData() === null) {
|
||||||
|
throw new StatusException($this->l10n->t('No data was provided to create an attachment.'));
|
||||||
|
}
|
||||||
$attachment = $this->attachmentMapper->insert($attachment);
|
$attachment = $this->attachmentMapper->insert($attachment);
|
||||||
|
|
||||||
// extend data so the frontend can use it properly after creating
|
// extend data so the frontend can use it properly after creating
|
||||||
|
|||||||
@@ -25,9 +25,11 @@ namespace OCA\Deck\Service;
|
|||||||
|
|
||||||
use OC\Security\CSP\ContentSecurityPolicyManager;
|
use OC\Security\CSP\ContentSecurityPolicyManager;
|
||||||
use OCA\Deck\Db\Attachment;
|
use OCA\Deck\Db\Attachment;
|
||||||
|
use OCA\Deck\StatusException;
|
||||||
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
||||||
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
|
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
|
||||||
use OCP\AppFramework\Http\FileDisplayResponse;
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
||||||
|
use OCP\Files\Cache\IScanner;
|
||||||
use OCP\Files\IAppData;
|
use OCP\Files\IAppData;
|
||||||
use OCP\Files\NotFoundException;
|
use OCP\Files\NotFoundException;
|
||||||
use OCP\Files\NotPermittedException;
|
use OCP\Files\NotPermittedException;
|
||||||
@@ -100,6 +102,10 @@ class FileService implements IAttachmentService {
|
|||||||
return $attachment;
|
return $attachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws StatusException
|
||||||
|
*/
|
||||||
private function getUploadedFile () {
|
private function getUploadedFile () {
|
||||||
$file = $this->request->getUploadedFile('file');
|
$file = $this->request->getUploadedFile('file');
|
||||||
$error = null;
|
$error = null;
|
||||||
@@ -115,13 +121,13 @@ class FileService implements IAttachmentService {
|
|||||||
];
|
];
|
||||||
|
|
||||||
if (empty($file)) {
|
if (empty($file)) {
|
||||||
$error = $this->l10n->t('No file uploaded');
|
$error = $this->l10n->t('No file uploaded or file size exceeds maximum of %s', [\OCP\Util::humanFileSize(\OCP\Util::uploadLimit())]);
|
||||||
}
|
}
|
||||||
if (!empty($file) && array_key_exists('error', $file) && $file['error'] !== UPLOAD_ERR_OK) {
|
if (!empty($file) && array_key_exists('error', $file) && $file['error'] !== UPLOAD_ERR_OK) {
|
||||||
$error = $phpFileUploadErrors[$file['error']];
|
$error = $phpFileUploadErrors[$file['error']];
|
||||||
}
|
}
|
||||||
if ($error !== null) {
|
if ($error !== null) {
|
||||||
throw new \Exception($error);
|
throw new StatusException($error);
|
||||||
}
|
}
|
||||||
return $file;
|
return $file;
|
||||||
}
|
}
|
||||||
@@ -131,7 +137,7 @@ class FileService implements IAttachmentService {
|
|||||||
$folder = $this->getFolder($attachment);
|
$folder = $this->getFolder($attachment);
|
||||||
$fileName = $file['name'];
|
$fileName = $file['name'];
|
||||||
if ($folder->fileExists($fileName)) {
|
if ($folder->fileExists($fileName)) {
|
||||||
throw new \Exception('File already exists.');
|
throw new StatusException('File already exists.');
|
||||||
}
|
}
|
||||||
$target = $folder->newFile($fileName);
|
$target = $folder->newFile($fileName);
|
||||||
$target->putContent(file_get_contents($file['tmp_name'], 'r'));
|
$target->putContent(file_get_contents($file['tmp_name'], 'r'));
|
||||||
@@ -141,6 +147,8 @@ class FileService implements IAttachmentService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This method requires to be used with POST so we can properly get the form data
|
* This method requires to be used with POST so we can properly get the form data
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function update(Attachment $attachment) {
|
public function update(Attachment $attachment) {
|
||||||
$file = $this->getUploadedFile();
|
$file = $this->getUploadedFile();
|
||||||
|
|||||||
Reference in New Issue
Block a user