Sha256: 5306eacb4c78ad498bbdf5f48faaf8f74c55a82fb3f31917434905ad7688196c

Contents?: true

Size: 877 Bytes

Versions: 33

Compression:

Stored size: 877 Bytes

Contents

require_dependency "my_forum/application_controller"

module MyForum
  class AttachmentsController < ApplicationController

    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

  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
my_forum-0.0.2.4 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.2.3 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.2.2 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.2.1 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.2 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta60 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta59 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta58 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta57 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta56 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta55 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta54 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta53 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta52 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta51 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta50 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta49 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta48 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta47 app/controllers/my_forum/attachments_controller.rb
my_forum-0.0.1.beta46 app/controllers/my_forum/attachments_controller.rb