Class: RemoteFiles::FileStore

Inherits:
AbstractStore show all
Defined in:
lib/remote_files/file_store.rb

Instance Attribute Summary

Attributes inherited from AbstractStore

#identifier

Instance Method Summary collapse

Methods inherited from AbstractStore

#[]=, #file_from_url, #initialize, #options, #read_only?, #to_sym

Constructor Details

This class inherits a constructor from RemoteFiles::AbstractStore

Instance Method Details

#delete!(identifier) ⇒ Object



45
46
47
48
49
# File 'lib/remote_files/file_store.rb', line 45

def delete!(identifier)
  (directory + identifier).delete
rescue Errno::ENOENT
  raise NotFoundError, $!.message, $!.backtrace
end

#directoryObject



9
10
11
12
13
14
# File 'lib/remote_files/file_store.rb', line 9

def directory
  @directory ||= Pathname.new(options[:directory]).tap do |dir|
    dir.mkdir unless dir.exist?
    raise "#{dir} is not a directory" unless dir.directory?
  end
end

#directory_nameObject



59
60
61
# File 'lib/remote_files/file_store.rb', line 59

def directory_name
  options[:directory].to_s
end

#retrieve!(identifier) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/remote_files/file_store.rb', line 33

def retrieve!(identifier)
  content = (directory + identifier).read

  RemoteFiles::File.new(identifier,
    :content      => content,
    :stored_in    => [self]
    # what about content-type? maybe use the mime-types gem?
  )
rescue Errno::ENOENT
  raise NotFoundError, $!.message, $!.backtrace
end

#store!(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/remote_files/file_store.rb', line 16

def store!(file)
  file_name = directory + file.identifier

  FileUtils.mkdir_p(file_name.parent)

  file_name.open('wb') do |f|
    if file.content.respond_to?(:read)
      while blk = file.content.read(2048)
        f << blk
      end
    else
      f.write(file.content)
      # what about content-type?
    end
  end
end

#url(identifier) ⇒ Object



51
52
53
# File 'lib/remote_files/file_store.rb', line 51

def url(identifier)
  "file://localhost#{directory + identifier}"
end

#url_matcherObject



55
56
57
# File 'lib/remote_files/file_store.rb', line 55

def url_matcher
  @url_matcher ||= /file:\/\/localhost#{directory}\/(.*)/
end