Skip to content

Commit 518d031

Browse files
committed
fix(api): Unlock the file via the context
Signed-off-by: Max <max@nextcloud.com>
1 parent 1d77504 commit 518d031

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

lib/Context/FileContext.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ public function saveWithLock(string $content, callable $doWhileLocked): void {
151151
});
152152
}
153153

154+
#[Override]
155+
public function cleanup(): void
156+
{
157+
$this->unlock();
158+
}
159+
154160
private function computeCheckSum(?string $content = null): string {
155161
if ($content === null) {
156162
$content = $this->file->getContent();
@@ -175,4 +181,12 @@ private function lock(): bool {
175181
return true;
176182
}
177183

184+
private function unlock(): void {
185+
// Disable file locking for Readme.md files, because in the
186+
// current setup, this makes it almost impossible to delete these files.
187+
if (strcasecmp($this->file->getName(), 'Readme.md') !== 0) {
188+
$this->lockService->unlock($this->file);
189+
}
190+
}
191+
178192
}

lib/Context/IContext.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function updateDocument(Document $document): ?Document;
2323
public function getFile(): ?File;
2424
public function loadContent(): ?string;
2525
public function saveWithLock(string $content, callable $doWhileLocked): void;
26+
/**
27+
* This will be called when the last active editing session ends.
28+
*/
29+
public function cleanup(): void;
2630
}
2731

2832
readonly class DocumentData {

lib/Controller/PublicSessionController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public function create(string $token, ?string $filePath = null, ?string $baseVer
8585
#[NoAdminRequired]
8686
#[PublicPage]
8787
public function close(int $documentId, int $sessionId, string $sessionToken, string $token): DataResponse {
88-
$file = $this->fileService->getFileByIdFromShare($documentId, $token);
89-
return $this->apiService->close($documentId, $sessionId, $sessionToken, $file);
88+
return $this->apiService->close($documentId, $sessionId, $sessionToken);
9089
}
9190

9291
#[NoAdminRequired]

lib/Controller/SessionController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public function close(int $documentId, int $sessionId, string $sessionToken): Da
7474
if ($userId === null) {
7575
throw new InvalidSessionException();
7676
}
77-
$file = $this->fileService->getFileById($documentId, $userId);
78-
return $this->apiService->close($documentId, $sessionId, $sessionToken, $file);
77+
return $this->apiService->close($documentId, $sessionId, $sessionToken);
7978
}
8079

8180
#[NoAdminRequired]

lib/Service/ApiService.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ public function create(IContext $context, ?string $baseVersionEtag, ?string $gue
7676
);
7777
}
7878

79-
public function close(int $documentId, int $sessionId, string $sessionToken, File $file): DataResponse {
79+
public function close(int $documentId, int $sessionId, string $sessionToken): DataResponse {
8080
$this->sessionService->closeSession($documentId, $sessionId, $sessionToken);
8181
$this->sessionService->removeInactiveSessionsWithoutSteps($documentId);
8282
$activeSessions = $this->sessionService->getActiveSessions($documentId);
8383
if (count($activeSessions) === 0) {
84-
$this->lockService->unlock($file);
84+
$document = $this->documentService->getDocument($documentId);
85+
$type = $document->getContextType();
86+
$id = $document->getContextId();
87+
$context = $this->contextManager->getContext($type, $id, $sessionToken);
88+
$context->cleanup();
8589
}
8690
return new DataResponse([]);
8791
}

0 commit comments

Comments
 (0)