app/models/s3_multipart/upload.rb in s3_multipart-0.0.8 vs app/models/s3_multipart/upload.rb in s3_multipart-0.0.9

- old
+ new

@@ -1,15 +1,15 @@ module S3Multipart class Upload < ::ActiveRecord::Base extend S3Multipart::TransferHelpers - attr_accessible :key, :upload_id, :name, :location, :uploader -#before_create :validate_mime_types + attr_accessible :key, :upload_id, :name, :location, :uploader, :size + before_create :validate_file_type, :validate_file_size def self.create(params) response = initiate(params) - super(key: response["key"], upload_id: response["upload_id"], name: response["name"], uploader: params["uploader"]) + super(key: response["key"], upload_id: response["upload_id"], name: response["name"], uploader: params["uploader"], size: params["content_size"]) end def execute_callback(stage, session) controller = deserialize(uploader) @@ -21,15 +21,22 @@ end end private - def validate_mime_types(upload) - ext = upload.name.match(/\.([a-zA-Z0-9]+)$/)[1] - controller = deserialize(upload.uploader) + def validate_file_size + size = self.size + limits = deserialize(self.uploader).size_limits + raise FileSizeError, "File size is too small" if limits[:min] > size + raise FileSizeError, "File size is too large" if limits[:max] < size + end + def validate_file_type + ext = self.name.match(/\.([a-zA-Z0-9]+)$/)[1] + controller = deserialize(self.uploader) + if !controller.file_types.include?(ext) - return false + raise FileTypeError, "File type not supported" end end def deserialize(uploader) S3Multipart::Uploader.deserialize(uploader)