Skip to content
Prev Previous commit
Next Next commit
fix: Avoid writing file content if it has not changed
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and mejo- committed Mar 29, 2023
commit dd2f7c5c946bb404ad6b22586c40676499b0b9f7
24 changes: 18 additions & 6 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function getSteps($documentId, $lastVersion): array {
* @throws NotPermittedException
* @throws Exception
*/
public function autosave(?File $file, int $documentId, int $version, ?string $autoaveDocument, ?string $documentState, bool $force = false, bool $manualSave = false, ?string $shareToken = null, ?string $filePath = null): Document {
public function autosave(?File $file, int $documentId, int $version, ?string $autoSaveDocument, ?string $documentState, bool $force = false, bool $manualSave = false, ?string $shareToken = null, ?string $filePath = null): Document {
/** @var Document $document */
$document = $this->documentMapper->find($documentId);

Expand All @@ -327,7 +327,7 @@ public function autosave(?File $file, int $documentId, int $version, ?string $au
}
}

if ($autoaveDocument === null) {
if ($autoSaveDocument === null) {
return $document;
}
// Do not save if version already saved
Expand All @@ -341,10 +341,10 @@ public function autosave(?File $file, int $documentId, int $version, ?string $au
return $document;
}

if (empty($autoaveDocument)) {
if (empty($autoSaveDocument)) {
$this->logger->debug('Saving empty document', [
'requestVersion' => $version,
'requestAutosaveDocument' => $autoaveDocument,
'requestAutosaveDocument' => $autoSaveDocument,
'requestDocumentState' => $documentState,
'document' => $document->jsonSerialize(),
'fileSizeBeforeSave' => $file->getSize(),
Expand All @@ -357,14 +357,26 @@ public function autosave(?File $file, int $documentId, int $version, ?string $au
]);
}

// Version changed but the content remains the same
if ($autoSaveDocument === $file->getContent()) {
if ($documentState) {
$this->writeDocumentState($file->getId(), $documentState);
}
$document->setLastSavedVersion($stepsVersion);
$document->setLastSavedVersionTime($file->getMTime());
$document->setLastSavedVersionEtag($file->getEtag());
$this->documentMapper->update($document);
return $document;
}

$this->cache->set('document-save-lock-' . $documentId, true, 10);
try {
$this->lockManager->runInScope(new LockContext(
$file,
ILock::TYPE_APP,
Application::APP_NAME
), function () use ($file, $autoaveDocument, $documentState) {
$file->putContent($autoaveDocument);
), function () use ($file, $autoSaveDocument, $documentState) {
$file->putContent($autoSaveDocument);
if ($documentState) {
$this->writeDocumentState($file->getId(), $documentState);
}
Expand Down