Sha256: 3d0112f06e2d42fc34e48174ed99980e71cbb6a909cc4b9bd91dd66f5a659c94

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module CloudCrowd

  # The CloudCrowd::AssetStore should provide a common API for stashing and retrieving
  # assets via URLs, in production this will be S3 but in development it may
  # be the filesystem or /tmp.
  class AssetStore
    include FileUtils
    
    def initialize
      mkdir_p temp_storage_path unless File.exists? temp_storage_path
    end
    
    # Path to CloudCrowd's temporary local storage.
    def temp_storage_path
      "#{Dir.tmpdir}/cloud_crowd_tmp"
    end
    
    # Copy a finished file from our local storage to S3.
    def save(local_path, save_path)
      ensure_s3_connection
      @bucket.put(save_path, File.open(local_path), {}, 'public-read')
    end
    
    # Cleanup all S3 files for a job that's been completed and retrieved.
    def cleanup_job(job)
      ensure_s3_connection
      @bucket.delete_folder("#{job.action}/job_#{job.id}")
    end
    
    # Return the S3 public URL for a finshed file.
    def url(save_path)
      @bucket.key(save_path).public_link
    end
    
    private
    
    # Unused for the moment. Think about using the filesystem instead of S3
    # in development.
    def save_to_filesystem(local_path, save_path)
      save_path = File.join("/tmp/cloud_crowd_storage", save_path)
      save_dir = File.dirname(save_path)
      mkdir_p save_dir unless File.exists? save_dir
      cp(local_path, save_path)
    end
    
    # Workers, through the course of many WorkUnits, keep around an AssetStore.
    # Ensure we have a persistent S3 connection after first use.
    def ensure_s3_connection
      unless @s3 && @bucket
        params = {:port => 80, :protocol => 'http'}
        @s3 = RightAws::S3.new(CloudCrowd.config[:aws_access_key], CloudCrowd.config[:aws_secret_key], params)
        @bucket = @s3.bucket(CloudCrowd.config[:s3_bucket], true)
      end
    end
  
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
documentcloud-cloud-crowd-0.0.1 lib/cloud_crowd/asset_store.rb