Sha256: 5db10c8d3c65b87b74279f5f619e2c07bcd5b610e246dd84451a6b4fc3daa4a0

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module Blogo::Admin
  # Handles image upload with CKeditor.
  class ImagesController < BaseController
    skip_before_filter :verify_authenticity_token

    layout false

    # Directory in public with blog images
    IMAGE_DIRECTORY = 'system/blog/images'

    # POST /admin/images
    #
    # Upload image.
    def create
      upload_io = params[:upload]

      image_name = upload_io.original_filename
      file_path = Rails.root.join('public', image_directory, image_name)

      if File.exists?(file_path)
        @error = I18n.translate('blogo.admin.image_already_exists', image_name: image_name)
      else
        dir = File.dirname(file_path)
        FileUtils.mkdir_p(dir) unless File.exists?(dir)
        File.binwrite(file_path, upload_io.read)
      end

      @image_path = File.join('/', image_directory, image_name)
    end


    private

    # Image directory with year/month subdirectories.
    #
    # @return [String]
    def image_directory
      @image_directory ||= begin
        date_dir = Time.now.strftime('%Y/%m')
        File.join(IMAGE_DIRECTORY, date_dir)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
blogo-0.1.1 app/controllers/blogo/admin/images_controller.rb
blogo-0.1.0 app/controllers/blogo/admin/images_controller.rb
blogo-0.0.10 app/controllers/blogo/admin/images_controller.rb
blogo-0.0.9 app/controllers/blogo/admin/images_controller.rb