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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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 from 409dce9 to 194c053 Compare February 6, 2025 09:31
  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 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.

👍

* @returns {Promise} - Resolves when references have been removed
* and file has been sent to trash
*/
async destroy(file, { ifMatch = '' } = {}) {
const { _id, relationships } = file
const fileId = file.id || file._id
Copy link
Contributor

Choose a reason for hiding this comment

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

quite a nit, but _id is the preferred format, so I would reverse: file._id || file.id , so file._id is evaluated first (and skip file._id if it's true)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I can't remember why this change is part of this PR. It doesn't seem necessary.
I'll see if I can remove it otherwise I'll make the change you suggested.

? undefined
: sanitizeName
? sanitizeAndValidateFileName(name)
: name,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm highly convinced that nested ternaries are a reading and understanding hell 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah. I quite like them as you can see 😅. I'll go with 2 assignments then.

Copy link
Contributor

Choose a reason for hiding this comment

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

We all have deviant sides, who am I to judge...

* @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

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