managing errors and exceptions

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/66/head
Maxence Lange 2018-11-28 11:10:02 -01:00
rodzic 1643206f5c
commit 4c5450bd86
3 zmienionych plików z 64 dodań i 6 usunięć

Wyświetl plik

@ -0,0 +1,8 @@
<?php
namespace OCA\Social\Exceptions;
class CacheContentMimeTypeException extends \Exception {
}

Wyświetl plik

@ -34,6 +34,7 @@ namespace OCA\Social\Service\ActivityPub;
use Exception;
use OCA\Social\Db\CacheDocumentsRequest;
use OCA\Social\Exceptions\CacheContentException;
use OCA\Social\Exceptions\CacheContentMimeTypeException;
use OCA\Social\Exceptions\CacheContentSizeException;
use OCA\Social\Exceptions\CacheDocumentDoesNotExistException;
use OCA\Social\Model\ActivityPub\ACore;
@ -47,6 +48,10 @@ use OCP\Files\SimpleFS\ISimpleFile;
class DocumentService implements ICoreService {
const ERROR_SIZE = 1;
const ERROR_MIMETYPE = 2;
/** @var CacheDocumentsRequest */
private $cacheDocumentsRequest;
@ -84,15 +89,19 @@ class DocumentService implements ICoreService {
*/
public function cacheRemoteDocument(string $id, bool $public = false) {
$document = $this->cacheDocumentsRequest->getById($id, $public);
if ($document->getError() > 0) {
throw new CacheDocumentDoesNotExistException();
}
if ($document->getLocalCopy() !== '') {
return $document;
}
// TODO - ignore this if getCaching is older than 15 minutes
if ($document->getCaching() > (time() - (CacheDocumentsRequest::CACHE_TTL * 60))) {
return $document;
}
$mime = '';
$this->cacheDocumentsRequest->initCaching($document);
try {
@ -100,12 +109,19 @@ class DocumentService implements ICoreService {
$document->setMimeType($mime);
$document->setLocalCopy($localCopy);
$this->cacheDocumentsRequest->endCaching($document);
return $document;
} catch (CacheContentMimeTypeException $e) {
$document->setMimeType($mime);
$document->setError(self::ERROR_MIMETYPE);
$this->cacheDocumentsRequest->endCaching($document);
} catch (CacheContentSizeException $e) {
$document->setError(self::ERROR_SIZE);
$this->cacheDocumentsRequest->endCaching($document);
} catch (CacheContentException $e) {
}
return $document;
throw new CacheDocumentDoesNotExistException();
}
@ -128,18 +144,22 @@ class DocumentService implements ICoreService {
/**
* @return int
* @throws CacheDocumentDoesNotExistException
* @throws NotPermittedException
* @throws Exception
*/
public function manageCacheDocuments(): int {
$update = $this->cacheDocumentsRequest->getNotCachedDocuments();
$count = 0;
foreach ($update as $item) {
$this->cacheRemoteDocument($item->getId());
try {
$this->cacheRemoteDocument($item->getId());
} catch (Exception $e) {
continue;
}
$count++;
}
return sizeof($update);
return $count;
}

Wyświetl plik

@ -32,7 +32,9 @@ namespace OCA\Social\Service;
use Exception;
use OCA\Social\Exceptions\CacheContentException;
use OCA\Social\Exceptions\CacheContentMimeTypeException;
use OCA\Social\Exceptions\CacheContentSizeException;
use OCA\Social\Exceptions\CacheDocumentDoesNotExistException;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
@ -80,6 +82,7 @@ class CacheService {
* @throws CacheContentException
* @throws NotPermittedException
* @throws CacheContentSizeException
* @throws CacheContentMimeTypeException
*/
public function saveRemoteFileToCache(string $url, &$mime = '') {
@ -108,6 +111,8 @@ class CacheService {
$mime = mime_content_type($tmpPath);
fclose($tmpFile);
$this->filterMimeTypes($mime);
$cache = $folder->newFile($filename);
$cache->putContent($content);
@ -115,13 +120,38 @@ class CacheService {
}
/**
*
* @param string $mime
*
* @throws CacheContentMimeTypeException
*/
public function filterMimeTypes(string $mime) {
$allowedMimeType = [
'image/jpeg',
'image/gif',
'image/png'
];
if (in_array($mime, $allowedMimeType)) {
return;
}
throw new CacheContentMimeTypeException();
}
/**
* @param string $path
*
* @return ISimpleFile
* @throws CacheContentException
* @throws CacheDocumentDoesNotExistException
*/
public function getContentFromCache(string $path) {
if ($path === '') {
throw new CacheDocumentDoesNotExistException();
}
$pos = strrpos($path, '/');
$dir = substr($path, 0, $pos);