|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Text\Context; |
| 9 | + |
| 10 | +use OCA\Text\Db\Document; |
| 11 | +use OCA\Text\Service\FileService; |
| 12 | +use OCA\Text\Service\LockService; |
| 13 | +use OCP\Files\File; |
| 14 | +use OCP\Files\Lock\ILock; |
| 15 | +use OCP\IL10N; |
| 16 | +use OCP\IUser; |
| 17 | + |
| 18 | +class FileContext implements IContext { |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + private readonly FileService $fileService, |
| 22 | + private readonly IL10N $l10n, |
| 23 | + private readonly LockService $lockService, |
| 24 | + private readonly File $file, |
| 25 | + private readonly ?string $baseVersionEtag, |
| 26 | + private readonly ?string $token = null, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + public function check(): ?string { |
| 31 | + // Block using text for disabled download internal shares |
| 32 | + if ($this->fileService->isDownloadDisabled($this->file)) { |
| 33 | + return $this->l10n->t('This file cannot be displayed as download is disabled by the share'); |
| 34 | + } |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + public function checkDocument(Document $document): ?string { |
| 39 | + if ($this->baseVersionEtag !== null && $this->baseVersionEtag !== $document->getBaseVersionEtag()) { |
| 40 | + return $this->l10n->t('Editing session has expired. Please reload the page.'); |
| 41 | + } |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + public function isReadOnly(): bool { |
| 46 | + return $this->fileService->isReadOnly($this->file, $this->token); |
| 47 | + } |
| 48 | + |
| 49 | + public function getId(): int { |
| 50 | + return $this->file->getId(); |
| 51 | + } |
| 52 | + |
| 53 | + public function getType(): string { |
| 54 | + return 'file'; |
| 55 | + } |
| 56 | + |
| 57 | + public function toString(): string { |
| 58 | + return $this->getType() . ' (' . $this->getId() . ')'; |
| 59 | + } |
| 60 | + |
| 61 | + public function loadContent(): ?string { |
| 62 | + return $this->fileService->loadContent($this->file); |
| 63 | + } |
| 64 | + |
| 65 | + public function getLockInfo(): ?ILock { |
| 66 | + return $this->lockService->getLockByOthers($this->file); |
| 67 | + } |
| 68 | + |
| 69 | + public function getOwner(): ?IUser { |
| 70 | + return $this->file->getOwner(); |
| 71 | + } |
| 72 | + |
| 73 | + public function lock(): bool { |
| 74 | + // Disable file locking for Readme.md files, because in the |
| 75 | + // current setup, this makes it almost impossible to delete these files. |
| 76 | + if (strcasecmp($this->file->getName(), 'Readme.md') !== 0) { |
| 77 | + return $this->lockService->lock($this->file); |
| 78 | + } |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + public function createDocument(): Document { |
| 83 | + $document = new Document(); |
| 84 | + $document->setId($this->getId()); |
| 85 | + $document->setLastSavedVersion(0); |
| 86 | + $document->setLastSavedVersionTime($this->file->getMTime()); |
| 87 | + $document->setLastSavedVersionEtag($this->file->getEtag()); |
| 88 | + $document->setChecksum($this->computeChecksum()); |
| 89 | + // This is a new document - so it needs a fresh base version etag. |
| 90 | + $document->setBaseVersionEtag(uniqid()); |
| 91 | + return $document; |
| 92 | + } |
| 93 | + |
| 94 | + public function computeCheckSum(): string { |
| 95 | + return hash('crc32', $this->file->getContent()); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments