error on too big document

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/53/head
Maxence Lange 2018-11-25 11:50:12 -01:00
rodzic ffd70a71a6
commit 22c6cde699
10 zmienionych plików z 94 dodań i 9 usunięć

Wyświetl plik

@ -434,6 +434,13 @@
<notnull>true</notnull> <notnull>true</notnull>
</field> </field>
<field>
<name>error</name>
<type>integer</type>
<length>1</length>
<notnull>true</notnull>
</field>
<field> <field>
<name>creation</name> <name>creation</name>
<type>timestamp</type> <type>timestamp</type>

Wyświetl plik

@ -5,7 +5,7 @@
<name>Social</name> <name>Social</name>
<summary>🎉 Nextcloud becomes part of the federated social networks!</summary> <summary>🎉 Nextcloud becomes part of the federated social networks!</summary>
<description><![CDATA[test]]></description> <description><![CDATA[test]]></description>
<version>0.0.42</version> <version>0.0.43</version>
<licence>agpl</licence> <licence>agpl</licence>
<author mail="maxence@artificial-owl.com">Maxence Lange</author> <author mail="maxence@artificial-owl.com">Maxence Lange</author>
<author mail="jus@bitgrid.net">Julius Härtl</author> <author mail="jus@bitgrid.net">Julius Härtl</author>

Wyświetl plik

@ -80,6 +80,7 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder {
$qb = $this->getCacheDocumentsUpdateSql(); $qb = $this->getCacheDocumentsUpdateSql();
$this->limitToIdString($qb, $document->getId()); $this->limitToIdString($qb, $document->getId());
$qb->set('local_copy', $qb->createNamedParameter($document->getLocalCopy())); $qb->set('local_copy', $qb->createNamedParameter($document->getLocalCopy()));
$qb->set('error', $qb->createNamedParameter($document->getError()));
$qb->execute(); $qb->execute();
} }

Wyświetl plik

@ -31,8 +31,6 @@ namespace OCA\Social\Db;
use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TArrayTools;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Model\ActivityPub\Document; use OCA\Social\Model\ActivityPub\Document;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
@ -79,7 +77,7 @@ class CacheDocumentsRequestBuilder extends CoreRequestBuilder {
/** @noinspection PhpMethodParametersCountMismatchInspection */ /** @noinspection PhpMethodParametersCountMismatchInspection */
$qb->select( $qb->select(
'cd.id', 'cd.type', 'cd.media_type', 'cd.mime_type', 'cd.url', 'cd.local_copy', 'cd.id', 'cd.type', 'cd.media_type', 'cd.mime_type', 'cd.url', 'cd.local_copy',
'cd.public', 'cd.creation', 'cd.caching' 'cd.public', 'cd.error', 'cd.creation', 'cd.caching'
) )
->from(self::TABLE_CACHE_DOCUMENTS, 'cd'); ->from(self::TABLE_CACHE_DOCUMENTS, 'cd');

Wyświetl plik

@ -155,7 +155,6 @@ class CoreRequestBuilder {
} }
/** /**
* Limit the request to the ActorId * Limit the request to the ActorId
* *
@ -477,6 +476,8 @@ class CoreRequestBuilder {
->selectAlias('cd.url', 'cachedocument_url') ->selectAlias('cd.url', 'cachedocument_url')
->selectAlias('cd.local_copy', 'cachedocument_local_copy') ->selectAlias('cd.local_copy', 'cachedocument_local_copy')
->selectAlias('cd.caching', 'cachedocument_caching') ->selectAlias('cd.caching', 'cachedocument_caching')
->selectAlias('cd.public', 'cachedocument_public')
->selectAlias('cd.error', 'cachedocument_error')
->selectAlias('ca.creation', 'cachedocument_creation') ->selectAlias('ca.creation', 'cachedocument_creation')
->leftJoin( ->leftJoin(
$this->defaultSelectAlias, CoreRequestBuilder::TABLE_CACHE_DOCUMENTS, 'cd', $this->defaultSelectAlias, CoreRequestBuilder::TABLE_CACHE_DOCUMENTS, 'cd',

Wyświetl plik

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

Wyświetl plik

@ -61,6 +61,9 @@ class Document extends ACore implements JsonSerializable {
/** @var bool */ /** @var bool */
private $public = false; private $public = false;
/** @var int */
private $error = 0;
/** /**
* Document constructor. * Document constructor.
@ -150,6 +153,25 @@ class Document extends ACore implements JsonSerializable {
} }
/**
* @return int
*/
public function getError(): int {
return $this->error;
}
/**
* @param int $error
*
* @return Document
*/
public function setError(int $error): Document {
$this->error = $error;
return $this;
}
/** /**
* @return string * @return string
*/ */
@ -192,6 +214,7 @@ class Document extends ACore implements JsonSerializable {
parent::importFromDatabase($data); parent::importFromDatabase($data);
$this->setPublic(($this->getInt('public', $data, 0) === 1) ? true : false); $this->setPublic(($this->getInt('public', $data, 0) === 1) ? true : false);
$this->setError($this->getInt('error', $data, 0));
$this->setLocalCopy($this->get('local_copy', $data, '')); $this->setLocalCopy($this->get('local_copy', $data, ''));
$this->setMediaType($this->get('media_type', $data, '')); $this->setMediaType($this->get('media_type', $data, ''));
$this->setMimeType($this->get('mime_type', $data, '')); $this->setMimeType($this->get('mime_type', $data, ''));

Wyświetl plik

@ -33,6 +33,7 @@ namespace OCA\Social\Service\ActivityPub;
use OCA\Social\Db\CacheDocumentsRequest; use OCA\Social\Db\CacheDocumentsRequest;
use OCA\Social\Exceptions\CacheContentException; use OCA\Social\Exceptions\CacheContentException;
use OCA\Social\Exceptions\CacheContentSizeException;
use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException;
use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Document; use OCA\Social\Model\ActivityPub\Document;
@ -99,6 +100,8 @@ class DocumentService implements ICoreService {
$document->setMimeType($mime); $document->setMimeType($mime);
$document->setLocalCopy($localCopy); $document->setLocalCopy($localCopy);
$this->cacheDocumentsRequest->endCaching($document); $this->cacheDocumentsRequest->endCaching($document);
} catch (CacheContentSizeException $e) {
$this->cacheDocumentsRequest->endCaching($document);
} catch (CacheContentException $e) { } catch (CacheContentException $e) {
} }

Wyświetl plik

@ -32,6 +32,7 @@ namespace OCA\Social\Service;
use Exception; use Exception;
use OCA\Social\Exceptions\CacheContentException; use OCA\Social\Exceptions\CacheContentException;
use OCA\Social\Exceptions\CacheContentSizeException;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
@ -41,9 +42,15 @@ use OCP\Files\SimpleFS\ISimpleFile;
class CacheService { class CacheService {
const ERROR_MAX_SIZE = 1;
/** @var IAppData */ /** @var IAppData */
private $appData; private $appData;
/** @var ConfigService */
private $configService;
/** @var MiscService */ /** @var MiscService */
private $miscService; private $miscService;
@ -52,10 +59,14 @@ class CacheService {
* CacheService constructor. * CacheService constructor.
* *
* @param IAppData $appData * @param IAppData $appData
* @param ConfigService $configService
* @param MiscService $miscService * @param MiscService $miscService
*/ */
public function __construct(IAppData $appData, MiscService $miscService) { public function __construct(
IAppData $appData, ConfigService $configService, MiscService $miscService
) {
$this->appData = $appData; $this->appData = $appData;
$this->configService = $configService;
$this->miscService = $miscService; $this->miscService = $miscService;
} }
@ -68,6 +79,7 @@ class CacheService {
* @return string * @return string
* @throws CacheContentException * @throws CacheContentException
* @throws NotPermittedException * @throws NotPermittedException
* @throws CacheContentSizeException
*/ */
public function saveRemoteFileToCache(string $url, &$mime = '') { public function saveRemoteFileToCache(string $url, &$mime = '') {
@ -131,13 +143,26 @@ class CacheService {
* *
* @return string * @return string
* @throws CacheContentException * @throws CacheContentException
* @throws CacheContentSizeException
*/ */
public function retrieveContent(string $url) { public function retrieveContent(string $url) {
$content = file_get_contents($url); $maxSize =
if ($content === false) { $this->configService->getAppValueInt(ConfigService::SOCIAL_MAX_SIZE) * 1024 * 1024;
$fd = fopen($url, "r");
if ($fd === false) {
throw new CacheContentException(); throw new CacheContentException();
} }
$content = '';
while (!feof($fd)) {
$content .= fread($fd, 4096);
if (strlen($content) > $maxSize) {
throw new CacheContentSizeException();
}
}
fclose($fd);
return $content; return $content;
} }

Wyświetl plik

@ -52,10 +52,12 @@ class ConfigService {
const SOCIAL_ADDRESS = 'address'; const SOCIAL_ADDRESS = 'address';
const SOCIAL_MAX_SIZE = 'max_size';
/** @var array */ /** @var array */
public $defaults = [ public $defaults = [
self::SOCIAL_ADDRESS => '' self::SOCIAL_ADDRESS => '',
self::SOCIAL_MAX_SIZE => 25
]; ];
/** @var string */ /** @var string */
@ -111,6 +113,22 @@ class ConfigService {
return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue); return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
} }
/**
* Get a value by key
*
* @param string $key
*
* @return int
*/
public function getAppValueInt(string $key): int {
$defaultValue = null;
if (array_key_exists($key, $this->defaults)) {
$defaultValue = $this->defaults[$key];
}
return (int)$this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
}
/** /**
* Set a value by key * Set a value by key
* *
@ -248,6 +266,7 @@ class ConfigService {
/** /**
* // TODO - check the Apps folder * // TODO - check the Apps folder
*
* @return string * @return string
* @throws SocialAppConfigException * @throws SocialAppConfigException
*/ */