Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/53/head
Maxence Lange 2018-11-24 13:15:26 -01:00 zatwierdzone przez Julius Härtl
rodzic 832302d678
commit 90a5a13dfb
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4C614C6ED2CDE6DF
6 zmienionych plików z 206 dodań i 136 usunięć

Wyświetl plik

@ -32,8 +32,6 @@ namespace OCA\Social\Db;
use DateTime;
use OCA\Social\Exceptions\CacheDocumentDoesNotExistException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Model\ActivityPub\Document;
use OCP\DB\QueryBuilder\IQueryBuilder;
@ -50,7 +48,9 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder {
$qb->setValue('id', $qb->createNamedParameter($document->getId()))
->setValue('type', $qb->createNamedParameter($document->getType()))
->setValue('url', $qb->createNamedParameter($document->getUrl()))
->setValue('media_type', $qb->createNamedParameter($document->getMediaType()))
->setValue('local_copy', $qb->createNamedParameter($document->getLocalCopy()))
->setValue('public', $qb->createNamedParameter(($document->isPublic()) ? '1' : '0'))
->setValue(
'creation',
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
@ -59,18 +59,69 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder {
}
/**
* @param Document $document
*/
public function initCaching(Document $document) {
$qb = $this->getCacheDocumentsUpdateSql();
$this->limitToIdString($qb, $document->getId());
$qb->set(
'caching', $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
);
$qb->execute();
}
/**
* @param Document $document
*/
public function endCaching(Document $document) {
$qb = $this->getCacheDocumentsUpdateSql();
$this->limitToIdString($qb, $document->getId());
$qb->set('local_copy', $qb->createNamedParameter($document->getLocalCopy()));
$qb->execute();
}
/**
* @param string $url
*
* @return Document
* @throws CacheDocumentDoesNotExistException
* @throws SocialAppConfigException
* @throws UrlCloudException
*/
public function getFromSource(string $url) {
public function getBySource(string $url) {
$qb = $this->getCacheDocumentsSelectSql();
$this->limitToUrl($qb, $url);
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
throw new CacheDocumentDoesNotExistException();
}
return $this->parseCacheDocumentsSelectSql($data);
}
/**
* @param string $id
*
* @param bool $public
*
* @return Document
* @throws CacheDocumentDoesNotExistException
*/
public function getById(string $id, bool $public = false) {
$qb = $this->getCacheDocumentsSelectSql();
$this->limitToIdString($qb, $id);
if ($public === true) {
$this->limitToPublic($qb);
}
$cursor = $qb->execute();
$data = $cursor->fetch();

Wyświetl plik

@ -79,7 +79,7 @@ class CacheDocumentsRequestBuilder extends CoreRequestBuilder {
/** @noinspection PhpMethodParametersCountMismatchInspection */
$qb->select(
'cd.id', 'cd.type', 'cd.media_type', 'cd.mime_type', 'cd.url', 'cd.local_copy',
'cd.creation', 'cd.caching'
'cd.public', 'cd.creation', 'cd.caching'
)
->from(self::TABLE_CACHE_DOCUMENTS, 'cd');
@ -106,12 +106,9 @@ class CacheDocumentsRequestBuilder extends CoreRequestBuilder {
* @param array $data
*
* @return Document
* @throws UrlCloudException
* @throws SocialAppConfigException
*/
protected function parseCacheDocumentsSelectSql(array $data): Document {
$document = new Document();
$document->setUrlCloud($this->configService->getCloudAddress());
$document->importFromDatabase($data);
return $document;

Wyświetl plik

@ -33,7 +33,6 @@ namespace OCA\Social\Db;
use Doctrine\DBAL\Query\QueryBuilder;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Model\ActivityPub\Document;
use OCA\Social\Model\ActivityPub\Image;
use OCA\Social\Model\ActivityPub\Person;
@ -146,6 +145,17 @@ class CoreRequestBuilder {
}
/**
* Limit the request to the ActorId
*
* @param IQueryBuilder $qb
*/
protected function limitToPublic(IQueryBuilder &$qb) {
$this->limitToDBFieldInt($qb, 'public', 1);
}
/**
* Limit the request to the ActorId
*

Wyświetl plik

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

Wyświetl plik

@ -27,20 +27,31 @@ declare(strict_types=1);
*
*/
namespace OCA\Social\Service\ActivityPub;
use OCA\Social\Db\CacheDocumentsRequest;
use OCA\Social\Exceptions\CacheContentException;
use OCA\Social\Exceptions\CacheDocumentDoesNotExistException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Document;
use OCA\Social\Service\CacheService;
use OCA\Social\Service\ICoreService;
use OCA\Social\Service\MiscService;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
class DocumentService implements ICoreService {
/** @var CacheDocumentsRequest */
private $cacheDocumentsRequest;
/** @var CacheService */
private $cacheService;
/** @var MiscService */
private $miscService;
@ -49,18 +60,65 @@ class DocumentService implements ICoreService {
* DocumentService constructor.
*
* @param CacheDocumentsRequest $cacheDocumentsRequest
* @param CacheService $cacheService
* @param MiscService $miscService
*/
public function __construct(
CacheDocumentsRequest $cacheDocumentsRequest, MiscService $miscService
CacheDocumentsRequest $cacheDocumentsRequest, CacheService $cacheService,
MiscService $miscService
) {
$this->cacheDocumentsRequest = $cacheDocumentsRequest;
$this->cacheService = $cacheService;
$this->miscService = $miscService;
}
public function getFromCache(array $documents) {
/**
* @param string $id
*
* @return Document
* @throws CacheDocumentDoesNotExistException
* @throws NotPermittedException
*/
public function cacheRemoteDocument(string $id) {
$document = $this->cacheDocumentsRequest->getById($id);
if ($document->getLocalCopy() !== '') {
return $document;
}
// TODO - check the size of the attachment, also to stop download after a certain size of content.
// TODO - ignore this is getCaching is older than 15 minutes
if ($document->getCaching() !== '') {
return $document;
}
$this->cacheDocumentsRequest->initCaching($document);
try {
$localCopy = $this->cacheService->saveRemoteFileToCache($document->getUrl(), $mime);
$document->setMimeType($mime);
$document->setLocalCopy($localCopy);
$this->cacheDocumentsRequest->endCaching($document);
} catch (CacheContentException $e) {
}
return $document;
}
/**
* @param string $id
*
* @param bool $public
*
* @return ISimpleFile
* @throws CacheContentException
* @throws CacheDocumentDoesNotExistException
*/
public function getFromCache(string $id, bool $public = false) {
$document = $this->cacheDocumentsRequest->getById($id, $public);
return $this->cacheService->getContentFromCache($document->getLocalCopy());
}

Wyświetl plik

@ -30,169 +30,115 @@ declare(strict_types=1);
namespace OCA\Social\Service;
use daita\MySmallPhpTools\Model\Request;
use OCA\Social\Exceptions\RequestException;
use Exception;
use OCA\Social\Exceptions\CacheContentException;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
class CurlService {
class CacheService {
/** @var IAppData */
private $appData;
/** @var MiscService */
private $miscService;
/**
* CurlService constructor.
* CacheService constructor.
*
* @param IAppData $appData
* @param MiscService $miscService
*/
public function __construct(MiscService $miscService) {
public function __construct(IAppData $appData, MiscService $miscService) {
$this->appData = $appData;
$this->miscService = $miscService;
}
/**
* @param Request $request
* @param string $url
*
* @return array
* @throws RequestException
*/
public function request(Request $request): array {
$curl = $this->initRequest($request);
$this->initRequestPost($curl, $request);
$this->initRequestPut($curl, $request);
$this->initRequestDelete($curl, $request);
$result = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->parseRequestResultCode301($code);
// $this->parseRequestResultCode401($code);
$this->parseRequestResultCode404($code, $request);
// $this->parseRequestResultCode503($code);
// $this->parseRequestResultCode500($code);
// $this->parseRequestResult($result);
$ret = json_decode((string)$result, true);
// if ($ret === null) {
// throw new RequestException('500 Internal server error - could not parse JSON response');
// }
if (!is_array($ret)) {
$ret = ['_result' => $result];
}
$ret['_address'] = $request->getAddress();
$ret['_path'] = $request->getUrl();
$ret['_code'] = $code;
return $ret;
}
/**
* @param Request $request
* @param string $mime
*
* @return resource
* @return string
* @throws CacheContentException
* @throws NotPermittedException
*/
private function initRequest(Request $request) {
public function saveRemoteFileToCache(string $url, &$mime = '') {
$curl = $this->generateCurlRequest($request);
$headers = $request->getHeaders();
$filename = sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
// creating a path aa/bb/cc/dd/ from the filename aabbccdd-0123-[...]
$path = chunk_split(substr($filename, 0, 8), 2, '/');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
try {
$folder = $this->appData->getFolder($path);
} catch (NotFoundException $e) {
$folder = $this->appData->newFolder($path);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$content = $this->retrieveContent($url);
return $curl;
// TODO - get mime type in a better way.
// To get the mime type, we create a temp file
$tmpFile = tmpfile();
$tmpPath = stream_get_meta_data($tmpFile)['uri'];
fwrite($tmpFile, $content);
$mime = mime_content_type($tmpPath);
fclose($tmpFile);
$cache = $folder->newFile($filename);
$cache->putContent($content);
return $path . $filename;
}
/**
* @param Request $request
* @param string $path
*
* @return resource
* @return ISimpleFile
* @throws CacheContentException
*/
private function generateCurlRequest(Request $request) {
$url = 'https://' . $request->getAddress() . $request->getParsedUrl();
if ($request->getType() !== Request::TYPE_GET) {
$curl = curl_init($url);
} else {
$curl = curl_init($url . '?' . $request->getUrlData());
}
public function getContentFromCache(string $path) {
return $curl;
$pos = strrpos($path, '/');
$dir = substr($path, 0, $pos);
$filename = substr($path, $pos + 1);
try {
$file = $this->appData->getFolder($dir)
->getFile($filename);
return $file;
} catch (Exception $e) {
throw new CacheContentException();
}
}
/**
* @param resource $curl
* @param Request $request
*/
private function initRequestPost($curl, Request $request) {
if ($request->getType() !== Request::TYPE_POST) {
return;
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody());
}
/**
* @param resource $curl
* @param Request $request
*/
private function initRequestPut($curl, Request $request) {
if ($request->getType() !== Request::TYPE_PUT) {
return;
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody());
}
/**
* @param resource $curl
* @param Request $request
*/
private function initRequestDelete($curl, Request $request) {
if ($request->getType() !== Request::TYPE_DELETE) {
return;
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody());
}
/**
* @param int $code
* @param string $url
*
* @throws RequestException
* @return string
* @throws CacheContentException
*/
private function parseRequestResultCode301($code) {
if ($code === 301) {
throw new RequestException('301 Moved Permanently');
public function retrieveContent(string $url) {
$content = file_get_contents($url);
if ($content === false) {
throw new CacheContentException();
}
}
/**
* @param int $code
*
* @param Request $request
*
* @throws RequestException
*/
private function parseRequestResultCode404(int $code, Request $request) {
if ($code === 404) {
throw new RequestException('404 Not Found - ' . json_encode($request));
}
return $content;
}