Class: FieldView::Upload

Inherits:
Requestable show all
Defined in:
lib/fieldview/upload.rb

Constant Summary collapse

CONTENT_TYPES =
["image/vnd.climate.thermal.geotiff", "image/vnd.climate.ndvi.geotiff",
"image/vnd.climate.rgb.geotiff", "image/vnd.climate.raw.geotiff"]
REQUIRED_CHUNK_SIZE =

5 Megabytes is the current chunk size

5*1024*1024
CHUNK_CONTENT_TYPE =
"application/octet-stream"
PATH =
"uploads"

Instance Attribute Summary collapse

Attributes inherited from Requestable

#auth_token

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, content_length, auth_token = nil) ⇒ Upload

Returns a new instance of Upload.



38
39
40
41
42
43
44
45
# File 'lib/fieldview/upload.rb', line 38

def initialize(id, content_length, auth_token = nil)
  self.id = id
  self.content_length = content_length.to_i
  if self.content_length <= 0 then
    raise ArgumentError.new("You must set content_length to a non-zero value")
  end
  super(auth_token)
end

Instance Attribute Details

#content_lengthObject

Returns the value of attribute content_length.



11
12
13
# File 'lib/fieldview/upload.rb', line 11

def content_length
  @content_length
end

#content_typeObject

Returns the value of attribute content_type.



11
12
13
# File 'lib/fieldview/upload.rb', line 11

def content_type
  @content_type
end

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/fieldview/upload.rb', line 11

def id
  @id
end

Class Method Details

.create(auth_token, md5, content_length, content_type) ⇒ Object

Creates an upload with the specified items, all required. will return an a new upload object that has it’s ID which can be used to upload. See the constant “CONTENT_TYPES” for the list of known types



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fieldview/upload.rb', line 21

def self.create(auth_token, md5, content_length, content_type)
  if not self.valid_content_type?(content_type) then
    raise ArgumentError.new("content_type must be set to one of the following: #{CONTENT_TYPES.join(', ')}")
  end

  response = auth_token.execute_request!(:post, PATH,
    params: {
      "md5" => md5,
      "length" => content_length,
      "contentType" => content_type
  })

  Util.verify_response_with_code("Upload creation", response, 201)

  return new(response.http_body.gsub('"', ''), content_length, auth_token)
end

.valid_content_type?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/fieldview/upload.rb', line 13

def self.valid_content_type?(content_type)
  return CONTENT_TYPES.include?(content_type)
end

Instance Method Details

#upload_chunk(start_bytes, end_bytes, bytes) ⇒ Object

Upload a chunk of a file, specify the range of the bytes, 0 based, for the bytes you’re passing, for example, the first 256 bytes would be start_bytes = 0 and end_bytes = 255, with bytes = 256 byte size object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fieldview/upload.rb', line 50

def upload_chunk(start_bytes, end_bytes, bytes)
  if (end_bytes.to_i - start_bytes.to_i + 1) != bytes.bytesize ||
    bytes.bytesize > REQUIRED_CHUNK_SIZE then
    raise ArgumentError.new("End bytes (#{end_bytes}) - Start bytes (#{start_bytes})" \
      " must be equal to bytes (#{bytes}) and no greater than #{REQUIRED_CHUNK_SIZE}")
  end
  response = auth_token.execute_request!(:put, "#{PATH}/#{self.id}",
    headers: {
      "Content-Range" => "bytes #{start_bytes}-#{end_bytes}/#{self.content_length}",
      "Content-Length" => bytes.bytesize,
      "Content-Type" => CHUNK_CONTENT_TYPE,
      "Transfer-Encoding" => "chunked"
    },
    params: bytes)

  # We expect a 204
  Util.verify_response_with_code("Chunk upload", response, 204)

  return response
end