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

#absolute_path, absolute_path_for, create_content_proxy_for, create_url, #find_files, find_files, find_repo_for, #mime_type_for_file, #path, #read, read_content, register_repo

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

#_get_path(content_descr) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/omf-web/content/file_repository.rb', line 69

def _get_path(content_descr)
  if content_descr.is_a? String
    path = content_descr.to_s
    if path.start_with? 'file:'
      path = path.split(':')[2]
    end
  elsif content_descr.is_a? Hash
    descr = content_descr
    if (url = descr[:url])
      path = url.split(':')[2] # git:repo_name:path
    else
      path = descr[:path]
    end
    unless path
      raise "Missing 'path' or 'url' in content description (#{descr.inspect})"
    end
    path = path.to_s
  else
    raise "Unsupported type '#{content_descr.class}'"
  end
  unless path
    raise "Can't find path information in '#{content_descr.inspect}'"
  end
  return path
end

#create_content_proxy_for(content_descr) ⇒ Object

Load content described by either a hash or a straightforward path and return a ‘ContentProxy’ holding it.

If descr is true, return nil if file for which proxy is requested already exists.

@return: Content proxy



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omf-web/content/file_repository.rb', line 28

def create_content_proxy_for(content_descr)
  path = _get_path(content_descr)
  # TODO: Make sure that key is really unique across multiple repositories
  descr = descr ? descr.dup : {}
  url = @url_prefix + path
  key = Digest::MD5.hexdigest(url)
  descr[:url] = url
  descr[:url_key] = key
  descr[:path] = path
  descr[:name] = url # Should be something human digestable
  if (descr[:strictly_new])
    Dir.chdir(@top_dir) do
      return nil if File.exist?(path)
    end
  end
  proxy = ContentProxy.create(descr, self)
  return proxy
end

#get_url_for_path(path) ⇒ Object

Return a URL for a path in this repo



65
66
67
# File 'lib/omf-web/content/file_repository.rb', line 65

def get_url_for_path(path)
  "file:#{path}"
end

#write(content_descr, content, message) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omf-web/content/file_repository.rb', line 47

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