sublimate 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sublimate/chunked_file.rb +22 -0
- data/lib/sublimate/uploader.rb +63 -0
- data/lib/sublimate.rb +8 -0
- metadata +47 -0
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            module Sublimate
         | 
| 2 | 
            +
              #todo: This seems like it could just be IO objects that map to portions of a file
         | 
| 3 | 
            +
              class ChunkedFile
         | 
| 4 | 
            +
                def initialize(path, opts={})
         | 
| 5 | 
            +
                  @path = path
         | 
| 6 | 
            +
                  @chunk_size = opts[:chunk_size] || (1024 * 1024 * 6)
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def each_chunk
         | 
| 10 | 
            +
                  counter = 0
         | 
| 11 | 
            +
                  offset = 0
         | 
| 12 | 
            +
                  file_size = File.size(@path)
         | 
| 13 | 
            +
                  while offset < file_size
         | 
| 14 | 
            +
                    data = IO.read(@path, @chunk_size, offset)
         | 
| 15 | 
            +
                    details = {:counter => counter }
         | 
| 16 | 
            +
                    yield(data, details)
         | 
| 17 | 
            +
                    offset += @chunk_size
         | 
| 18 | 
            +
                    counter += 1
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
            end
         | 
| @@ -0,0 +1,63 @@ | |
| 1 | 
            +
            module Sublimate
         | 
| 2 | 
            +
              class Uploader
         | 
| 3 | 
            +
                def initialize(attrs)
         | 
| 4 | 
            +
                  @attrs = attrs
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  fog_attrs = attrs.clone
         | 
| 7 | 
            +
                  fog_attrs.delete(:auto_create_bucket)
         | 
| 8 | 
            +
                  fog_attrs.delete(:multipart_chunk_size)
         | 
| 9 | 
            +
                  fog_attrs.delete(:bucket)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                  @fog_storage = Fog::Storage.new(fog_attrs)
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def storage
         | 
| 15 | 
            +
                  @fog_storage
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def store_file(path, opts = {})
         | 
| 19 | 
            +
                  opts = {:key => path}.merge(opts)
         | 
| 20 | 
            +
                  size = File.size(path)
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                  bucket = @fog_storage.directories.get(opts[:bucket])
         | 
| 23 | 
            +
                  if bucket.nil? && @attrs[:auto_create_bucket]
         | 
| 24 | 
            +
                    bucket = @fog_storage.directories.create(:key => opts[:bucket])
         | 
| 25 | 
            +
                  elsif bucket.nil?
         | 
| 26 | 
            +
                    raise "bucket not found"
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                  m = "do_multi_#{@attrs[:provider].downcase}"
         | 
| 30 | 
            +
                  if respond_to?(m)
         | 
| 31 | 
            +
                    chunked = ChunkedFile.new(path, :chunk_size => @attrs[:multipart_chunk_size])
         | 
| 32 | 
            +
                    send(m, bucket, chunked, opts)
         | 
| 33 | 
            +
                  else
         | 
| 34 | 
            +
                    opts[:body] = File.open(path)
         | 
| 35 | 
            +
                    bucket.files.create(opts)
         | 
| 36 | 
            +
                  end
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                def do_multi_aws(bucket, chunked, opts)
         | 
| 40 | 
            +
                  multi = @fog_storage.initiate_multipart_upload(opts[:bucket], opts[:key])
         | 
| 41 | 
            +
                  upload_id = multi.body["UploadId"]
         | 
| 42 | 
            +
                  results = []
         | 
| 43 | 
            +
                  chunked.each_chunk do |data, details|
         | 
| 44 | 
            +
                    part = @fog_storage.upload_part(opts[:bucket], opts[:key], upload_id, details[:counter] + 1, data)
         | 
| 45 | 
            +
                    etag = part.headers['ETag']
         | 
| 46 | 
            +
                    results << etag
         | 
| 47 | 
            +
                  end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                  completed_upload = @fog_storage.complete_multipart_upload(opts[:bucket], opts[:key], upload_id, results)
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                def do_multi_rackspace(bucket, chunked, opts)
         | 
| 53 | 
            +
                  count = 1
         | 
| 54 | 
            +
                  chunked.each_chunk do |data, details|
         | 
| 55 | 
            +
                    key = opts[:key] + "/#{count}"
         | 
| 56 | 
            +
                    bucket.files.create(:key => key, :body => data)
         | 
| 57 | 
            +
                    count += 1
         | 
| 58 | 
            +
                  end
         | 
| 59 | 
            +
                  result = @fog_storage.put_object_manifest(bucket.identity, opts[:key])
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
            end
         | 
    
        data/lib/sublimate.rb
    ADDED
    
    | @@ -0,0 +1,8 @@ | |
| 1 | 
            +
            require 'fog'
         | 
| 2 | 
            +
            # rackspace cloud: http://www.rackspace.com/knowledge_center/index.php/Does_Cloud_Files_support_large_file_transfer
         | 
| 3 | 
            +
            # s3: https://gist.github.com/908875
         | 
| 4 | 
            +
            # Google Storage apparently allows single PUTs up to 50GB
         | 
| 5 | 
            +
            module Sublimate
         | 
| 6 | 
            +
            end
         | 
| 7 | 
            +
            require 'sublimate/chunked_file'
         | 
| 8 | 
            +
            require 'sublimate/uploader'
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,47 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: sublimate
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Kurt Mackey
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2011-12-26 00:00:00.000000000Z
         | 
| 13 | 
            +
            dependencies: []
         | 
| 14 | 
            +
            description: Store ginormous files on S3 or the Rackspace Cloud using this gem.
         | 
| 15 | 
            +
            email: mrkurt@gmail.com
         | 
| 16 | 
            +
            executables: []
         | 
| 17 | 
            +
            extensions: []
         | 
| 18 | 
            +
            extra_rdoc_files: []
         | 
| 19 | 
            +
            files:
         | 
| 20 | 
            +
            - lib/sublimate.rb
         | 
| 21 | 
            +
            - lib/sublimate/chunked_file.rb
         | 
| 22 | 
            +
            - lib/sublimate/uploader.rb
         | 
| 23 | 
            +
            homepage: https://github.com/mrkurt/sublimate
         | 
| 24 | 
            +
            licenses: []
         | 
| 25 | 
            +
            post_install_message: 
         | 
| 26 | 
            +
            rdoc_options: []
         | 
| 27 | 
            +
            require_paths:
         | 
| 28 | 
            +
            - lib
         | 
| 29 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
              none: false
         | 
| 31 | 
            +
              requirements:
         | 
| 32 | 
            +
              - - ! '>='
         | 
| 33 | 
            +
                - !ruby/object:Gem::Version
         | 
| 34 | 
            +
                  version: '0'
         | 
| 35 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 36 | 
            +
              none: false
         | 
| 37 | 
            +
              requirements:
         | 
| 38 | 
            +
              - - ! '>='
         | 
| 39 | 
            +
                - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                  version: '0'
         | 
| 41 | 
            +
            requirements: []
         | 
| 42 | 
            +
            rubyforge_project: 
         | 
| 43 | 
            +
            rubygems_version: 1.8.10
         | 
| 44 | 
            +
            signing_key: 
         | 
| 45 | 
            +
            specification_version: 3
         | 
| 46 | 
            +
            summary: Fog with large files
         | 
| 47 | 
            +
            test_files: []
         |