text icon indicating copy to clipboard operation
text copied to clipboard

Content lost after file renaming from .md to .txt

Open cybermyber opened this issue 1 year ago • 4 comments

Describe the bug If you rename an .md file to a .txt file, the content will be lost after opening it in the built-in text editor. If you download the renamed file before opening it in the built-in text editor, the content is retained.

To Reproduce Steps to reproduce the behavior:

  1. Create or upload a new .md-File with some content
  2. Open the file with the text editor and check the content
  3. Close the file and rename it to example_filename.txt
  4. Open the example_filename.txt-File with the text editor

Expected behavior After renaming from .md to .txt, the former content should be retained after opening with the text editor.

Screenshots Screenshot from 2024-04-30 13-40-26 Screenshot from 2024-04-30 13-40-50

Server details:

  • Nextcloud version: 28.0.2
  • PHP Version: 8.1
  • Database: MariaDB 10.5.23

Client details:

  • OS: Fedora 39
  • Browser: Chrome
  • Browser version: 123.0.6312.122
  • Device: Desktop

cybermyber avatar Apr 30 '24 11:04 cybermyber

I was able to reproduce this. Definitely something we should look into. @juliushaertl @max-nextcloud any idea what is happening here? I would have expected the markdown source to be displayed in the editor instead of all content being deleted :thinking:

mejo- avatar May 14 '24 16:05 mejo-

I suspect the file id stays the same after the rename but the file is now opened with the plaintext editor rather than the markdown editor. At the same time it tries to recover the y.js state from the previous editing session which is invalid in the plaintext editor context.

Nice catch! We did not account for that yet.

max-nextcloud avatar May 15 '24 08:05 max-nextcloud

I see two ways to fix this:

  1. Clear the document state and all related database entries when a rename changes the mime type between markdown and something else.
  2. add a mimetype or editor type to the key for finding the document state for a given document
  3. both

max-nextcloud avatar May 15 '24 08:05 max-nextcloud

I think the cleanest solution would be to reset the document session when the file is renamed and this changes the mimetype.

Unfortunately the BeforeNodeRenamedEvent and NodeRenamedEvent events don't have full File nodes available for both $source and $target. Naturally the one that doesn't exist anymore/yet is a NonExistingFile node, which doesn't have FileInfo data and consequently doesn't have getMimeType().

@juliushaertl do you know a clean way to get the mimetype from the filename with existing functions from the server?

Otherwise we could compare the filename extension straight away and check if it changed and one is md.

mejo- avatar Aug 28 '24 15:08 mejo-