Class: Canistor::Storage::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/canistor/storage/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Object

Returns a new instance of Object.



15
16
17
18
19
20
21
22
# File 'lib/canistor/storage/object.rb', line 15

def initialize(**attributes)
  @headers = {}
  @data = ''
  attributes.each do |name, value|
    send("#{name}=", value)
  end
  @digest = nil
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



9
10
11
# File 'lib/canistor/storage/object.rb', line 9

def bucket
  @bucket
end

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/canistor/storage/object.rb', line 12

def data
  @data
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/canistor/storage/object.rb', line 10

def key
  @key
end

#last_modifiedObject (readonly)

Returns the value of attribute last_modified.



13
14
15
# File 'lib/canistor/storage/object.rb', line 13

def last_modified
  @last_modified
end

#regionObject

Returns the value of attribute region.



8
9
10
# File 'lib/canistor/storage/object.rb', line 8

def region
  @region
end

Instance Method Details

#delete(context, subject) ⇒ Object



81
82
83
# File 'lib/canistor/storage/object.rb', line 81

def delete(context, subject)
  context.http_response.signal_headers(200, {})
end

#digestObject



37
38
39
# File 'lib/canistor/storage/object.rb', line 37

def digest
  @digest ||= Digest::SHA1.hexdigest(data)
end

#etagObject



41
42
43
# File 'lib/canistor/storage/object.rb', line 41

def etag
  '"' + digest + '"'
end

#get(context, subject) ⇒ Object



67
68
69
70
# File 'lib/canistor/storage/object.rb', line 67

def get(context, subject)
  context.http_response.signal_headers(200, headers)
  context.http_response.signal_data(data)
end

#head(context, subject) ⇒ Object



63
64
65
# File 'lib/canistor/storage/object.rb', line 63

def head(context, subject)
  context.http_response.signal_headers(200, headers)
end

#headersObject



45
46
47
48
49
50
51
52
# File 'lib/canistor/storage/object.rb', line 45

def headers
  @headers.merge(identity_headers).merge(
    'date' => Time.now.httpdate,
    'content-length' => size.to_s,
    'last-modified' => last_modified.httpdate,
    'server' => 'Canistor'
  )
end

#identity_headersObject



54
55
56
57
58
59
60
61
# File 'lib/canistor/storage/object.rb', line 54

def identity_headers
  {
    'x-amz-request-id' => Base64.strict_encode64(SecureRandom.hex(16)),
    'x-amz-id' => digest[0, 16],
    'x-amz-id-2' => Base64.strict_encode64(digest),
    'etag' => etag
  }
end

#labelObject



28
29
30
# File 'lib/canistor/storage/object.rb', line 28

def label
  [region, bucket, key].map(&:to_s).join(':') + ' ' + headers.inspect
end

#put(context, subject) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/canistor/storage/object.rb', line 72

def put(context, subject)
  catch(:rendered_error) do
    source_object = source_object(context, subject)
    self.data = object_data(context, source_object)
    self.headers = object_headers(context, source_object)
    context.http_response.signal_headers(200, identity_headers)
  end
end

#sizeObject



24
25
26
# File 'lib/canistor/storage/object.rb', line 24

def size
  data&.size
end

#write(headers, data) ⇒ Object



32
33
34
35
# File 'lib/canistor/storage/object.rb', line 32

def write(headers, data)
  self.headers = headers
  self.data = data
end