Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
DeleteObjects (#154)
Browse files Browse the repository at this point in the history
* add delete objects

* add delete objects

* add delete objects

* Update gitignore
  • Loading branch information
pola88 authored and pickhardt committed Jun 12, 2017
1 parent 2d5d35d commit dcbe393
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ test_root
Gemfile.lock

.DS_Store
.rvmrc
root/*
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'
# Specify your gem's dependencies in fakes3.gemspec
gemspec
gemspec
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PATH
fakes3 (1.2.0)
builder
thor
xml-simple

GEM
remote: https://rubygems.org/
Expand Down
1 change: 1 addition & 0 deletions fakes3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |s|
#s.add_development_dependency "debugger"
s.add_dependency "thor"
s.add_dependency "builder"
s.add_dependency "xml-simple"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
17 changes: 17 additions & 0 deletions lib/fakes3/file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ def delete_object(bucket,object_name,request)
end
end

def delete_objects(bucket, objects, request)
begin
filenames = []
objects.each do |object_name|
filenames << File.join(@root,bucket.name,object_name)
object = bucket.find(object_name)
bucket.remove(object)
end

FileUtils.rm_rf(filenames)
rescue
puts $!
$!.backtrace.each { |line| puts line }
return nil
end
end

# TODO: abstract getting meta data from request.
def create_metadata(content, request)
metadata = {}
Expand Down
21 changes: 18 additions & 3 deletions lib/fakes3/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'fakes3/util'
require 'fakes3/file_store'
require 'fakes3/xml_adapter'
require 'fakes3/xml_parser'
require 'fakes3/bucket_query'
require 'fakes3/unsupported_operation'
require 'fakes3/errors'
Expand All @@ -26,6 +27,7 @@ class Request
MOVE = "MOVE"
DELETE_OBJECT = "DELETE_OBJECT"
DELETE_BUCKET = "DELETE_BUCKET"
DELETE_OBJECTS = "DELETE_OBJECTS"

attr_accessor :bucket, :object, :type, :src_bucket,
:src_object, :method, :webrick_request,
Expand Down Expand Up @@ -162,6 +164,7 @@ def do_GET(request, response)
end
end
response['Content-Length'] = File::Stat.new(real_obj.io.path).size
response['Content-Disposition'] = 'attachment'
if s_req.http_verb == 'HEAD'
response.body = ""
real_obj.io.close
Expand Down Expand Up @@ -240,6 +243,10 @@ def do_multipartPUT(request, response)
end

def do_POST(request,response)
if request.query_string === 'delete'
return do_DELETE(request, response)
end

s_req = normalize_request(request)
key = request.query['key']
query = CGI::parse(request.request_uri.query || "")
Expand Down Expand Up @@ -319,6 +326,10 @@ def do_DELETE(request, response)
s_req = normalize_request(request)

case s_req.type
when Request::DELETE_OBJECTS
bucket_obj = @store.get_bucket(s_req.bucket)
keys = XmlParser.delete_objects(s_req.webrick_request)
@store.delete_objects(bucket_obj,keys,s_req.webrick_request)
when Request::DELETE_OBJECT
bucket_obj = @store.get_bucket(s_req.bucket)
@store.delete_object(bucket_obj,s_req.object,s_req.webrick_request)
Expand All @@ -332,7 +343,6 @@ def do_DELETE(request, response)

def do_OPTIONS(request, response)
super

response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'PUT, POST, HEAD, GET, OPTIONS'
response['Access-Control-Allow-Headers'] = 'Accept, Content-Type, Authorization, Content-Length, ETag, X-CSRF-Token, Content-Disposition'
Expand All @@ -358,8 +368,9 @@ def normalize_delete(webrick_req, s_req)
if elems.size == 0
raise UnsupportedOperation
elsif elems.size == 1
s_req.type = Request::DELETE_BUCKET
s_req.type = webrick_req.query_string == 'delete' ? Request::DELETE_OBJECTS : Request::DELETE_BUCKET
s_req.query = query
s_req.webrick_request = webrick_req
else
s_req.type = Request::DELETE_OBJECT
object = elems[1,elems.size].join('/')
Expand Down Expand Up @@ -482,7 +493,11 @@ def normalize_request(webrick_req)
when 'DELETE'
normalize_delete(webrick_req,s_req)
when 'POST'
normalize_post(webrick_req,s_req)
if webrick_req.query_string != 'delete'
normalize_post(webrick_req,s_req)
else
normalize_delete(webrick_req,s_req)
end
else
raise "Unknown Request"
end
Expand Down
16 changes: 16 additions & 0 deletions lib/fakes3/xml_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'xmlsimple'

module FakeS3
class XmlParser
def self.delete_objects(request)
keys = []

objects = XmlSimple.xml_in(request.body, {'NoAttr' => true})['Object']
objects.each do |key|
keys << key['Key'][0]
end

keys
end
end
end

0 comments on commit dcbe393

Please sign in to comment.