class BenevolentGaze::Kiosk

Constants

USE_AWS

Public Instance Methods

upload(filename, file, device_name) click to toggle source
# File lib/benevolent_gaze/kiosk.rb, line 35
def upload(filename, file, device_name)
    doomsday = Time.mktime(2038, 1, 18).to_i
    if (filename)
      new_file_name = device_name.to_s + SecureRandom.uuid.to_s + filename
      bucket = ENV['AWS_CDN_BUCKET']
      image = MiniMagick::Image.open(file.path)

      animated_gif = `identify -format "%n" "#{file.path}"`.to_i > 1
      if animated_gif
        image.repage "0x0"
        if image.height > image.width
          image.resize "300"
          offset = (image.height/2) - 150
          image.crop("300x300+0+#{offset}")
        else
          image.resize "x300"
          offset = (image.width/2) - 150
          image.crop("300x300+#{offset}+0")
        end
        image << "+repage"
      else
        image.auto_orient
        if image.height > image.width
          image.resize "300"
          offset = (image.height/2) - 150
          image.crop("300x300+0+#{offset}")
        else
          image.resize "x300"
          offset = (image.width/2) - 150
          image.crop("300x300+#{offset}+0")
        end
        image.format "png"
      end

      if USE_AWS
        AWS::S3::Base.establish_connection!(
          :access_key_id     => ENV['AWS_ACCESS_KEY_ID'],
          :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
        )
        AWS::S3::S3Object.store(
          new_file_name,
          image.to_blob,
          bucket,
          :access => :public_read
        )
        image_url = AWS::S3::S3Object.url_for( new_file_name, bucket, :expires => doomsday )
      else
        upload_path =  @@local_file_system + '/images/uploads/'
        file_on_disk = upload_path + new_file_name
        File.open(File.expand_path(file_on_disk), "w") do |f|
          f.write(image.to_blob)
        end
        image_url = "images/uploads/" + new_file_name
      end

      return image_url 
      
    else
      return nil
    end
end