Class: EducodeSales::UploadFilesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/upload_files_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#createObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 27

def create
  folder = File.join(Rails.root, "public", "files", 'educode_sales')
  upload_file = params["file"]
  raise "未上传文件" unless upload_file
  save_path = File.join(folder)
  ext = file_ext(upload_file.original_filename)
  local_path, digest = file_save_to_local(save_path, upload_file.tempfile, ext)
  content_type = upload_file.content_type.presence || 'application/octet-stream'
  disk_filename = local_path[save_path.size + 1, local_path.size]
  @attachment = Attachment.where(disk_filename: disk_filename,
                                 author_id: @current_admin.user_id).first
  if @attachment.blank?
    @attachment = Attachment.new
    @attachment.filename = upload_file.original_filename
    if params[:activity_id].present?
      @attachment.container_id = params[:activity_id]
      @attachment.container_type = 'EducodeSales::Activity'
    elsif params[:idea_id].present?
      @attachment.container_id = params[:idea_id]
      @attachment.container_type = 'EducodeSales::Idea'
    elsif params[:follow_up_id].present?
      @attachment.container_id = params[:follow_up_id]
      @attachment.container_type = 'EducodeSales::FollowUp'
    else
      @attachment.description = "business"
      @attachment.container_id = params[:business_id]
    end
    
    @attachment.disk_filename = local_path[save_path.size + 1, local_path.size]
    @attachment.filesize = upload_file.tempfile.size
    @attachment.content_type = content_type
    @attachment.digest = digest
    @attachment.author_id = @current_admin.user_id
  
    @attachment.save!
  else
    logger.info "文件已存在,id = #{@attachment.id}, filename = #{@attachment.filename}"
  end
  render json: { success: true, attachment_id: @attachment.id ,filename: @attachment.filename, url: folder +  "/" + @attachment.disk_filename}
end

#destroyObject



68
69
70
71
72
73
74
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 68

def destroy
  folder = File.join(Rails.root, "public", "files", 'educode_sales')
  file = Attachment.find(params[:id])
  file.destroy
  File.delete(folder +  "/" +file.disk_filename) if FileTest::exist?(folder +  "/" +file.disk_filename)
  render_success
end

#downloadObject



76
77
78
79
80
81
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 76

def download
  folder = File.join(Rails.root, "public", "files", 'educode_sales')
  file = Attachment.find_by_id(params[:id])
  raise "未找到文件" unless file
  send_file(folder +  "/" +file.disk_filename, filename: file.title, stream: true, type: file.content_type.presence || 'application/octet-stream')
end

#edu_setting(name) ⇒ Object



83
84
85
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 83

def edu_setting name
  EduSetting.get(name)
end

#file_ext(file_name) ⇒ Object



88
89
90
91
92
93
94
95
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 88

def file_ext(file_name)
  ext = ''
  exts = file_name.split(".")
  if exts.size > 1
    ext = ".#{exts.last}"
  end
  ext
end

#file_save_to_local(save_path, temp_file, ext) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 97

def file_save_to_local(save_path, temp_file, ext)
  unless Dir.exists?(save_path)
    FileUtils.mkdir_p(save_path) ##不成功这里会抛异常
  end

  digest = md5_file(temp_file)
  digest = "#{digest}_#{(Time.now.to_f * 1000).to_i}"
  local_file_path = File.join(save_path, digest) + ext
  save_temp_file(temp_file, local_file_path)

  [local_file_path, digest]
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 6

def index
  respond_to do |format|
    format.html do
    end
    format.json do
      if params[:follow_up_id].present?
        @files = Attachment.where(container_type: "EducodeSales::FollowUp", container_id: params[:follow_up_id])
      else
        @files = Attachment.where(description: "business", container_id:params[:business_id])
      end
  
      if params[:sort].present? && params[:sort][:field]
        @files = @files.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @files = @files.order("created_on desc")
      end
      @files = @files.page(params[:page]).per(params[:limit])
    end
  end
end

#md5_file(temp_file) ⇒ Object



111
112
113
114
115
116
117
118
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 111

def md5_file(temp_file)
  md5 = Digest::MD5.new
  temp_file.rewind
  while (buffer = temp_file.read(8192))
    md5.update(buffer)
  end
  md5.hexdigest
end

#save_temp_file(temp_file, save_file_path) ⇒ Object



120
121
122
123
124
125
126
127
# File 'app/controllers/educode_sales/upload_files_controller.rb', line 120

def save_temp_file(temp_file, save_file_path)
  File.open(save_file_path, 'wb') do |f|
    temp_file.rewind
    while (buffer = temp_file.read(8192))
      f.write(buffer)
    end
  end
end