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

Add ability to validate already saved attachment (needs remote download) #193

Open
gaelchriste opened this issue May 31, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request v2 Features for releasing v2

Comments

@gaelchriste
Copy link

gaelchriste commented May 31, 2023

Hello, in DimensionValidator is there a reason to validate image dimensions only when uploading attachment and not all the time ?

def validate_each(record, attribute, _value)
return true unless record.send(attribute).attached?
changes = record.attachment_changes[attribute.to_s]
return true if changes.blank?
files = Array.wrap(changes.is_a?(ActiveStorage::Attached::Changes::CreateMany) ? changes.attachables : changes.attachable)
files.each do |file|
metadata = Metadata.new(file).metadata
next if is_valid?(record, attribute, metadata)
break
end
end

Example:

user.avatar.attach(...) # image with wrong dimensions
user.valid? # => false
user.save(validate: false)
user.valid? # => true

Thank you!

@Mth0158
Copy link
Collaborator

Mth0158 commented Dec 1, 2023

@igorkasyanchuk any thoughts on this one?
I guess the validator should add an error when using .valid? as stated in Rails documentation

@Mth0158 Mth0158 added the bug Something isn't working label Dec 1, 2023
@igorkasyanchuk
Copy link
Owner

igorkasyanchuk commented Dec 4, 2023

yeah, perhaps, but can create a bigger issue. If a file is stored in S3 for example, it means that to validate it we need to download it back and measure the dimensions. Probably we can mark this issue as "known issue". It can be handled somehow inside app logic.

wdyt?

@ckornaros
Copy link

@igorkasyanchuk I agree with @Mth0158 .valid? should raise an error in creation or update. I understand your point of view that it can be a issue in some case so maybe we can add an option to choose if we want to validate attachment only on creation ?

@Mth0158
Copy link
Collaborator

Mth0158 commented Nov 8, 2024

Hi @ckornaros,
Having done some work on the gem recently, I think the best approach here would be to add an option on validator needing file ios to run (basically all the are using the Attachable concern).
IMO it has to be an option because the files are stored remotely, to analyze and validate these files we have to download them.

@Mth0158 Mth0158 added question Further information is requested enhancement New feature or request labels Nov 8, 2024
@Mth0158 Mth0158 removed the bug Something isn't working label Nov 26, 2024
@Mth0158 Mth0158 changed the title Dimensions validations on saved attachment Add ability to validate already saved attachment (needs remote download) Nov 26, 2024
@Mth0158 Mth0158 added v2 Features for releasing v2 and removed question Further information is requested labels Nov 26, 2024
@Mth0158 Mth0158 self-assigned this Nov 26, 2024
@Mth0158
Copy link
Collaborator

Mth0158 commented Jan 2, 2025

Hi @ckornaros, @gaelchriste,

Long time no see. This issue should be solved with the v2 coming this month! ✅
This issue only affects has_one_attached relation. The has_many_attached would have validated all files again (and again and again and costing a lot of memory usage for nothing, therefore the use of blob metadata).

FYI, starting from v2 we will store the file metadata (width, height and so on) inside ActiveStorage::Blob#metadata method. Therefore, when doing the below code:

user.avatar.attach(...) # image with wrong dimensions
user.valid? # => false
user.save(validate: false)
user.valid? # => true

It should return false for the last line since the file metadata saved in the first user.valid? will be used for validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v2 Features for releasing v2
Projects
None yet
Development

No branches or pull requests

4 participants