class Object

Public Instance Methods

create() click to toggle source
# File lib/active_admin_pro/summernote_image/summernote_image.rb, line 6
def create
  @file = params[:file]
  if ENV["AWS_BUCKET"]
    @bucket = s3_bucket
  elsif ENV["GOOGLE_STORAGE_BUCKET"]
    @bucket = google_storage_bucket
  end
  if @bucket
    render text: upload_file
  else
    render json: { error: "No AWS or Google cloud storage bucket setup.  Please set the AWS_BUCKET or GOOGLE_STORAGE_BUCKET." }, status: :unprocessable_entity
  end
end
google_storage_bucket() click to toggle source
# File lib/active_admin_pro/summernote_image/summernote_image.rb, line 31
def google_storage_bucket
  connection = Fog::Storage.new(
    provider:                         "Google",
    google_storage_access_key_id:     ENV.fetch("GOOGLE_STORAGE_ACCESS_KEY_ID"),
    google_storage_secret_access_key: ENV.fetch("GOOGLE_STORAGE_SECRET_ACCESS_KEY")
  )
  connection.directories.get(ENV.fetch("GOOGLE_STORAGE_BUCKET"))
end
s3_bucket() click to toggle source
# File lib/active_admin_pro/summernote_image/summernote_image.rb, line 40
def s3_bucket
  connection = Fog::Storage.new(
    provider:              "AWS",
    aws_access_key_id:     ENV.fetch("AWS_ACCESS_KEY_ID"),
    aws_secret_access_key: ENV.fetch("AWS_SECRET_ACCESS_KEY")
  )
  connection.directories.get(ENV.fetch("AWS_BUCKET"))
end
storage_object_key() click to toggle source

The storage object key used for saving the image uploaded from the Summernote editor. @return [String]

# File lib/active_admin_pro/summernote_image/summernote_image.rb, line 24
def storage_object_key
  file_ext = File.extname(@file.original_filename)
  file_base = File.basename(@file.original_filename, file_ext)
  time = Time.now.to_i
  "aap/si/#{file_base.parameterize}-#{time}#{file_ext}"
end
upload_file() click to toggle source

Upload the image to the Amazon S3 bucket or Google Storage bucket. @return [String] The public URL of the image

# File lib/active_admin_pro/summernote_image/summernote_image.rb, line 51
def upload_file
  file = @bucket.files.create(
    key:    storage_object_key,
    body:   @file.tempfile,
    public: true
  )
  file.save
  file.public_url
end