Module: AssetCloud::FreeKeyLocator

Defined in:
lib/asset_cloud/free_key_locator.rb

Instance Method Summary collapse

Instance Method Details

#find_free_key_like(key, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/asset_cloud/free_key_locator.rb', line 7

def find_free_key_like(key, options = {})
  # Check weather the suggested key name is free. If so we
  # simply return it.

  if !options[:force_uuid] && !exist?(key)
    key
  else
    ext         = File.extname(key)
    dirname     = File.dirname(key)
    base        = dirname == "." ? File.basename(key, ext) : File.join(File.dirname(key), File.basename(key, ext))
    base        = base.gsub(/_[\h]{8}-[\h]{4}-4[\h]{3}-[\h]{4}-[\h]{12}/, "")

    # Attach UUID to avoid name collision
    key = "#{base}_#{SecureRandom.uuid}#{ext}"
    return key unless exist?(key)

    raise StandardError, "Filesystem out of free filenames"
  end
end