Class: FroalaEditorSDK::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/froala-editor-sdk/utils/validation.rb

Overview

Image Validation class. Checks if image is matching the allowed extensions and mime types.

Class Method Summary collapse

Class Method Details

.check(file, options = nil) ⇒ Object

Checks an image with the options. Params:

file

The image that will be validated.

options

The image options that contain allowed extensions and mime types.

Raises exception if the image has not passed the validation



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/froala-editor-sdk/utils/validation.rb', line 21

def self.check(file, options = nil)

  mime = file.content_type
  ext = ::File.extname(file.original_filename)

  # Check if there is custom validation.
  if options[:validation].class != Proc
    ext(ext, options) && mime(mime, options)
  else
    options[:validation].call(file.path, mime)
  end
end

.ext(ext, options) ⇒ Object



8
9
10
# File 'lib/froala-editor-sdk/utils/validation.rb', line 8

def self.ext(ext, options)
  raise "Not allowed" unless options[:validation][:allowedExts].include?(ext)
end

.mime(mime, options) ⇒ Object



12
13
14
# File 'lib/froala-editor-sdk/utils/validation.rb', line 12

def self.mime(mime, options)
  raise "Invalid mime type" unless options[:validation][:allowedMimeTypes].include?(mime)
end