I'm using Srhine for images in my project and Omniauth gems for logging via Google and Facebook.
My method of getting data from Omniauth provider is the standard one:
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.name = [auth.info.first_name, auth.info.last_name].join(" ")
user.password = Devise.friendly_token[0, 20]
user.avatar_remote_url = auth.info.image
end
end
It allows me to login via Google and Facebook and get user data, name and image. I also have standard registration and possibility to update user.avatar so I wanted to validate the image so that it has a limited size and so that the extensions are also limited. I wrote the following validation in my Uploader class:
Attacher.validate do
validate_max_size 2*1024*1024
# validate_extension %w[jpg jpeg png webp]
end
The problem is that the validation of extension doesn't allow for registering new user via Facebook. In case of FB auth.info.image doesn't really return a link to an image like 'https://blabla/image.jpg' but rather it creates a link which, when you paste it into the browser, results in direct download of the image to the hard drive. How can I write this validation so that it allows for the FB way of providing image to be saved? Or maybe how can I skip this validation in the Omniauth method?
You can set a custom flag in
Shrine::Attachercontext hash before attaching the file:Then in the
validateblock you can access it and skip extension validation if the flag is present: