Class: OMF::Web::FileContentRepository

Inherits:
ContentRepository show all
Defined in:
lib/omf-web/content/file_repository.rb

Overview

This class provides an interface to a directory based repository It retrieves, archives and versions content.

Constant Summary

Constants inherited from ContentRepository

ContentRepository::MIME_TYPE, ContentRepository::REPO_PLUGINS

Instance Attribute Summary

Attributes inherited from ContentRepository

#name, #top_dir

Instance Method Summary collapse

Methods inherited from ContentRepository

#_get_path, #absolute_path, absolute_path_for, create, create_content_proxy_for, #create_content_proxy_for, create_url, #exist?, find_files, find_repo_for, #get_url_for_path, #mime_type_for_file, #path, #read, read_content, #read_only?, reference_dir, reference_dir=, register_mime_type, register_repo, #to_s

Constructor Details

#initialize(name, opts) ⇒ FileContentRepository

Returns a new instance of FileContentRepository.



15
16
17
18
# File 'lib/omf-web/content/file_repository.rb', line 15

def initialize(name, opts)
  super
  #@url_prefix = "file:#{name}:"
end

Instance Method Details

#find_files(search_pattern, opts = {}) ⇒ Object

Return an array of file names which are in the repository and match ‘search_pattern’



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omf-web/content/file_repository.rb', line 39

def find_files(search_pattern, opts = {})
  Dir.chdir(@top_dir)
  Dir.glob("**/*#{search_pattern}*").map do |path|
    next if File.directory?(path)
    mt = mime_type_for_file(path)
    {
      name: path,
      url: get_url_for_path(path),
      mime_type: mt,
      size: File.size(path)
    }
  end.compact
end

#write(content_descr, content, message) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/omf-web/content/file_repository.rb', line 20

def write(content_descr, content, message)
  raise ReadOnlyContentRepositoryException.new if @read_only

  path = _get_path(content_descr)
  Dir.chdir(@top_dir) do
    d_name = File.dirname(path)
    FileUtils.mkpath(d_name) unless File.exist?(d_name)
    unless File.writable?(path) || File.writable?(d_name)
      raise "Cannot write to file '#{path}'"
    end
    f = File.open(path, 'w')
    f.write(content)
    f.close
  end
end