Skip to content

Commit

Permalink
Fix offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
luilver committed Feb 20, 2021
1 parent 063ac09 commit 6c94c44
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 146 deletions.
203 changes: 96 additions & 107 deletions app/controllers/importers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class ImportersController < ApplicationController
def new
@importer = Importer.new
@importer.entity_type = params[:entity_type]
if params[:entity_id]
@importer.entity_id = params[:entity_id]
end
@importer.entity_id = params[:entity_id] if params[:entity_id]
respond_with(@importer)
end

Expand All @@ -35,12 +33,11 @@ def create

respond_to do |format|
if errors
format.html { render "create", :locals => {errors: errors} }
format.html { render "create", locals: { errors: errors } }
else
format.html { redirect_to form_map_columns_importer_path(@importer) }
end
end

end

# get /importers/:id/map
Expand All @@ -53,32 +50,28 @@ def form_map_columns
attributes_extra = []

object = @importer.entity_class
_attrs = object.attribute_names - ['id']
attrs = object.attribute_names - ['id']

_attrs.each do |attr|
attrs.each do |attr|
attributes.push(
{
name: attr,
required: object.validators_on(attr).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
}
name: attr,
required: object.validators_on(attr).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
)
end

if @importer.entity_type == 'lead'
_attrs = Address.attribute_names - %w(id created_at updated_at deleted_at address_type addressable_type addressable_id)
attrs = Address.attribute_names - %w[id created_at updated_at deleted_at address_type addressable_type addressable_id]

_attrs.each do |attr|
attrs.each do |attr|
attributes_extra.push(
{
name: attr,
required: Address.validators_on(attr).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
}
name: attr,
required: Address.validators_on(attr).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
)
end
end

respond_to do |format|
format.html { render "form_map_columns", :locals => {columns: columns, attributes: attributes, attributes_extra: attributes_extra} }
format.html { render "form_map_columns", locals: { columns: columns, attributes: attributes, attributes_extra: attributes_extra } }
end
end

Expand All @@ -97,97 +90,93 @@ def map_columns
end
end

=begin
# get /campaigns/import AJAX
#----------------------------------------------------------------------------
def import
@importer = Importer.new
@importer.entity_type = 'Campaign'
respond_with(@importer)
end
# patch /campaigns/import AJAX
#----------------------------------------------------------------------------
def import_upload
@error = false
@result = {
items: [],
errors: []
}
if params[:importer]
@importer = Importer.create(import_params)
if @importer.valid?
@importer.save
@result = FatFreeCRM::ImportHandle.process(@importer)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@error,@result)
end
# get /campaigns/%id/import AJAX
#----------------------------------------------------------------------------
def import_leads
@importer = Importer.new
@importer.entity_type = 'Lead'
respond_with(@importer)
end
# patch /campaigns/import AJAX
#----------------------------------------------------------------------------
def uploads_import_leads
@error = false
@result = {
items: [],
errors: []
}
if params[:importer]
@importer = Importer.create(import_params)
if @importer.valid?
@importer.save
@colummns = FatFreeCRM::ImportHandle.get_columns(@importer.attachment.path)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@colummns) do |format|
format.js { render :uploads_import_leads }
end
end
=end
# # get /campaigns/import AJAX
# #----------------------------------------------------------------------------
# def import
# @importer = Importer.new
# @importer.entity_type = 'Campaign'
# respond_with(@importer)
# end
#
# # patch /campaigns/import AJAX
# #----------------------------------------------------------------------------
# def import_upload
# @error = false
# @result = {
# items: [],
# errors: []
# }
#
# if params[:importer]
# @importer = Importer.create(import_params)
# if @importer.valid?
# @importer.save
# @result = FatFreeCRM::ImportHandle.process(@importer)
# else
# puts @importer.errors.full_messages
# @result[:errors].push(@importer.errors.full_messages)
# @error = true
# end
# end
# respond_with(@error,@result)
# end
#
#
# # get /campaigns/%id/import AJAX
# #----------------------------------------------------------------------------
# def import_leads
# @importer = Importer.new
# @importer.entity_type = 'Lead'
# respond_with(@importer)
# end
#
# # patch /campaigns/import AJAX
# #----------------------------------------------------------------------------
# def uploads_import_leads
# @error = false
# @result = {
# items: [],
# errors: []
# }
#
# if params[:importer]
# @importer = Importer.create(import_params)
# if @importer.valid?
# @importer.save
# @colummns = FatFreeCRM::ImportHandle.get_columns(@importer.attachment.path)
# else
# puts @importer.errors.full_messages
# @result[:errors].push(@importer.errors.full_messages)
# @error = true
# end
# end
# respond_with(@colummns) do |format|
# format.js { render :uploads_import_leads }
# end
# end
#
# post /importers/create
#----------------------------------------------------------------------------
=begin
def create
@error = false
@result = {
items: [],
errors: []
}
if params[:importer]
@importer = Importer.create(import_params)
if @importer.valid?
@importer.save
@result = FatFreeCRM::ImportHandle.process(@importer)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@error,@result)
end
=end
# def create
# @error = false
# @result = {
# items: [],
# errors: []
# }
#
# if params[:importer]
# @importer = Importer.create(import_params)
# if @importer.valid?
# @importer.save
# @result = FatFreeCRM::ImportHandle.process(@importer)
# else
# puts @importer.errors.full_messages
# @result[:errors].push(@importer.errors.full_messages)
# @error = true
# end
# end
# respond_with(@error,@result)
# end

private

Expand Down
18 changes: 10 additions & 8 deletions app/models/files/imported_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ class ImportedFile < ActiveRecord::Base
validates :md5sum, uniqueness: { message: "file already imported" }

def generate_md5sum
self.md5sum = Digest::MD5.hexdigest File.open(filename).read unless filename.empty? rescue ""
self.md5sum = Digest::MD5.hexdigest File.open(filename).read unless filename.empty?
rescue StandardError
""
end

private

def filetype
valid = File.open(filename).type_from_file_command == "application/vnd.ms-excel" rescue ""
if valid == ""
errors.add(:filename, "no such file")
end
unless valid
errors.add(:filename, "invalid filetype")
end
valid = begin
File.open(filename).type_from_file_command == "application/vnd.ms-excel"
rescue StandardError
""
end
errors.add(:filename, "no such file") if valid == ""
errors.add(:filename, "invalid filetype") unless valid
end

ActiveSupport.run_load_hooks(:fat_free_crm_imported_file, self)
Expand Down
12 changes: 7 additions & 5 deletions app/models/files/importer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: importers
Expand All @@ -16,21 +18,21 @@
class Importer < ActiveRecord::Base
attribute :entity_attrs

has_attached_file :attachment, :path => ":rails_root/public/importers/:id/:filename"
has_attached_file :attachment, path: ":rails_root/public/importers/:id/:filename"

validates_attachment :attachment, presence: true

validates_attachment_content_type :attachment,
:content_type => %w(text/xml application/xml application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/x-ole-storage),
:message => 'Only EXCEL files are allowed.'
content_type: %w[text/xml application/xml application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/x-ole-storage],
message: 'Only EXCEL files are allowed.'
validates_attachment_file_name :attachment, matches: [/\.xls/, /\.xlsx?$/]

def get_messages()
def messages
JSON.parse(messages)
end

def entity_class
self.entity_type.capitalize.constantize
entity_type.capitalize.constantize
end

ActiveSupport.run_load_hooks(:fat_free_crm_importer, self)
Expand Down
2 changes: 1 addition & 1 deletion app/views/importers/map_columns.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%div #{t :importer_status_label } #{ @importer.status }
- @importer.get_messages.each do |message|
- @importer.messages.each do |message|
%div #{message}

12 changes: 10 additions & 2 deletions config/initializers/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
#------------------------------------------------------------------------------
File.class_eval do
def type_from_file_command
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
mime_type = `file -b --mime-type #{self.path}`.split(':').last.strip rescue "application/x-#{type}"
type = (begin
original_filename.match(/\.(\w+)$/)[1]
rescue StandardError
"octet-stream"
end).downcase
mime_type = begin
`file -b --mime-type #{path}`.split(':').last.strip
rescue StandardError
"application/x-#{type}"
end
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
mime_type
end
Expand Down
4 changes: 3 additions & 1 deletion config/initializers/paperclip.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
end
Loading

0 comments on commit 6c94c44

Please sign in to comment.