Class: MyForum::AttachmentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/my_forum/attachments_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_user!, #current_user, #current_user_groups, #current_user_id, #forum_time, #is_admin?, #new_pm_count

Instance Method Details

#createObject



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

def create
  upload_path = File.join(Attachment::UPLOAD_PATH, current_user.id.to_s)

  # Create dir of not exists
  FileUtils::mkdir_p upload_path

  attachment_ids = []

  params[:files].each do |uploaded_io|
    next unless Attachment::ALLOWED_FILE_CONTENT_TYPE.include? uploaded_io.content_type

    file_name = "#{Time.now.to_i}_#{uploaded_io.original_filename}"
    File.open(File.join(upload_path, file_name), 'wb') do |file|
      file.write(uploaded_io.read)
    end

    attachment = Attachment.create(user_id: current_user.id, file_name: file_name)
    attachment_ids.push attachment.id
  end

  render json: { success: true, attachments: attachment_ids }.to_json
end