Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable30] Cache extractFields response from Collabora #3932

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCP\Files\Template\Field;
use OCP\Files\Template\FieldType;
use OCP\Http\Client\IClientService;
use OCP\ICacheFactory;
use Psr\Log\LoggerInterface;

class TemplateFieldService {
Expand All @@ -21,7 +22,8 @@ public function __construct(
private CapabilitiesService $capabilitiesService,
private AppConfig $appConfig,
private IRootFolder $rootFolder,
private LoggerInterface $logger
private LoggerInterface $logger,
private ICacheFactory $cacheFactory
) {
}

Expand All @@ -38,18 +40,26 @@ public function extractFields(Node|int $file) {
$file = $this->rootFolder->getFirstNodeById($file);
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();
try {
$localCache = $this->cacheFactory->createLocal('richdocuments_templates/');
$cacheName = $file->getId() . "/" . $file->getEtag();
$cachedResponse = $localCache->get($cacheName);

if ($cachedResponse !== null) {
return $cachedResponse;
}

$form = RemoteOptionsService::getDefaultOptions();
$form['query'] = ['limit' => 'content-control'];
$form['multipart'] = [[
'name' => 'data',
'contents' => $file->getStorage()->fopen($file->getInternalPath(), 'r'),
'headers' => ['Content-Type' => 'multipart/form-data'],
]];
$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();

$form = RemoteOptionsService::getDefaultOptions();
$form['query'] = ['limit' => 'content-control'];
$form['multipart'] = [[
'name' => 'data',
'contents' => $file->getStorage()->fopen($file->getInternalPath(), 'r'),
'headers' => ['Content-Type' => 'multipart/form-data'],
]];

try {
$response = $httpClient->post(
$collaboraUrl . "/cool/extract-document-structure",
$form
Expand All @@ -76,7 +86,10 @@ public function extractFields(Node|int $file) {
];
}

return array_merge([], ...$fields);
$fields = array_merge([], ...$fields);
$localCache->set($cacheName, $fields, 3600);

return $fields;
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return [];
Expand Down
Loading