summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-02-23 21:27:29 -0100
committerBackportbot <backportbot-noreply@rullzer.com>2019-02-27 15:00:57 +0000
commitb1b0038f7f99cf1012aee3c71b29a4feb50daf4b (patch)
tree48b8326f2c75f442cc0b5c3d0913e5e81416152b
parent0a55a4e3a43c8a5a75cc2b03f8cf1cbc7bdd1130 (diff)
Generating cache of context documents
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r--lib/Model/LinkedDataSignature.php10
-rw-r--r--lib/Service/SignatureService.php65
2 files changed, 73 insertions, 2 deletions
diff --git a/lib/Model/LinkedDataSignature.php b/lib/Model/LinkedDataSignature.php
index abf4406d..03009815 100644
--- a/lib/Model/LinkedDataSignature.php
+++ b/lib/Model/LinkedDataSignature.php
@@ -32,9 +32,12 @@ namespace OCA\Social\Model;
use daita\MySmallPhpTools\Traits\TArrayTools;
+use JsonLdException;
use JsonSerializable;
use OCA\Social\Exceptions\LinkedDataSignatureMissingException;
use OCA\Social\Model\ActivityPub\ACore;
+use OCA\Social\Service\SignatureService;
+use stdClass;
/**
@@ -78,6 +81,7 @@ class LinkedDataSignature implements JsonSerializable {
public function __construct() {
}
+
/**
* @return string
*/
@@ -96,6 +100,7 @@ class LinkedDataSignature implements JsonSerializable {
return $this;
}
+
/**
* @return string
*/
@@ -305,8 +310,9 @@ class LinkedDataSignature implements JsonSerializable {
$res = jsonld_normalize(
$object,
[
- 'algorithm' => 'URDNA2015',
- 'format' => 'application/nquads'
+ 'algorithm' => 'URDNA2015',
+ 'format' => 'application/nquads',
+ 'documentLoader' => [SignatureService::class, 'documentLoader']
]
);
diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php
index b2edfdab..93d57dc1 100644
--- a/lib/Service/SignatureService.php
+++ b/lib/Service/SignatureService.php
@@ -35,6 +35,8 @@ use daita\MySmallPhpTools\Model\Request;
use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime;
use Exception;
+use JsonLdException;
+use OCA\Social\AppInfo\Application;
use OCA\Social\Db\ActorsRequest;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidOriginException;
@@ -53,7 +55,12 @@ use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\LinkedDataSignature;
use OCA\Social\Model\RequestQueue;
+use OCP\Files\IAppData;
+use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
+use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IRequest;
+use stdClass;
class SignatureService {
@@ -412,5 +419,63 @@ class SignatureService {
}
}
+
+ /**
+ * @param string $url
+ *
+ * @return stdClass
+ * @throws JsonLdException
+ * @throws NotPermittedException
+ */
+ public static function documentLoader($url): stdClass {
+ $recursion = 0;
+ $x = debug_backtrace();
+ if ($x) {
+ foreach ($x as $n) {
+ if ($n['function'] === __FUNCTION__) {
+ $recursion++;
+ }
+ }
+ }
+
+ if ($recursion > 5) {
+ exit();
+ }
+
+ $folder = self::getContextCacheFolder();
+ $filename = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
+ $filename = str_replace('/', '.', $filename) . '.json';
+
+ try {
+ $cache = $folder->getFile($filename);
+ $data = json_decode($cache->getContent());
+ } catch (NotFoundException $e) {
+ $cache = $folder->newFile($filename);
+ $data = jsonld_default_document_loader($url);
+ $cache->putContent(json_encode($data));
+ }
+
+ return $data;
+ }
+
+
+ /**
+ * @return ISimpleFolder
+ * @throws NotPermittedException
+ */
+ private static function getContextCacheFolder(): ISimpleFolder {
+ $path = 'context';
+
+ $appData = \OC::$server->getAppDataDir(Application::APP_NAME);
+ try {
+ $folder = $appData->getFolder($path);
+ } catch (NotFoundException $e) {
+ $folder = $appData->newFolder($path);
+ }
+
+ return $folder;
+ }
+
+
}