Skip to content

Conversation

@pbirrer
Copy link
Contributor

@pbirrer pbirrer commented Jun 12, 2025

📝 Summary

When copying markdown files, the attachment IDs embedded in the file content were not updated to match the newly created attachments. This PR fixes that by:

  • Updating AttachmentService::copyAttachments() to return a map of old to new file IDs.
  • Adding AttachmentService::replaceAttachmentFileIds() to update these references in the file content.

🏁 Checklist

  • Code is properly formatted (npm run lint / npm run stylelint / composer run cs:check)
  • Sign-off message is added to all commits
  • Tests (unit, integration and/or end-to-end) passing and the changes are covered with tests
  • Documentation (README or documentation) has been updated or is not required

@rvjr
Copy link

rvjr commented Jun 12, 2025

@mejo- this is a bugfix we discovered during our internal use of #6676. Copied files still referenced attachments in the attachments folder of the source file, when they were referred in md by file id.

Copy link
Collaborator

@max-nextcloud max-nextcloud left a comment

Choose a reason for hiding this comment

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

Thanks a lot for bringing this up and addressing it right away. 🥇

I have one comment. Please check if I'm mistaken - just read the code and did not try it myself.

As I said in the comment... a unit test might come in handy to ensure the function works as desired. If you need further help with writing it let me know.

foreach ($fileIdMapping as $mapping) {
$patterns[] = '/(\[[^\]]+\]\(\S+\/f\/)' . $mapping[0] . '/';
// Replace `[title](URL/f/sourceId` with `[title](URL/f/targetId`
$replacements[] = '${1}' . $mapping[1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this will drop the trailing / as it's included in the patterns but not in the replacements.

As this is a public static method and the arguments are not very complex it seems like a good candidate for a unit test. You'd have to mock getContent and putContent on target similarly to how it's done in the testReplaceAttachmentFolderId test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ahh... my bad... the / is ending the regexp. Unit test would still be nice - but now i see that this probably works well as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I added a unit test for replaceAttachmentFileIds. It actually helped catch a couple issues with my original regex too.

@juliusknorr juliusknorr added bug Something isn't working 3. to review labels Jun 13, 2025
@max-nextcloud max-nextcloud self-requested a review June 13, 2025 06:28
@pbirrer pbirrer force-pushed the fix/attachment-id-replacement-on-copy branch from bdf1626 to 205b6aa Compare June 13, 2025 10:36
@max-nextcloud max-nextcloud enabled auto-merge June 13, 2025 12:35
auto-merge was automatically disabled June 13, 2025 13:02

Head branch was pushed to by a user without write access

@pbirrer pbirrer force-pushed the fix/attachment-id-replacement-on-copy branch from 205b6aa to b0617cb Compare June 13, 2025 13:02
@mejo-
Copy link
Member

mejo- commented Jun 14, 2025

Thanks so much for taking care of this @pbirrer! Much appreciated 🫶

@mejo- mejo- enabled auto-merge June 14, 2025 09:22
@mejo-
Copy link
Member

mejo- commented Jun 14, 2025

/backport to stable31

@mejo-
Copy link
Member

mejo- commented Jun 14, 2025

/backport to stable30

@mejo- mejo- merged commit 5a32623 into nextcloud:main Jun 14, 2025
67 checks passed
@github-actions
Copy link
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants