module UploaderHelperLocal

Protected Instance Methods

delete_uploaded_files() click to toggle source
# File lib/mrpin/core/uploaders/carrierwave/uploader_helper_local.rb, line 111
def delete_uploaded_files
  return unless delete_uploaded_files_after_destroy?

  url_uploaders_map.each_key do |field_name|
    field_name = field_name.to_s

    path_relative = self.send(field_name)
    UtilsIO.try_remove_file_relative(path_relative)
    #try remove gz file too
    UtilsIO.try_remove_file_relative("#{path_relative}.gz")
  end

  nil
end
delete_uploaded_files_after_destroy?() click to toggle source

properties

# File lib/mrpin/core/uploaders/carrierwave/uploader_helper_local.rb, line 18
def delete_uploaded_files_after_destroy?
  true
end
get_path_for_uploader_absolute(field_name) click to toggle source
# File lib/mrpin/core/uploaders/carrierwave/uploader_helper_local.rb, line 104
def get_path_for_uploader_absolute(field_name)
  path_relative = get_path_for_uploader_relative(field_name)

  UtilsIO.to_path_absolute(path_relative)
end
get_upload_file_name(field_name, file_path) click to toggle source
# File lib/mrpin/core/uploaders/carrierwave/uploader_helper_local.rb, line 91
def get_upload_file_name(field_name, file_path)
  result = SecureRandom.uuid.to_s

  extension = File.extname(file_path)

  unless extension.nil?
    result += extension.downcase
  end

  result
end
try_update_uploaded_files() click to toggle source

default constructor

# File lib/mrpin/core/uploaders/carrierwave/uploader_helper_local.rb, line 28
def try_update_uploaded_files
  self.url_uploaders_map.each do |field_name, uploader_field|
    field_name     = field_name.to_s
    uploader_field = uploader_field.to_s

    uploader_value = self.send(uploader_field)

    next unless self.changed_attributes.keys.include?(uploader_field)
    next unless uploader_value.present?

    path_source = ''

    begin
      path_source       = uploader_value.file.file.to_s

      # delete old file if was uploaded
      path_relative_old = self.send(field_name)

      UtilsIO.try_remove_file_relative(path_relative_old)
      #try remove gz file too
      UtilsIO.try_remove_file_relative("#{path_relative_old}.gz")

      path_absolute = get_path_for_uploader_absolute(field_name)

      # create required dirs
      FileUtils.mkdir_p(path_absolute)

      name = get_upload_file_name(field_name, path_source)

      path_destination = File.join(path_absolute, name)

      # upload new file
      upload_file(field_name, path_source, path_destination)
    rescue Exception => e
      errors[:base] << e.to_s
    ensure
      File.delete(path_source) if File.exist?(path_source)
    end
  end

  nil
end
upload_file(field_name, path_source, path_destination) click to toggle source
# File lib/mrpin/core/uploaders/carrierwave/uploader_helper_local.rb, line 72
def upload_file(field_name, path_source, path_destination)
  assert(!File.exist?(path_destination), "file #{path_destination} already exist")

  FileUtils.copy(path_source, path_destination)

  #compress file
  %x(gzip -6 -k #{path_destination})

  path_relative = UtilsIO.to_path_relative(path_destination)

  self.send("#{field_name}=", path_relative)

  puts "file uploaded: #{path_destination}"

  nil
end