Skip to content

Commit 742747b

Browse files
mejo-backportbot[bot]
authored andcommitted
fix: Reset session if file rename changes mimetype from/to markdown
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]>
1 parent 1b76944 commit 742747b

2 files changed

Lines changed: 76 additions & 8 deletions

File tree

cypress/e2e/changeMimetype.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { randUser } from '../utils/index.js'
7+
8+
const user = randUser()
9+
10+
describe('Changing mimetype from/to markdown resets document session', function() {
11+
before(function() {
12+
// Init user
13+
cy.createUser(user)
14+
cy.login(user)
15+
cy.uploadFile('empty.md', 'text/markdown', 'test1.md')
16+
cy.uploadFile('empty.txt', 'text/plain', 'test2.txt')
17+
})
18+
beforeEach(function() {
19+
cy.login(user)
20+
cy.visit('/apps/files')
21+
})
22+
23+
it('Rename from md to txt', function() {
24+
cy.openFile('test1.md')
25+
cy.getContent()
26+
.type('## Hello world')
27+
cy.closeFile()
28+
29+
cy.moveFile('test1.md', 'test1.txt')
30+
cy.visit('/apps/files')
31+
32+
cy.openFile('test1.txt')
33+
cy.getContent()
34+
.find('pre')
35+
.should('contain', '## Hello world')
36+
})
37+
38+
it('Rename from txt to md', function() {
39+
cy.openFile('test2.txt')
40+
cy.getContent()
41+
.type('Hello world')
42+
cy.closeFile()
43+
44+
cy.moveFile('test2.txt', 'test2.md')
45+
cy.visit('/apps/files')
46+
47+
cy.openFile('test2.md')
48+
cy.getContent()
49+
.should('contain', 'Hello world')
50+
})
51+
})

lib/Listeners/BeforeNodeRenamedListener.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,23 @@
2525

2626
namespace OCA\Text\Listeners;
2727

28+
use Exception;
2829
use OCA\Text\Service\AttachmentService;
30+
use OCA\Text\Service\DocumentService;
2931
use OCP\EventDispatcher\Event;
3032
use OCP\EventDispatcher\IEventListener;
3133
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
3234
use OCP\Files\File;
35+
use Psr\Log\LoggerInterface;
3336

3437
/**
3538
* @template-implements IEventListener<Event|BeforeNodeRenamedEvent>
3639
*/
3740
class BeforeNodeRenamedListener implements IEventListener {
38-
private AttachmentService $attachmentService;
39-
40-
public function __construct(AttachmentService $attachmentService) {
41-
$this->attachmentService = $attachmentService;
41+
public function __construct(
42+
private readonly AttachmentService $attachmentService,
43+
private readonly DocumentService $documentService,
44+
private readonly LoggerInterface $logger) {
4245
}
4346

4447
public function handle(Event $event): void {
@@ -47,10 +50,24 @@ public function handle(Event $event): void {
4750
}
4851
$source = $event->getSource();
4952
$target = $event->getTarget();
50-
if ($source instanceof File
51-
&& $source->getMimeType() === 'text/markdown'
52-
&& $target instanceof File
53-
) {
53+
54+
if (!($source instanceof File && $target instanceof File)) {
55+
return;
56+
}
57+
58+
$sourceIsMarkdown = $source->getMimeType() === 'text/markdown';
59+
$targetIsMarkdown = pathinfo($target->getPath(), PATHINFO_EXTENSION) === 'md'; // NonExistingFile has no `getMimetype()`
60+
61+
// Reset document state if mimetype changes from/to markdown as this means another editor is loaded
62+
if ($sourceIsMarkdown xor $targetIsMarkdown) {
63+
try {
64+
$this->documentService->resetDocument($source->getId(), true);
65+
} catch (Exception $e) {
66+
$this->logger->error($e->getMessage(), ['exception' => $e]);
67+
}
68+
}
69+
70+
if ($sourceIsMarkdown) {
5471
$this->attachmentService->moveAttachments($source, $target);
5572
}
5673
}

0 commit comments

Comments
 (0)