Class: Whoosh::Storage::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/storage/local.rb

Instance Method Summary collapse

Constructor Details

#initialize(root:) ⇒ Local

Returns a new instance of Local.



10
11
12
# File 'lib/whoosh/storage/local.rb', line 10

def initialize(root:)
  @root = root
end

Instance Method Details

#save(uploaded_file, prefix = "") ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/whoosh/storage/local.rb', line 14

def save(uploaded_file, prefix = "")
  dir = prefix.empty? ? @root : File.join(@root, prefix)
  FileUtils.mkdir_p(dir)
  filename = "#{SecureRandom.uuid}_#{uploaded_file.filename}"
  path = File.join(dir, filename)
  File.open(path, "wb") { |f| f.write(uploaded_file.read) }
  prefix.empty? ? filename : File.join(prefix, filename)
end