Skip to content

Comments

fix: Check file existence before using save as#5372

Open
juliusknorr wants to merge 4 commits intomainfrom
fix/saveas-existing-file
Open

fix: Check file existence before using save as#5372
juliusknorr wants to merge 4 commits intomainfrom
fix/saveas-existing-file

Conversation

@juliusknorr
Copy link
Member

@juliusknorr juliusknorr commented Feb 17, 2026

When using the "Save as" function you may end up saving files with the same name which did not lead to a expected copy then. This extends to check for existence before issuing the save as method with Collabora.

In the future a file picker could be used for the folder but we would need to extend that with a name input and further logic in the library so this is a smaller change that can be backported.

@juliusknorr juliusknorr requested a review from elzody as a code owner February 17, 2026 14:23
@juliusknorr juliusknorr added bug Something isn't working 3. to review Ready to be reviewed labels Feb 17, 2026
@elzody elzody force-pushed the fix/saveas-existing-file branch from 426f5e5 to 773389e Compare February 18, 2026 00:42
Signed-off-by: Julius Knorr <jus@bitgrid.net>
@juliusknorr juliusknorr force-pushed the fix/saveas-existing-file branch from 773389e to 8f54906 Compare February 18, 2026 21:25
@moodyjmz
Copy link

@juliusknorr the code looks good. I see there are some build and merge issues.

Also, could you point me at something, or tell me how to test this?

@juliusknorr
Copy link
Member Author

juliusknorr commented Feb 19, 2026

The failing test indeed seems related to the change as it is affecting the save as feature to save a document under a new name. However it seems to only occur in the "direct editing" mode.

This direct editing mode is used by the Nextcloud mobile clients to open office for editing in a webview:
https://github.com/nextcloud/richdocuments/blob/main/docs/mobile_editor.md

To manually test this you can use the steps from #4739 (comment)

Could you maybe take over and see if it is a valid failure and if so work on a fix?

Ping me if you need more assistance for getting this up locally or running tests locally.

@moodyjmz
Copy link

The failing test indeed seems related to the change as it is affecting the save as feature to save a document under a new name. However it seems to only occur in the "direct editing" mode.

This direct editing mode is used by the Nextcloud mobile clients to open office for editing in a webview: https://github.com/nextcloud/richdocuments/blob/main/docs/mobile_editor.md

To manually test this you can use the steps from #4739 (comment)

Could you maybe take over and see if it is a valid failure and if so work on a fix?

Ping me if you need more assistance for getting this up locally or running tests locally.

Yep, I can take over, I will try getting it up locally alone first - sometimes mistakes are good :)

@moodyjmz moodyjmz self-assigned this Feb 19, 2026
@moodyjmz moodyjmz marked this pull request as draft February 19, 2026 10:29
@moodyjmz moodyjmz changed the title fix: Check file existencen before using save as fix: Check file existence before using save as Feb 19, 2026
@moodyjmz
Copy link

So my first go once setup

  • create doc
  • generate link
  • open link in new private window
  • try 'save as'
  • get 'Error checking if file exists.'
curl --request POST \
  --url 'http://nextcloud.local/ocs/v2.php/apps/richdocuments/api/v1/document?format=json' -u admin:admin \
  --header 'Content-Type: multipart/form-data' \
  --header 'OCS-Apirequest: true' \
  --form fileId=[file id]

@moodyjmz
Copy link

import { getClient, getDefaultPropfind, resultToNode, defaultRootPath } from '@nextcloud/files/dav'

defaultRootPath is used to lookup the file, however it is undefined

@moodyjmz
Copy link

moodyjmz commented Feb 20, 2026

function getRootPath() {
  if (isPublicShare()) {
    return `/files/${getSharingToken()}`;
  }
  return `/files/${getCurrentUser()?.uid}`;
}

in node_modules/@nextcloud/files/dist/dav.mjs

Gives undefined as PublicShare is false

@moodyjmz
Copy link

Problem: When opening a document via a direct editing link (e.g. from the OCS API /api/v1/document) in an unauthenticated session (such as an incognito window to simulate the way mobile works - I have not looked at mobile), the "Save As" functionality was producing an invalid DAV URL: remote.php/dav/files/undefined/filename.docx.

Root cause: The defaultRootPath from @nextcloud/files/dav is evaluated once at module import time. It relies on getCurrentUser() from @nextcloud/auth, which reads data-user from the HTML element. In direct editing sessions (rendered as a @publicpage with the 'base' template), there is no authenticated browser session, so getCurrentUser() returns null and the path becomes /files/undefined.

Changed

  1. DirectViewController.php — Added 'userId' => $direct->getUid() to the params passed to the frontend initial state. The direct editing token already stores the initiating user's ID, but it was never provided to the frontend.
  2. SaveAs.vue — Replaced the static defaultRootPath import with a computed rootPath property that resolves the user ID at runtime, falling back to Config.get('userId') (from the backend initial state) when getCurrentUser() is unavailable.
  3. SaveAs.vue — Wrapped the DAV file existence check in if (!Config.get('direct')) because direct editing sessions have no authenticated DAV session, so the stat() call would fail with NotAuthenticated. The backend handles filename conflicts itself (appending (2) etc.). A future improvement could use a dedicated API endpoint that validates the WOPI token to check file existence on behalf of the user.

Signed-off-by: James Manuel <moodyjmz@users.noreply.github.com>
Signed-off-by: James Manuel <moodyjmz@users.noreply.github.com>
@moodyjmz moodyjmz force-pushed the fix/saveas-existing-file branch from 2baf239 to e378915 Compare February 23, 2026 14:05
@moodyjmz
Copy link

I left this open as I made some changes now - I think they are aligned with the original intent though

Copy link
Member Author

@juliusknorr juliusknorr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed I missed that we have no webdav access with direct editing. This is good enough for fixing the reported issue I#d say.

@juliusknorr juliusknorr marked this pull request as ready for review February 24, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Ready to be reviewed bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants