Sha256: d3fd3599ed9b1f5a022cc9750ea1f90950eb59c15a994d208c26ac688d683bb3

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require "SimpleCSVUploder/version"

module SimpleCSVUploder
  attr_accessor :file

  def initialize(params = {})
    @file = params.delete(:file)
    super
    if @file
      self.filename = sanitize_filename(@file.original_filename)
      self.content_type = @file.content_type
      self.file_contents = @file.read
    end
  end

  def upload_local
    path = "#{Rails.root}/public/uploads/csv"
    FileUtils.mkdir_p(path) unless File.exists?(path)
    FileUtils.copy(@file.tempfile, path)
  end

  private

  def sanitize_filename(filename)
    return File.basename(filename)
  end

  def check_duplication_filename
    if Document.where(filename: self.filename).present?
      errors.add(:file, 'File already exists.')
    end
  end

  def csv_file_format
    if self.content_type != "text/csv"
     errors.add(:file, 'File format should be only CSV.')
    end
  end

  NUM_BYTES_IN_MEGABYTE = 1048576
  def file_size_under_one_mb
    if !@file.nil?
      if (@file.size.to_f / NUM_BYTES_IN_MEGABYTE) > 1
        errors.add(:file, 'File size cannot be over one megabyte.')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
SimpleCSVUploder-0.1.3 lib/SimpleCSVUploder.rb