Skip to content

Commit

Permalink
refactored the copy_object method to make it simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
Kieran Elliott committed Jul 1, 2012
1 parent c9a1dc9 commit 90af615
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions lib/fakes3/file_store.rb
Expand Up @@ -124,43 +124,22 @@ def object_metadata(bucket,object)
end

def copy_object(src_bucket_name,src_name,dst_bucket_name,dst_name)
src_root = File.join(@root,src_bucket_name,src_name,SHUCK_METADATA_DIR)
src_metadata_filename = File.join(src_root,"metadata")
src_metadata = YAML.load(File.open(src_metadata_filename,'rb').read)
src_content_filename = File.join(src_root,"content")

dst_filename= File.join(@root,dst_bucket_name,dst_name)
FileUtils.mkdir_p(dst_filename)

metadata_dir = File.join(dst_filename,SHUCK_METADATA_DIR)
FileUtils.mkdir_p(metadata_dir)

content = File.join(metadata_dir,"content")
metadata = File.join(metadata_dir,"metadata")
obj = nil
if src_bucket_name == dst_bucket_name && src_name == dst_name
# source and destination are the same, nothing to do but
# find current object so it can be returned
obj = src_bucket.find(src_name)
else
src_root = File.join(@root,src_bucket_name,src_name,SHUCK_METADATA_DIR)
dst_root = File.join(@root,dst_bucket_name,dst_name,SHUCK_METADATA_DIR)

File.open(content,'wb') do |f|
File.open(src_content_filename,'rb') do |input|
f << input.read
end
end
FileUtils.mkdir_p(dst_root)
FileUtils.copy_file(File.join(src_root,"content"),File.join(dst_root,"content"))
FileUtils.copy_file(File.join(src_root,"metadata"), File.join(dst_root,"metadata"))

File.open(metadata,'w') do |f|
File.open(src_metadata_filename,'r') do |input|
f << input.read
end
dst_bucket = self.get_bucket(dst_bucket_name)
dst_bucket.add(get_object(dst_bucket.name, dst_name, ""))
end

src_bucket = self.get_bucket(src_bucket_name)
dst_bucket = self.get_bucket(dst_bucket_name)

obj = S3Object.new
obj.name = dst_name
obj.md5 = src_metadata[:md5]
obj.content_type = src_metadata[:content_type]

src_obj = src_bucket.find(src_name)
dst_bucket.add(obj)
src_bucket.remove(src_obj)
return obj
end

Expand Down

0 comments on commit 90af615

Please sign in to comment.