Class: Dragonfly::DataStorage::FileDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/data_storage/file_data_store.rb

Instance Method Summary collapse

Instance Method Details

#destroy(relative_path) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 47

def destroy(relative_path)
  path = absolute(relative_path)
  FileUtils.rm path
  FileUtils.rm extra_data_path(path)
  purge_empty_directories(relative_path)
rescue Errno::ENOENT => e
  raise DataNotFound, e.message
end

#disambiguate(path) ⇒ Object



56
57
58
59
60
61
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 56

def disambiguate(path)
  dirname = File.dirname(path)
  basename = File.basename(path, '.*')
  extname = File.extname(path)
  "#{dirname}/#{basename}_#{Time.now.usec.to_s(32)}#{extname}"
end

#retrieve(relative_path) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 35

def retrieve(relative_path)
  path = absolute(relative_path)
  file = File.new(path)
  file.close
  [
    file,
    retrieve_extra_data(path)
  ]
rescue Errno::ENOENT => e
  raise DataNotFound, e.message
end

#store(temp_object, opts = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 12

def store(temp_object, opts={})
  relative_path = if opts[:path]
    opts[:path]
  else
    filename = temp_object.name || 'file'
    relative_path = relative_path_for(filename)
  end

  begin
    path = absolute(relative_path)
    until !File.exist?(path)
      path = disambiguate(path)
    end
    prepare_path(path)
    temp_object.to_file(path).close
    store_extra_data(path, temp_object)
  rescue Errno::EACCES => e
    raise UnableToStore, e.message
  end

  relative(path)
end