migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OC\Collaboration\Reference\File;
use OC\Files\Node\NonExistingFile;
use OC\Files\Node\NonExistingFolder;
use OCP\Collaboration\Reference\IReferenceManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Share\Events\ShareCreatedEvent;
use OCP\Share\Events\ShareDeletedEvent;
/** @template-implements IEventListener<Event|NodeDeletedEvent|ShareDeletedEvent|ShareCreatedEvent> */
class FileReferenceEventListener implements IEventListener {
public function __construct(
private IReferenceManager $manager,
) {
}
public static function register(IEventDispatcher $eventDispatcher): void {
$eventDispatcher->addServiceListener(NodeDeletedEvent::class, FileReferenceEventListener::class);
$eventDispatcher->addServiceListener(ShareDeletedEvent::class, FileReferenceEventListener::class);
$eventDispatcher->addServiceListener(ShareCreatedEvent::class, FileReferenceEventListener::class);
}
/**
* @inheritDoc
*/
public function handle(Event $event): void {
if ($event instanceof NodeDeletedEvent) {
if ($event->getNode() instanceof NonExistingFolder || $event->getNode() instanceof NonExistingFile) {
return;
}
$this->manager->invalidateCache((string)$event->getNode()->getId());
}
if ($event instanceof ShareDeletedEvent) {
$this->manager->invalidateCache((string)$event->getShare()->getNodeId());
}
if ($event instanceof ShareCreatedEvent) {
$this->manager->invalidateCache((string)$event->getShare()->getNodeId());
}
}
}
@@ -0,0 +1,181 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OC\Collaboration\Reference\File;
use OC\User\NoUserException;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\Reference;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IL10N;
use OCP\IPreview;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class FileReferenceProvider extends ADiscoverableReferenceProvider {
private ?string $userId;
private IL10N $l10n;
public function __construct(
private IURLGenerator $urlGenerator,
private IRootFolder $rootFolder,
IUserSession $userSession,
private IMimeTypeDetector $mimeTypeDetector,
private IPreview $previewManager,
IFactory $l10n,
) {
$this->userId = $userSession->getUser()?->getUID();
$this->l10n = $l10n->get('files');
}
public function matchReference(string $referenceText): bool {
return $this->getFilesAppLinkId($referenceText) !== null;
}
private function getFilesAppLinkId(string $referenceText): ?int {
$start = $this->urlGenerator->getAbsoluteURL('/apps/files/');
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/files/');
$fileId = null;
if (mb_strpos($referenceText, $start) === 0) {
$parts = parse_url($referenceText);
parse_str($parts['query'] ?? '', $query);
$fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId;
$fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId;
}
if (mb_strpos($referenceText, $startIndex) === 0) {
$parts = parse_url($referenceText);
parse_str($parts['query'] ?? '', $query);
$fileId = isset($query['fileid']) ? (int)$query['fileid'] : $fileId;
$fileId = isset($query['openfile']) ? (int)$query['openfile'] : $fileId;
}
if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/index.php/f/')) === 0) {
$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/index.php/f/'), '', $referenceText);
}
if (mb_strpos($referenceText, $this->urlGenerator->getAbsoluteURL('/f/')) === 0) {
$fileId = str_replace($this->urlGenerator->getAbsoluteURL('/f/'), '', $referenceText);
}
return $fileId !== null ? (int)$fileId : null;
}
public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$reference = new Reference($referenceText);
try {
$this->fetchReference($reference);
} catch (NotFoundException $e) {
$reference->setRichObject('file', null);
$reference->setAccessible(false);
}
return $reference;
}
return null;
}
/**
* @throws NotFoundException
*/
private function fetchReference(Reference $reference): void {
if ($this->userId === null) {
throw new NotFoundException();
}
$fileId = $this->getFilesAppLinkId($reference->getId());
if ($fileId === null) {
throw new NotFoundException();
}
try {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$files = $userFolder->getById($fileId);
if (empty($files)) {
throw new NotFoundException();
}
/** @var Node $file */
$file = array_shift($files);
$reference->setTitle($file->getName());
$reference->setDescription($file->getMimetype());
$reference->setUrl($this->urlGenerator->getAbsoluteURL('/index.php/f/' . $fileId));
if ($this->previewManager->isMimeSupported($file->getMimeType())) {
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 1600, 'y' => 630, 'fileId' => $fileId]));
} else {
$fileTypeIconUrl = $this->mimeTypeDetector->mimeTypeIcon($file->getMimeType());
$reference->setImageUrl($fileTypeIconUrl);
}
$reference->setRichObject('file', [
'id' => $file->getId(),
'name' => $file->getName(),
'size' => $file->getSize(),
'path' => $userFolder->getRelativePath($file->getPath()),
'link' => $reference->getUrl(),
'mimetype' => $file->getMimetype(),
'mtime' => $file->getMTime(),
'preview-available' => $this->previewManager->isAvailable($file)
]);
} catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) {
throw new NotFoundException();
}
}
public function getCachePrefix(string $referenceId): string {
return (string)$this->getFilesAppLinkId($referenceId);
}
public function getCacheKey(string $referenceId): ?string {
return $this->userId ?? '';
}
public function getId(): string {
return 'files';
}
public function getTitle(): string {
return $this->l10n->t('Files');
}
public function getOrder(): int {
return 0;
}
public function getIconUrl(): string {
return $this->urlGenerator->imagePath('files', 'folder.svg');
}
}
@@ -0,0 +1,183 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OC\Collaboration\Reference;
use Fusonic\OpenGraph\Consumer;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\LimitStream;
use GuzzleHttp\Psr7\Utils;
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OC\SystemConfig;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\IReferenceProvider;
use OCP\Collaboration\Reference\Reference;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\NotFoundException;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
class LinkReferenceProvider implements IReferenceProvider {
public const MAX_PREVIEW_SIZE = 1024 * 1024;
public const ALLOWED_CONTENT_TYPES = [
'image/png',
'image/jpg',
'image/jpeg',
'image/gif',
'image/svg+xml',
'image/webp'
];
public function __construct(
private IClientService $clientService,
private LoggerInterface $logger,
private SystemConfig $systemConfig,
private IAppDataFactory $appDataFactory,
private IURLGenerator $urlGenerator,
private Limiter $limiter,
private IUserSession $userSession,
private IRequest $request,
) {
}
public function matchReference(string $referenceText): bool {
if ($this->systemConfig->getValue('reference_opengraph', true) !== true) {
return false;
}
return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText);
}
public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$reference = new Reference($referenceText);
$this->fetchReference($reference);
return $reference;
}
return null;
}
private function fetchReference(Reference $reference): void {
try {
$user = $this->userSession->getUser();
if ($user) {
$this->limiter->registerUserRequest('opengraph', 10, 120, $user);
} else {
$this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress());
}
} catch (RateLimitExceededException $e) {
return;
}
$client = $this->clientService->newClient();
try {
$headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]);
} catch (\Exception $e) {
$this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]);
return;
}
$linkContentLength = $headResponse->getHeader('Content-Length');
if (is_numeric($linkContentLength) && (int) $linkContentLength > 5 * 1024 * 1024) {
$this->logger->debug('Skip resolving links pointing to content length > 5 MB');
return;
}
$linkContentType = $headResponse->getHeader('Content-Type');
$expectedContentType = 'text/html';
$suffixedExpectedContentType = $expectedContentType . ';';
$startsWithSuffixed = str_starts_with($linkContentType, $suffixedExpectedContentType);
// check the header begins with the expected content type
if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) {
$this->logger->debug('Skip resolving links pointing to content type that is not "text/html"');
return;
}
try {
$response = $client->get($reference->getId(), [ 'timeout' => 10 ]);
} catch (\Exception $e) {
$this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]);
return;
}
$responseBody = (string)$response->getBody();
// OpenGraph handling
$consumer = new Consumer();
$consumer->useFallbackMode = true;
$object = $consumer->loadHtml($responseBody);
$reference->setUrl($reference->getId());
if ($object->title) {
$reference->setTitle($object->title);
}
if ($object->description) {
$reference->setDescription($object->description);
}
if ($object->images) {
try {
$host = parse_url($object->images[0]->url, PHP_URL_HOST);
if ($host === false || $host === null) {
$this->logger->warning('Could not detect host of open graph image URI for ' . $reference->getId());
} else {
$appData = $this->appDataFactory->get('core');
try {
$folder = $appData->getFolder('opengraph');
} catch (NotFoundException $e) {
$folder = $appData->newFolder('opengraph');
}
$response = $client->get($object->images[0]->url, ['timeout' => 10]);
$contentType = $response->getHeader('Content-Type');
$contentLength = $response->getHeader('Content-Length');
if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) {
$stream = Utils::streamFor($response->getBody());
$bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0);
$reference->setImageContentType($contentType);
$folder->newFile(md5($reference->getId()), $bodyStream->getContents());
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
}
}
} catch (GuzzleException $e) {
$this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
} catch (\Throwable $e) {
$this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
}
}
}
public function getCachePrefix(string $referenceId): string {
return $referenceId;
}
public function getCacheKey(string $referenceId): ?string {
return null;
}
}
@@ -0,0 +1,255 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OC\Collaboration\Reference;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\Collaboration\Reference\File\FileReferenceProvider;
use OCP\Collaboration\Reference\IDiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\IReferenceManager;
use OCP\Collaboration\Reference\IReferenceProvider;
use OCP\Collaboration\Reference\Reference;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Throwable;
class ReferenceManager implements IReferenceManager {
public const CACHE_TTL = 3600;
/** @var IReferenceProvider[]|null */
private ?array $providers = null;
private ICache $cache;
public function __construct(
private LinkReferenceProvider $linkReferenceProvider,
ICacheFactory $cacheFactory,
private Coordinator $coordinator,
private ContainerInterface $container,
private LoggerInterface $logger,
private IConfig $config,
private IUserSession $userSession,
) {
$this->cache = $cacheFactory->createDistributed('reference');
}
/**
* Extract a list of URLs from a text
*
* @return string[]
*/
public function extractReferences(string $text): array {
preg_match_all(IURLGenerator::URL_REGEX, $text, $matches);
$references = $matches[0] ?? [];
return array_map(function ($reference) {
return trim($reference);
}, $references);
}
/**
* Try to get a cached reference object from a reference string
*/
public function getReferenceFromCache(string $referenceId): ?IReference {
$matchedProvider = $this->getMatchedProvider($referenceId);
if ($matchedProvider === null) {
return null;
}
$cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId);
return $this->getReferenceByCacheKey($cacheKey);
}
/**
* Try to get a cached reference object from a full cache key
*/
public function getReferenceByCacheKey(string $cacheKey): ?IReference {
$cached = $this->cache->get($cacheKey);
if ($cached) {
return Reference::fromCache($cached);
}
return null;
}
/**
* Get a reference object from a reference string with a matching provider
* Use a cached reference if possible
*/
public function resolveReference(string $referenceId): ?IReference {
$matchedProvider = $this->getMatchedProvider($referenceId);
if ($matchedProvider === null) {
return null;
}
$cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId);
$cached = $this->cache->get($cacheKey);
if ($cached) {
return Reference::fromCache($cached);
}
$reference = $matchedProvider->resolveReference($referenceId);
if ($reference) {
$this->cache->set($cacheKey, Reference::toCache($reference), self::CACHE_TTL);
return $reference;
}
return null;
}
/**
* Try to match a reference string with all the registered providers
* Fallback to the link reference provider (using OpenGraph)
*
* @return IReferenceProvider|null the first matching provider
*/
private function getMatchedProvider(string $referenceId): ?IReferenceProvider {
$matchedProvider = null;
foreach ($this->getProviders() as $provider) {
$matchedProvider = $provider->matchReference($referenceId) ? $provider : null;
if ($matchedProvider !== null) {
break;
}
}
if ($matchedProvider === null && $this->linkReferenceProvider->matchReference($referenceId)) {
$matchedProvider = $this->linkReferenceProvider;
}
return $matchedProvider;
}
/**
* Get a hashed full cache key from a key and prefix given by a provider
*/
private function getFullCacheKey(IReferenceProvider $provider, string $referenceId): string {
$cacheKey = $provider->getCacheKey($referenceId);
return md5($provider->getCachePrefix($referenceId)) . (
$cacheKey !== null ? ('-' . md5($cacheKey)) : ''
);
}
/**
* Remove a specific cache entry from its key+prefix
*/
public function invalidateCache(string $cachePrefix, ?string $cacheKey = null): void {
if ($cacheKey === null) {
$this->cache->clear(md5($cachePrefix));
return;
}
$this->cache->remove(md5($cachePrefix) . '-' . md5($cacheKey));
}
/**
* @return IReferenceProvider[]
*/
public function getProviders(): array {
if ($this->providers === null) {
$context = $this->coordinator->getRegistrationContext();
if ($context === null) {
return [];
}
$this->providers = array_filter(array_map(function ($registration): ?IReferenceProvider {
try {
/** @var IReferenceProvider $provider */
$provider = $this->container->get($registration->getService());
} catch (Throwable $e) {
$this->logger->error('Could not load reference provider ' . $registration->getService() . ': ' . $e->getMessage(), [
'exception' => $e,
]);
return null;
}
return $provider;
}, $context->getReferenceProviders()));
$this->providers[] = $this->container->get(FileReferenceProvider::class);
}
return $this->providers;
}
/**
* @inheritDoc
*/
public function getDiscoverableProviders(): array {
// preserve 0 based index to avoid returning an object in data responses
return array_values(
array_filter($this->getProviders(), static function (IReferenceProvider $provider) {
return $provider instanceof IDiscoverableReferenceProvider;
})
);
}
/**
* @inheritDoc
*/
public function touchProvider(string $userId, string $providerId, ?int $timestamp = null): bool {
$providers = $this->getDiscoverableProviders();
$matchingProviders = array_filter($providers, static function (IDiscoverableReferenceProvider $provider) use ($providerId) {
return $provider->getId() === $providerId;
});
if (!empty($matchingProviders)) {
if ($timestamp === null) {
$timestamp = time();
}
$configKey = 'provider-last-use_' . $providerId;
$this->config->setUserValue($userId, 'references', $configKey, (string) $timestamp);
return true;
}
return false;
}
/**
* @inheritDoc
*/
public function getUserProviderTimestamps(): array {
$user = $this->userSession->getUser();
if ($user === null) {
return [];
}
$userId = $user->getUID();
$keys = $this->config->getUserKeys($userId, 'references');
$prefix = 'provider-last-use_';
$keys = array_filter($keys, static function (string $key) use ($prefix) {
return str_starts_with($key, $prefix);
});
$timestamps = [];
foreach ($keys as $key) {
$providerId = substr($key, strlen($prefix));
$timestamp = (int) $this->config->getUserValue($userId, 'references', $key);
$timestamps[$providerId] = $timestamp;
}
return $timestamps;
}
}
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Julien Veyssier <eneiluj@posteo.net>
*
* @author Julien Veyssier <eneiluj@posteo.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OC\Collaboration\Reference;
use OCP\Collaboration\Reference\IDiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IReferenceManager;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\IInitialStateService;
/** @template-implements IEventListener<Event|RenderReferenceEvent> */
class RenderReferenceEventListener implements IEventListener {
public function __construct(
private IReferenceManager $manager,
private IInitialStateService $initialStateService,
) {
}
public static function register(IEventDispatcher $eventDispatcher): void {
$eventDispatcher->addServiceListener(RenderReferenceEvent::class, RenderReferenceEventListener::class);
}
/**
* @inheritDoc
*/
public function handle(Event $event): void {
if (!($event instanceof RenderReferenceEvent)) {
return;
}
$providers = $this->manager->getDiscoverableProviders();
$jsonProviders = array_map(static function (IDiscoverableReferenceProvider $provider) {
return $provider->jsonSerialize();
}, $providers);
$this->initialStateService->provideInitialState('core', 'reference-provider-list', $jsonProviders);
$timestamps = $this->manager->getUserProviderTimestamps();
$this->initialStateService->provideInitialState('core', 'reference-provider-timestamps', $timestamps);
}
}