Sha256: e4e2ee7f17bee89e2d07c48c019f5797e6c247077e800703bc49588754c281cb

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

module Vimeo
  module Advanced
    module SimpleUpload
      class Chunk
        MULTIPART_BOUNDARY = "-----------RubyMultipartPost"

        attr_reader :id, :index
        attr_reader :task, :vimeo
        attr_reader :data, :size

        def initialize(task, data)
          @task = task
          @vimeo = task.vimeo
          @data = data
          @size = data.size
          @index = task.chunks.count
        end

        # Performs the upload via Multipart.
        def upload
          endpoint = "#{task.endpoint}"

          response = task.oauth_consumer.request(:post, endpoint, vimeo.get_access_token, {}, {}) do |req|
            req.set_content_type("multipart/form-data", { "boundary" => MULTIPART_BOUNDARY })

            io = StringIO.new(data)
            io.instance_variable_set :"@original_filename", task.filename
            def io.original_filename; @original_filename; end
            def io.content_type; "application/octet-stream"; end

            parts = []
            parts << Parts::ParamPart.new(MULTIPART_BOUNDARY, "ticket_id", task.id)
            parts << Parts::ParamPart.new(MULTIPART_BOUNDARY, "chunk_id", index)
            parts << Parts::FilePart.new(MULTIPART_BOUNDARY, "file_data", io)
            parts << Parts::EpiloguePart.new(MULTIPART_BOUNDARY)

            ios                = parts.map{|p| p.to_io }
            req.content_length = parts.inject(0) {|sum,i| sum + i.length }
            req.body_stream    = CompositeReadIO.new(*ios)

            :continue
          end

          # free memory (for big file uploads)
          @data = nil

          @id = response.body
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
vimeo-1.5.4 lib/vimeo/advanced/simple_upload/chunk.rb
vimeo-1.5.3 lib/vimeo/advanced/simple_upload/chunk.rb
slainer68_vimeo-1.5.2 lib/vimeo/advanced/simple_upload/chunk.rb