Sha256: 9f77c3e87ed23e7b71c814d93a8bf7080a22d0a21c0480541f6a9e5a801a29e4

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module MobileWorkflow
  module S3Storable
    if Object.const_defined?("Aws::S3")
      extend ActiveSupport::Concern
      
      def binary_urls(object)
        return unless params["binaries"]
    
        params["binaries"].map do |binary|
          object_attribute = binary["identifier"]
          extension = binary["mimetype"].split('/')[1] # i.e. image/jpg --> jpg, video/mp4 --> mp4
      
          {
            "identifier" => binary["identifier"],
            "url" => presigned_url("#{object.class.name.underscore}/#{object.id}/#{object_attribute}/#{s3_object_uuid}.#{extension}"),
            "method" => "PUT"
          }
        end
      end
  
      private
      def s3_object_uuid
        SecureRandom.uuid
      end
      
      def presigned_url(key)
        presigner.presigned_url(:put_object, bucket: ENV['AWS_BUCKET_NAME'], key: key, metadata: {})
      end
  
      def presigner
        Aws::S3::Presigner.new(client: s3_client)
      end
  
      def s3_client
        Aws::S3::Client.new(region: ENV['AWS_REGION'], access_key_id: ENV['AWS_ACCESS_ID'], secret_access_key: ENV['AWS_SECRET_KEY'])
      end
      
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mobile_workflow-0.7.5 app/controllers/concerns/mobile_workflow/s3_storable.rb
mobile_workflow-0.7.4 app/controllers/concerns/mobile_workflow/s3_storable.rb
mobile_workflow-0.7.3 app/controllers/concerns/mobile_workflow/s3_storable.rb
mobile_workflow-0.7.2 app/controllers/concerns/mobile_workflow/s3_storable.rb