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

#copy_to_store!(file, target_store) ⇒ Object



48
49
50
# File 'lib/remote_files/file_store.rb', line 48

def copy_to_store!(file, target_store)
  FileUtils.cp(directory + file.identifier, target_store.directory + file.identifier)
end

#delete!(identifier) ⇒ Object



67
68
69
70
71
# File 'lib/remote_files/file_store.rb', line 67

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



81
82
83
# File 'lib/remote_files/file_store.rb', line 81

def directory_name
  options[:directory].to_s
end

#files(prefix = '') ⇒ Object



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

def files(prefix = '')
  dir = directory + prefix

  return [] unless dir.exist?

  dir.children.reject do |child|
    child.directory?
  end.map do |child|
    File.new(child.basename.to_s,
             :stored_in => [self],
             :last_update_ts => child.mtime
    )
  end
end

#retrieve!(identifier) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/remote_files/file_store.rb', line 52

def retrieve!(identifier)
  path = directory + identifier

  content = (path).read

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

#store!(file) ⇒ Object



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

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



73
74
75
# File 'lib/remote_files/file_store.rb', line 73

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

#url_matcherObject



77
78
79
# File 'lib/remote_files/file_store.rb', line 77

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