Skip to content

Commit

Permalink
fix: Reset session if file rename changes mimetype from/to markdown
Browse files Browse the repository at this point in the history
For the event target file, we cannot use `getMimetype()` or
`getExtension()` as it's a node of type `NonExistingFile`.

Fixes: #5736

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- authored and backportbot[bot] committed Sep 11, 2024
1 parent 1b76944 commit 742747b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
51 changes: 51 additions & 0 deletions cypress/e2e/changeMimetype.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { randUser } from '../utils/index.js'

const user = randUser()

describe('Changing mimetype from/to markdown resets document session', function() {
before(function() {
// Init user
cy.createUser(user)
cy.login(user)
cy.uploadFile('empty.md', 'text/markdown', 'test1.md')
cy.uploadFile('empty.txt', 'text/plain', 'test2.txt')
})
beforeEach(function() {
cy.login(user)
cy.visit('/apps/files')
})

it('Rename from md to txt', function() {
cy.openFile('test1.md')
cy.getContent()
.type('## Hello world')
cy.closeFile()

cy.moveFile('test1.md', 'test1.txt')
cy.visit('/apps/files')

cy.openFile('test1.txt')
cy.getContent()
.find('pre')
.should('contain', '## Hello world')
})

it('Rename from txt to md', function() {
cy.openFile('test2.txt')
cy.getContent()
.type('Hello world')
cy.closeFile()

cy.moveFile('test2.txt', 'test2.md')
cy.visit('/apps/files')

cy.openFile('test2.md')
cy.getContent()
.should('contain', 'Hello world')
})
})
33 changes: 25 additions & 8 deletions lib/Listeners/BeforeNodeRenamedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@

namespace OCA\Text\Listeners;

use Exception;
use OCA\Text\Service\AttachmentService;
use OCA\Text\Service\DocumentService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\File;
use Psr\Log\LoggerInterface;

/**
* @template-implements IEventListener<Event|BeforeNodeRenamedEvent>
*/
class BeforeNodeRenamedListener implements IEventListener {
private AttachmentService $attachmentService;

public function __construct(AttachmentService $attachmentService) {
$this->attachmentService = $attachmentService;
public function __construct(
private readonly AttachmentService $attachmentService,

Check failure on line 42 in lib/Listeners/BeforeNodeRenamedListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidDocblock

lib/Listeners/BeforeNodeRenamedListener.php:42:3: InvalidDocblock: Param1 of OCA\Text\Listeners\BeforeNodeRenamedListener::__construct has invalid syntax (see https://psalm.dev/008)

Check failure on line 42 in lib/Listeners/BeforeNodeRenamedListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

ParseError

lib/Listeners/BeforeNodeRenamedListener.php:42:20: ParseError: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 42 (see https://psalm.dev/173)
private readonly DocumentService $documentService,
private readonly LoggerInterface $logger) {
}

public function handle(Event $event): void {
Expand All @@ -47,10 +50,24 @@ public function handle(Event $event): void {
}
$source = $event->getSource();
$target = $event->getTarget();
if ($source instanceof File
&& $source->getMimeType() === 'text/markdown'
&& $target instanceof File
) {

if (!($source instanceof File && $target instanceof File)) {
return;
}

$sourceIsMarkdown = $source->getMimeType() === 'text/markdown';
$targetIsMarkdown = pathinfo($target->getPath(), PATHINFO_EXTENSION) === 'md'; // NonExistingFile has no `getMimetype()`

// Reset document state if mimetype changes from/to markdown as this means another editor is loaded
if ($sourceIsMarkdown xor $targetIsMarkdown) {
try {
$this->documentService->resetDocument($source->getId(), true);

Check failure on line 64 in lib/Listeners/BeforeNodeRenamedListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedThisPropertyFetch

lib/Listeners/BeforeNodeRenamedListener.php:64:5: UndefinedThisPropertyFetch: Instance property OCA\Text\Listeners\BeforeNodeRenamedListener::$documentService is not defined (see https://psalm.dev/041)
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);

Check failure on line 66 in lib/Listeners/BeforeNodeRenamedListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedThisPropertyFetch

lib/Listeners/BeforeNodeRenamedListener.php:66:5: UndefinedThisPropertyFetch: Instance property OCA\Text\Listeners\BeforeNodeRenamedListener::$logger is not defined (see https://psalm.dev/041)
}
}

if ($sourceIsMarkdown) {
$this->attachmentService->moveAttachments($source, $target);

Check failure on line 71 in lib/Listeners/BeforeNodeRenamedListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedThisPropertyFetch

lib/Listeners/BeforeNodeRenamedListener.php:71:4: UndefinedThisPropertyFetch: Instance property OCA\Text\Listeners\BeforeNodeRenamedListener::$attachmentService is not defined (see https://psalm.dev/041)
}
}
Expand Down

0 comments on commit 742747b

Please sign in to comment.