Skip to content

Commit

Permalink
Merge pull request #4464 from codewithvk/private/codewithvk/caching
Browse files Browse the repository at this point in the history
fix: send etag for settings URLs and update etag on file changes
  • Loading branch information
elzody authored Feb 13, 2025
2 parents fc1bbd0 + c9b774d commit 20e836c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public function checkFileInfo(string $fileId, string $access_token): JSONRespons
];

if ($this->capabilitiesService->hasSettingIframeSupport()) {

if (!$isPublic) {
$response['UserSettings'] = $this->generateSettings($userId, 'userconfig');
}
Expand Down Expand Up @@ -993,9 +992,10 @@ private function generateSettingToken(string $userId): string {
private function generateSettings(string $userId, string $type): array {
$nextcloudUrl = $this->appConfig->getNextcloudUrl() ?: trim($this->urlGenerator->getAbsoluteURL(''), '/');
$uri = $nextcloudUrl . '/index.php/apps/richdocuments/wopi/settings' . '?type=' . $type . '&access_token=' . $this->generateSettingToken($userId) . '&fileId=' . '-1';
$etag = $this->settingsService->getFolderEtag($type);
return [
'uri' => $uri,
'stamp' => time()
'stamp' => $etag
];
}
}
48 changes: 42 additions & 6 deletions lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function uploadFile(SettingsUrl $settingsUrl, string $fileData, string $u
$fileName = $settingsUrl->getFileName();
$newFile = $categoryFolder->newFile($fileName, $fileData);
$fileUri = $this->generateFileUri($settingsUrl->getType(), $settingsUrl->getCategory(), $fileName, $userId);
$this->refreshFolderEtag($settingsUrl->getType());

return [
'stamp' => $newFile->getETag(),
Expand Down Expand Up @@ -154,6 +155,15 @@ public function generateIframeToken(string $type, string $userId): array {
throw $e;
}
}

/**
*
* @param string $type
* @return string
*/
public function getFolderEtag($type) : string {
return $this->getTypeFolder($type)->getEtag();
}

/**
* generate setting config
Expand Down Expand Up @@ -210,12 +220,7 @@ private function getAllCategories(string $type): array {
*/
private function getCategoryDirFolderList(string $type) : array {
try {
$instanceId = $this->config->getSystemValue('instanceid', null);
if ($instanceId === null) {
throw new NotFoundException('Instance ID not found');
}
$rootFolder = $this->rootFolder;
$folder = $rootFolder->get('appdata_' . $instanceId . '/richdocuments' . '/' . $type);
$folder = $this->getTypeFolder($type);
if (!$folder instanceof Folder) {
return [];
}
Expand All @@ -225,6 +230,36 @@ private function getCategoryDirFolderList(string $type) : array {
}
}

/**
* extract folder of $type
*
* @param string $type
* @return Folder
*/
private function getTypeFolder($type) {
$instanceId = $this->config->getSystemValue('instanceid', null);
if ($instanceId === null) {
throw new NotFoundException('Instance ID not found');
}
$rootFolder = $this->rootFolder;
try {
$folder = $rootFolder->get('appdata_' . $instanceId . '/richdocuments' . '/' . $type);
} catch (NotFoundException $e) {
$baseFolder = $this->appData->newFolder($type);
$folder = $rootFolder->get('appdata_' . $instanceId . '/richdocuments' . '/' . $type);
}
return $folder;
}

/**
*
* @param string $type
*/
private function refreshFolderEtag($type) {
$folder = $this->getTypeFolder($type);
$folder->getStorage()->getCache()->update($folder->getId(), [ 'etag' => uniqid() ]);
}

/**
* Generate file URL.
*
Expand Down Expand Up @@ -314,6 +349,7 @@ public function deleteSettingsFile(string $type, string $category, string $name,
throw new NotFoundException("File '{$name}' not found in category '{$category}' for type '{$type}'.");
}
$categoryFolder->getFile($name)->delete();
$this->refreshFolderEtag($type);
} catch (NotFoundException $e) {
throw $e;
} catch (NotPermittedException $e) {
Expand Down

0 comments on commit 20e836c

Please sign in to comment.