Skip to content

Commit 41cabac

Browse files
authored
Merge pull request #6348 from nextcloud/backport/6345/stable30
[stable30] fix: Reset session if file rename changes mimetype from/to markdown
2 parents 17bdf3f + e071015 commit 41cabac

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

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
@@ -8,20 +8,23 @@
88

99
namespace OCA\Text\Listeners;
1010

11+
use Exception;
1112
use OCA\Text\Service\AttachmentService;
13+
use OCA\Text\Service\DocumentService;
1214
use OCP\EventDispatcher\Event;
1315
use OCP\EventDispatcher\IEventListener;
1416
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
1517
use OCP\Files\File;
18+
use Psr\Log\LoggerInterface;
1619

1720
/**
1821
* @template-implements IEventListener<Event|BeforeNodeRenamedEvent>
1922
*/
2023
class BeforeNodeRenamedListener implements IEventListener {
21-
private AttachmentService $attachmentService;
22-
23-
public function __construct(AttachmentService $attachmentService) {
24-
$this->attachmentService = $attachmentService;
24+
public function __construct(
25+
private readonly AttachmentService $attachmentService,
26+
private readonly DocumentService $documentService,
27+
private readonly LoggerInterface $logger) {
2528
}
2629

2730
public function handle(Event $event): void {
@@ -30,10 +33,24 @@ public function handle(Event $event): void {
3033
}
3134
$source = $event->getSource();
3235
$target = $event->getTarget();
33-
if ($source instanceof File
34-
&& $source->getMimeType() === 'text/markdown'
35-
&& $target instanceof File
36-
) {
36+
37+
if (!($source instanceof File && $target instanceof File)) {
38+
return;
39+
}
40+
41+
$sourceIsMarkdown = $source->getMimeType() === 'text/markdown';
42+
$targetIsMarkdown = pathinfo($target->getPath(), PATHINFO_EXTENSION) === 'md'; // NonExistingFile has no `getMimetype()`
43+
44+
// Reset document state if mimetype changes from/to markdown as this means another editor is loaded
45+
if ($sourceIsMarkdown xor $targetIsMarkdown) {
46+
try {
47+
$this->documentService->resetDocument($source->getId(), true);
48+
} catch (Exception $e) {
49+
$this->logger->error($e->getMessage(), ['exception' => $e]);
50+
}
51+
}
52+
53+
if ($sourceIsMarkdown) {
3754
$this->attachmentService->moveAttachments($source, $target);
3855
}
3956
}

0 commit comments

Comments
 (0)