Class: Bosh::Blobstore::SimpleBlobstoreServer

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/simple_blobstore_server.rb

Constant Summary collapse

BUFFER_SIZE =
16 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SimpleBlobstoreServer

Returns a new instance of SimpleBlobstoreServer.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simple_blobstore_server.rb', line 17

def initialize(config)
  super
  @path = config["path"]
  @nginx_path = config["nginx_path"]

  if File.exist?(@path)
    raise "Invalid path" unless File.directory?(@path)
  else
    FileUtils.mkdir_p(@path)
  end

  raise "Invalid user list" unless config["users"].kind_of?(Hash)
  @users = Set.new
  config["users"].each do |username, password|
    @users << [username, password]
  end
  raise "Must have at least one user" if @users.empty?
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/simple_blobstore_server.rb', line 57

def authorized?
  @auth ||=  Rack::Auth::Basic::Request.new(request.env)
  @auth.provided? && @auth.basic? && @auth.credentials && @users.include?(@auth.credentials)
end

#create(params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/simple_blobstore_server.rb', line 77

def create(params)
  if params[:content] && params[:content][:tempfile]
    # Process uploads coming directly to the simple blobstore
    create_file(params[:id]) do |file_name|
      tempfile = params[:content][:tempfile]
      FileUtils.copy_file(tempfile.path, file_name)
    end
  elsif params["content.name"] && params["content.path"]
    # Process uploads arriving via nginx
    create_file(params[:id]) do |file_name|
      FileUtils.mv(params["content.path"], file_name)
    end
  else
    error(400)
  end

end

#create_file(object_id) {|file_name| ... } ⇒ Object

Yields:

  • (file_name)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/simple_blobstore_server.rb', line 62

def create_file(object_id)
  object_id ||= generate_object_id
  file_name = get_file_name(object_id)

  error(409) if File.exist?(file_name)

  FileUtils.mkdir_p(File.dirname(file_name))

  yield file_name

  status(200)
  content_type(:text)
  object_id
end

#generate_object_idObject



46
47
48
# File 'lib/simple_blobstore_server.rb', line 46

def generate_object_id
  SecureRandom.uuid
end

#get_file_name(object_id) ⇒ Object



36
37
38
39
# File 'lib/simple_blobstore_server.rb', line 36

def get_file_name(object_id)
  sha1 = Digest::SHA1.hexdigest(object_id)
  File.join(@path, sha1[0, 2], object_id)
end

#get_nginx_path(object_id) ⇒ Object



41
42
43
44
# File 'lib/simple_blobstore_server.rb', line 41

def get_nginx_path(object_id)
  sha1 = Digest::SHA1.hexdigest(object_id)
  "#{@nginx_path}/#{sha1[0, 2]}/#{object_id}"
end

#protected!Object



50
51
52
53
54
55
# File 'lib/simple_blobstore_server.rb', line 50

def protected!
  unless authorized?
    response['WWW-Authenticate'] = %(Basic realm="Authenticate")
    throw(:halt, [401, "Not authorized\n"])
  end
end