Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow disabling file name sanitization #1586

Merged

Conversation

taratatach
Copy link
Member

Some clients, like Cozy Desktop, do their own file name sanitization
and don't expect cozy-client to do it for them. In fact, this
sanitization would lead to issues down the road.

To prevent these, we add a sanitizeName option to FileCollection's
methods to disable cozy-client's sanitization.
The option's value will be true by default so other clients don't
have anything to do.

@taratatach taratatach self-assigned this Feb 5, 2025
@taratatach taratatach force-pushed the feat/add-file-creation-option-to-disable-sanitization branch 2 times, most recently from 194c053 to c35efeb Compare February 6, 2025 09:40
Copy link
Member

@Merkur39 Merkur39 left a comment

Choose a reason for hiding this comment

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

👍

packages/cozy-stack-client/src/FileCollection.js Outdated Show resolved Hide resolved
packages/cozy-stack-client/src/FileCollection.js Outdated Show resolved Hide resolved
* @returns {Promise<object>} - A promise that returns the copied file if resolved.
* @throws {FetchError}
*
*/
async copy(id, name, dirId) {
async copy(id, name, dirId, { sanitizeName = true } = {}) {
Copy link
Contributor

@paultranvan paultranvan Feb 6, 2025

Choose a reason for hiding this comment

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

An alternative would have been to pass a function rather than a bool, like:
{ sanitizeFct = sanitizeAndValidateFileName } = {}
And then:
if (sanitizeFct) { sanitizeFct(name) }

Not a big deal but I find it a bit more elegant than using a boolean

Copy link
Member Author

@taratatach taratatach Feb 6, 2025

Choose a reason for hiding this comment

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

It would open up some use cases but I don't find copy(id, name, dirId, { sanitizeFct: undefined }) that elegant.

Copy link
Contributor

@paultranvan paultranvan Feb 6, 2025

Choose a reason for hiding this comment

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

Maybe sanitizeFct: () => {} would be better ? 😄
Anyways, I'm ok with both solutions, it's a matter of preference, so not a requirement to me

@taratatach taratatach force-pushed the feat/add-file-creation-option-to-disable-sanitization branch from c35efeb to 89f840f Compare February 12, 2025 11:00
  Some clients, like Cozy Desktop, do their own file name sanitization
  and don't expect `cozy-client` to do it for them. In fact, this
  sanitization would lead to issues down the road.

  To prevent these, we add a `sanitizeName` option to `FileCollection`'s
  methods to disable `cozy-client`'s sanitization.
  The option's value will be `true` by default so other clients don't
  have anything to do.
@taratatach taratatach force-pushed the feat/add-file-creation-option-to-disable-sanitization branch from 89f840f to 454d28b Compare February 12, 2025 14:01
async copy(id, name, dirId) {
async copy(id, name, dirId, { sanitizeName = true } = {}) {
let sanitizedName = name
if (sanitizedName != null && sanitizeName) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You slightly changed the condition here, from === undefined to != null. Is there a reason ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a habit I have because usually, when we don't want undefined, we don't want null either. And in this case, sanitizing null would throw TypeError: Cannot read properties of null (reading 'trim').

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, thanks

@taratatach taratatach merged commit 7b33a66 into master Feb 12, 2025
3 checks passed
@taratatach taratatach deleted the feat/add-file-creation-option-to-disable-sanitization branch February 12, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants