aki77 / activestorage-validator Goto Github PK
View Code? Open in Web Editor NEWActiveStorage blob validator.
Home Page: https://rubygems.org/gems/activestorage-validator
License: MIT License
ActiveStorage blob validator.
Home Page: https://rubygems.org/gems/activestorage-validator
License: MIT License
Since active storage supports has_many_attached, it is challenging to identify which file is not of the given content type. How can we add a filename from the blob to the error message?
Hello!
I've been having problems to upload an .xlsm file (Excel with macros) I'm using this mime-type application/vnd.ms-excel.sheet.macroEnabled.12 I got it from here and also here. However, I keep receiving a model error {upload_fail: {file: ["is not a valid file format"]}}
and my gem usage is standard i guess.
validates :file, blob: { content_type: [ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'application/vnd.ms-excel.sheet.macroEnabled.12', 'application/vnd.ms-excel.template.macroEnabled.12', 'application/vnd.ms-excel.addin.macroEnabled.12', 'application/vnd.ms-excel.sheet.binary.macroEnabled.12' ], size_range: 0..700.megabytes }
i can't upload .ts extension file. (.ts is video file extension)
this *.ts can't pass on validate when i using direct upload.
but this is working well passing validate when not using direct upload.
i use below code with content_type: :video
class Encode < ApplicationRecord
validates :file, presence: true, blob: { content_type: :video } # supported options: :image, :audio, :video, :text
end
Above Exception is raised when you write this line
validates :image, precence: true, blob: { content_type: ['image/png', 'image/jpeg'], size_range: 1..5.megabytes }
You must have to changed it to presence
Hello,
I am wondering if the content type is enough to validate the file type. Content type is given by the user, which means they can give whatever they want.
Wouldn't it be useful to validate the extension of the file?
We neen to add Arabic translation
Hey could you please add support to add custom error messages.
The current default informs the user to add a locale entry for the specific attribute errors.
I have created a fix where I have default error messages instead.
en:
activerecord:
errors:
messages:
content_type: "is not a valid file format"
max_size_error: "is not within valid size range"
I cannot push my branch up to your repo to create a PR, you need to probably allow that before anyone can contribute.
Hi,
Thanks for making this gem. I am working on using ActiveStorage for storing files uploaded from activeadmin's formtastic inputs. However, this gem seems not to be working with ActiveAdmin.
Although it does raise errors after the file with invalid extension was uploaded, the file is still stored in the storage.
Version:
Currently installing ActiveStorage Validator with Rails 6 (Beta 2) fails with activestorage-validator (~> 0.1.2) was resolved to 0.1.2, which depends on rails (~> 5.2.0)
.
Is there anything blocking the use with Rails 6 or just not tested yet?
Hi there,
Is there a validation option for files with .pdf extension ?
Hi there,
Is there a validation option for dimensions on image file types? e.g. max_size 1000px, min_size 100px, etc? If not, could I add this as a contribution to this project?
Cheers,
Ian
I have tested the validation by changing the extension of a file. For example I took a file called 'test.pdf' and changed it to 'test.docx'. I validate as follows:
validates :file, presence: true, blob: { content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}
This passes. Shouldn't the validation fail since this is really a pdf and not a docx document?
Hi,
The content type of a file can be spoofed which could allow someone to upload an executable file with the content type of jpg. This could allow your app to throw exceptions if it tries to perform image operations on an executable.
Usually the way to determine if something is an image is to read in the first few bytes of the file looking for a specific sequence of bytes. I haven't gotten to implementing something like this with Ruby but with Python the imghdr
standard library module has a imghr.what
function to call which lets you pass in a byte stream or file and it'll return back what format the file is in (jpg, png, etc.).
It's nice to be able to do this on a byte stream instead of a file because if you're uploading things as a stream you can determine if it's an image or not very quickly without uploading the entire file beforehand.
I suppose this would involve introducing a new validator that lets you whitelist a set of valid image formats and it could default to the common case of jpg, jpeg, png and webp. What do you think? Maybe this could be done using the Vips library or the image_processing
gem since this is a requirement of Active Storage?
Hi,
It was not absolutely sure this gem validated the individual sizes of each file in a has_many_attached or if it validated the size of all files combined in the association. I could kind-of see a use for both.
I checked the specs. They did not explicitly validate the size in a many relationship with multiple files.
I read the implementation to mean that it in fact does treat each file individually and does not do combined size validation.
I did figure it out (I think) but it might be useful to clarify this in the README?
Thanks for a great gem, btw.
Hi there
will the file still be uploaded to the bucket if there is a validation error? Looks like it will add errors (see here: https://github.com/aki77/activestorage-validator/blob/master/lib/activestorage/validator/blob.rb) but am ensure whether it will still upload the file to the google bucket.
any pointers would be much appreciated.
chrs
Can anyone help me to limit the number of images using has_many_attached?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.