Skip to content

Conversation

@AhsanIsEpic
Copy link
Contributor

@AhsanIsEpic AhsanIsEpic commented Apr 14, 2025

This pull request fixes #208, where folders with a space at the end of their name cause the Google Drive import to fail.

I’m not very experienced with PHP, please review thoroughly

@AhsanIsEpic AhsanIsEpic force-pushed the fix/improve-folder-creation-handling branch from df70c23 to af30a92 Compare April 14, 2025 20:58
@AhsanIsEpic
Copy link
Contributor Author

Could I get approval for workflows?

@AhsanIsEpic AhsanIsEpic force-pushed the fix/improve-folder-creation-handling branch from af30a92 to c6aff2f Compare April 14, 2025 23:03
@AhsanIsEpic
Copy link
Contributor Author

AhsanIsEpic commented Apr 14, 2025

I’ve tested the code with a couple of scenarios and it works perfectly:

  1. Verified that Google Drive migration completes successfully renaming invalid file names

However, I think I may have found a potential design issue:

The code is inconsistent on how file names are sanitised
All none letter and numbers in file names from Google Drive are replaced with hyphens (-) whereas folder name sanitation does not exist. This is not the same with the Google Photos API where only backslashes are replaced for album and photo names.

@AhsanIsEpic AhsanIsEpic marked this pull request as ready for review April 14, 2025 23:18
@AhsanIsEpic AhsanIsEpic force-pushed the fix/improve-folder-creation-handling branch from c6aff2f to a95301e Compare April 15, 2025 03:46
@AhsanIsEpic
Copy link
Contributor Author

Updated the code be consistent and match the sanitation in nextcloud/server#27891

@AhsanIsEpic AhsanIsEpic changed the title Fix: Improve folder creation error handling and sanitize folder names Fix: Improve sanitization of folder and file names Apr 15, 2025
Signed-off-by: Ahsan Ahmed <ahmedah05@outlook.com>
@AhsanIsEpic AhsanIsEpic force-pushed the fix/improve-folder-creation-handling branch from a95301e to d3a81f2 Compare April 15, 2025 04:44
@marcelklehr
Copy link
Member

marcelklehr commented Apr 15, 2025

Hello @AhsanIsEpic
Thank you for your work on this PR!
Mmmh, I'm not sure if the issue you referenced is authoritative. Perhaps we should use OCP\Files\IFilenameValidator instead here?

@AhsanIsEpic AhsanIsEpic force-pushed the fix/improve-folder-creation-handling branch from 544017f to ab78970 Compare April 16, 2025 03:36
…and Photos services using IFilenameValidator

Signed-off-by: Ahsan Ahmed <ahmedah05@outlook.com>
@AhsanIsEpic AhsanIsEpic force-pushed the fix/improve-folder-creation-handling branch from ab78970 to a44e74f Compare April 16, 2025 03:37
@AhsanIsEpic
Copy link
Contributor Author

Hello @AhsanIsEpic Thank you for your work on this PR! Mmmh, I'm not sure if the issue you referenced is authoritative. Perhaps we should use OCP\Files\IFilenameValidator instead here?

I ended up creating a shared utility function to handle all the possible errors that can be thrown from IFilenameValidator. I could of just replaced the filename with the file ID however, I wanted to keep the filename intact as much as possible.

Introduction of Utility Function:

Integration of Utility Function:

  • lib/Service/GoogleDriveAPIService.php:
    • Added FileUtils import.
    • Updated createDirsUnder method to use FileUtils::sanitizeFilename for directory names.
    • Updated getFileName method to use FileUtils::sanitizeFilename for file names.
  • lib/Service/GooglePhotosAPIService.php:
    • Added FileUtils import.
    • Updated importPhotos method to use FileUtils::sanitizeFilename for album names.
    • Updated getPhoto method to use FileUtils::sanitizeFilename for photo names.

default:
$logger->error('Unknown exception encountered during filename sanitization: ' . $filename);
$filename = 'Untitled';
break;
Copy link
Member

Choose a reason for hiding this comment

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

Wow, this is a really elaborate. Thank you for the effort you put into this! I was actually thinking more of something like this: https://github.com/nextcloud/server/pull/51608/files#diff-911ea9939fad17c78ada50c38706874091e2478e37a2ef5a155481c0c4a81a98R144-R165 But it turns out the methods used there are not in OCP :(

Copy link
Member

Choose a reason for hiding this comment

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

I'm inquiring now if we can get a proper sanitization method in OCP to avoid parsing error messages from the validator, which seems a bit brittle

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Nextcloud 32 will have that method in OCP

@github-actions
Copy link

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.)

@marcelklehr
Copy link
Member

Are you still interested in finishing this PR?

@AhsanIsEpic
Copy link
Contributor Author

Are you still interested in finishing this PR?

I'm still interested in finishing this PR, however I'm unsure if I should be waiting for nextcloud 32 to be released before completing this MR or would it be fine to use the development version?

@marcelklehr
Copy link
Member

We can implement this against nextcloud 32 and release it when nextcloud 32 is released, excluding support for nextcloud < 32

@marcelklehr
Copy link
Member

marcelklehr commented May 23, 2025

Optionally, we can also check if the method exists in the class and only use it if it exists. That would allow to support versions < 32

AhsanIsEpic and others added 2 commits May 24, 2025 17:56
…on handling and use built in validator for nextcloud 32+

Signed-off-by: Ahsan Ahmed <ahmedah05@outlook.com>
@AhsanIsEpic AhsanIsEpic force-pushed the fix/improve-folder-creation-handling branch from 57fddfc to 19afaf5 Compare May 24, 2025 17:20
@AhsanIsEpic
Copy link
Contributor Author

Optionally, we can also check if the method exists in the class and only use it if it exists. That would allow to support versions < 32

I've added a few lines to check if the OCP version is 32 or higher, and use the built-in validator in that case (still need to run some tests on it).

Would you prefer that I remove the sanitation logic I previously added and revert to the way they were originally for older versions, or should we keep it?

@marcelklehr
Copy link
Member

Looks good!

…anIsEpic/integration_google into AhsanIsEpic-fix/improve-folder-creation-handling
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
@marcelklehr marcelklehr force-pushed the fix/improve-folder-creation-handling branch from a06d173 to d33630a Compare June 23, 2025 11:45
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
@marcelklehr marcelklehr force-pushed the fix/improve-folder-creation-handling branch from d33630a to 4b4d171 Compare June 23, 2025 11:51
@marcelklehr marcelklehr merged commit 0473195 into nextcloud:main Jun 24, 2025
20 checks passed
@marcelklehr
Copy link
Member

Thank you so much for your contribution @AhsanIsEpic

@lukasdotcom lukasdotcom mentioned this pull request Jul 10, 2025
MarcelRobitaille added a commit to MarcelRobitaille/nextcloud_google_synchronization that referenced this pull request Aug 22, 2025
MarcelRobitaille added a commit to MarcelRobitaille/nextcloud_google_synchronization that referenced this pull request Aug 23, 2025
@marcelklehr
Copy link
Member

Hi @AhsanIsEpic !
Thank you again for taking the time to contribute to Nextcloud! There is a community talk instance with a room for the integrations team that I'm part of. I'd like to invite you to join this room. You can send me a message to marcel.klehr@nextcloud.com if you are interested, I will add you. 💙

Cheers
Marcel

@AhsanIsEpic
Copy link
Contributor Author

Sent an email across @marcelklehr

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Folder names with trailing spaces cause migration failure (Google Drive / Photos)

2 participants