Sha256: 14b3dc833f489f827ea41c83beafe9af1ef4d491bd03735c607b3d49e2f0f775

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

module CopyleaksApi
  module Validators
    class FileValidator
      SUPPORTED_FILE_TYPES = [:html, :htm, :txt, :pdf, :doc, :docx, :rtf].freeze
      SUPPORTED_IMAGE_TYPES = [:gif, :png, :bmp, :jpg, :jpeg].freeze
      BYTES_IN_MB = 1_024_000.0

      class << self
        # check file for ocr for correctness
        def validate_ocr!(path)
          validate_file(path, SUPPORTED_IMAGE_TYPES)
        end

        # check text file for correctness
        def validate_text_file!(path)
          validate_file(path, SUPPORTED_FILE_TYPES)
        end

        private

        # check given file for correctness to given type
        def validate_file(path, types)
          ext = file_extension(path)
          return if types.include?(ext) && file_size(path) <= allowed_file_size(ext)
          raise BadFileError, "#{path} file has unsupported extension or to large"
        end

        # returns good file size in MB for given type
        def allowed_file_size(type)
          case type.to_sym
          when :html, :htm
            5
          when :txt
            3
          when :pdf, :doc, :docx
            25
          when *SUPPORTED_IMAGE_TYPES
            25
          else
            0
          end
        end

        # extract file extension
        def file_extension(path)
          path.split('.').last.downcase.to_sym
        end

        # extract file size in MB
        def file_size(path)
          File.size(path) / BYTES_IN_MB
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
plagiarism-checker-2.1.2 lib/copyleaks_api/validators/file_validator.rb
plagiarism-checker-2.1.1 lib/copyleaks_api/validators/file_validator.rb
plagiarism-checker-2.1.0 lib/copyleaks_api/validators/file_validator.rb
plagiarism-checker-2.0.0 lib/copyleaks_api/validators/file_validator.rb
plagiarism-checker-1.0.0 lib/copyleaks_api/validators/file_validator.rb
mkisilenko-test-0.1.0 lib/copyleaks_api/validators/file_validator.rb