Skip to content

Commit

Permalink
More operations
Browse files Browse the repository at this point in the history
  • Loading branch information
kuon committed Nov 26, 2014
1 parent 3b87f82 commit 316d825
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 18 deletions.
27 changes: 27 additions & 0 deletions lib/swift_storage/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ class SwiftStorage::Account < SwiftStorage::Node
parent_node :service


# Write account meta data
#
# @param temp_url_key [String]
# The shared secret used to sign temporary URLs.
# Changing this key will invalidate all temporary URLs signed with the older
# key.
#
def write(temp_url_key: nil)
h = {}
h[H::ACCOUNT_TEMP_URL_KEY] = temp_url_key if temp_url_key

request(relative_path, :method => :post, :headers => h)
end

# Returns the temporary URL key
#
# @return [String]
# Key used to sign temporary URLs
#
def temp_url_key
metadata.temp_url_key rescue nil
end

def relative_path
''
end


end

22 changes: 22 additions & 0 deletions lib/swift_storage/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,27 @@ def objects
@objects ||= SwiftStorage::ObjectCollection.new(self)
end

def create
request(relative_path, :method => :put)
end

def write(write_acl: nil, read_acl: nil)
h = {}
read_acl = read_acl.join(',') if read_acl.respond_to?(:to_ary)
write_acl = write_acl.join(',') if write_acl.respond_to?(:to_ary)

h[H::CONTAINER_READ] = read_acl
h[H::CONTAINER_WRITE] = write_acl

request(relative_path, :method => :post, :headers => h)
end

def acl
r = headers.read.split(',') rescue nil
w = headers.write.split(',') rescue nil
struct(:read => r, :write => w)
end


end

4 changes: 4 additions & 0 deletions lib/swift_storage/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module Headers
DELETE_AFTER = 'X-Delete-After'.freeze
CACHE_CONTROL = 'Cache-Control'.freeze
EXPIRES = 'Expires'.freeze
OBJECT_MANIFEST = 'X-Object-Manifest'.freeze
CONTAINER_READ = 'X-Container-Read'.freeze
CONTAINER_WRITE = 'X-Container-Write'.freeze
ACCOUNT_TEMP_URL_KEY = 'X-Account-Meta-Temp-URL-Key'.freeze
end

end
12 changes: 8 additions & 4 deletions lib/swift_storage/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ def clear_cache
@metadata = nil
end

private
def struct(h)
return nil if h.empty?
Struct.new(*h.keys.map(&:to_sym)).new(*h.values)
def exists?
request(relative_path, :method => :head) && true
rescue SwiftStorage::Errors::NotFoundError
false
end

def delete
request(relative_path, :method => :delete)
end


Expand Down
50 changes: 36 additions & 14 deletions lib/swift_storage/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
# @attr [String] content_type
# Content type of the Object, eg: `image/png`.
#
# @attr [String] expires
# When the object is set to expire.
#
# @attr [String] cache_control
# Object cache control header.
#
class SwiftStorage::Object < SwiftStorage::Node

include SwiftStorage

parent_node :container


header_attributes :content_length,
:content_type
:content_type,
:expires,
:cache_control

# Read the object data
#
Expand Down Expand Up @@ -43,7 +50,10 @@ def read(output_stream=nil)
# This will always make a request to the API server and will not use cache
#
# @note If you want to only update the metadata, you may omit `input_stream`
# but you must specify all other options otherwise they will be overwritten.
# but you must specify all other options otherwise they will be overwritten.
#
# @note Some headers specified here may not work with a specific swift server
# as they must be enabled in the server configuration.
#
# @param input_stream [String, IO]
# The data to upload, if ommited, the write will not override the body and instead it will update
Expand Down Expand Up @@ -71,6 +81,11 @@ def read(output_stream=nil)
# @param expires [Time]
# Set the Expires header.
#
# @param object_manifest [String]
# When set, this object acts as a large object manifest. The value should be
# `<container>/<prefix>` where `<container>` is the container the object
# segments are in and `<prefix>` is the common prefix for all the segments.
#
# @return [input_stream]
# Return the `input_stream` argument, or `nil` if `input_stream` is ommited.
#
Expand All @@ -81,29 +96,33 @@ def write(input_stream=nil,
delete_after: nil,
cache_control: nil,
expires: nil,
object_manifest: nil,
metadata: nil)

h = {
Headers::CONTENT_DISPOSITION => attachment ? 'attachment' : 'inline'
}
h = {}

input_stream.nil? or content_type or raise ArgumentError, 'Content_type is required if input_stream is given'

h[Headers::CONTENT_TYPE] = content_type if content_type
object_manifest.nil? or input_stream.nil? or raise ArgumentError, 'Input must be nil on object manigest'

h[H::CONTENT_DISPOSITION] = attachment ? 'attachment' : 'inline'
h[H::OBJECT_MANIFEST] = object_manifest if object_manifest
h[H::CONTENT_TYPE] = content_type if content_type
h[H::EXPIRES] = expires.httpdate if expires
h[H::CACHE_CONTROL] = cache_control if cache_control

if delete_at
h[Headers::DELETE_AT] = delete_at.to_i.to_s
h[H::DELETE_AT] = delete_at.to_i.to_s
elsif delete_after
h[Headers::DELETE_AFTER] = delete_after.to_i.to_s
h[H::DELETE_AFTER] = delete_after.to_i.to_s
end

h[Headers::EXPIRES] = expires.httpdate if expires
h[Headers::CACHE_CONTROL] = cache_control if cache_control

merge_metadata(h, metadata)

method = input_stream || object_manifest ? :put : :post

request(relative_path,
:method => (input_stream ? :put : :post),
:method => method,
:headers => h,
:input_stream => input_stream)
clear_cache
Expand All @@ -130,7 +149,7 @@ def temp_url(expires=nil, method: :get)

# Returns the object's URL
#
# @note This URL is unsigned and the container authorization will apply. If
# @note This URL is unsigneds and the container authorization will apply. If
# the container do not allow public access, this URL will require an
# authentication token.
#
Expand All @@ -142,6 +161,9 @@ def url
end

private

H = SwiftStorage::Headers

def relative_path
File.join(container.name, name)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/swift_storage/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ def sig_to_hex(str)
}.join
end

def struct(h)
return nil if h.empty?
Struct.new(*h.keys.map(&:to_sym)).new(*h.values)
end

end

0 comments on commit 316d825

Please sign in to comment.