Class: Dragonfly::DataStorage::FileDataStore

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

Defined Under Namespace

Classes: UnableToFormUrl

Instance Method Summary collapse

Instance Method Details

#destroy(relative_path) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 51

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

#disambiguate(path) ⇒ Object



74
75
76
77
78
79
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 74

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

#retrieve(relative_path) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 41

def retrieve(relative_path)
  path = absolute(relative_path)
  pathname = Pathname.new(path)
  raise DataNotFound, "couldn't find file #{path}" unless pathname.exist?
  [
    pathname,
    (store_meta ? (path) : {})
  ]
end

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 17

def store(temp_object, opts={})
  meta = opts[:meta] || {}
  relative_path = if opts[:path]
    opts[:path]
  else
    filename = meta[:name] || temp_object.original_filename || '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
    (path, meta) if store_meta
  rescue Errno::EACCES => e
    raise UnableToStore, e.message
  end

  relative(path)
end

#url_for(relative_path, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dragonfly/data_storage/file_data_store.rb', line 61

def url_for(relative_path, opts={})
  if server_root.nil?
    raise NotConfigured, "you need to configure server_root for #{self.class.name} in order to form urls"
  else
    _, __, path = absolute(relative_path).partition(server_root)
    if path.empty?
      raise UnableToFormUrl, "couldn't form url for uid #{relative_path.inspect} with root_path #{root_path.inspect} and server_root #{server_root.inspect}"
    else
      path
    end
  end
end