class BrowseEverything::Driver::FileSystem

Public Instance Methods

authorized?() click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 39
def authorized?
  true
end
contents(path = '') click to toggle source

Retrieve the contents of a directory @param path [String] the path to a file system resource @return [Array<BrowseEverything::FileEntry>]

# File lib/browse_everything/driver/file_system.rb, line 21
def contents(path = '')
  real_path = File.join(home_path, path)
  values = if File.directory?(real_path)
             make_directory_entry real_path
           else
             [details(real_path)]
           end
  @entries = values.compact

  @sorter.call(@entries)
end
details(path, display = File.basename(path)) click to toggle source

Construct a FileEntry objects for a file-system resource @param path [String] path to the file @param display [String] display label for the resource @return [BrowseEverything::FileEntry]

# File lib/browse_everything/driver/file_system.rb, line 47
def details(path, display = File.basename(path))
  return nil unless File.exist? path
  info = File::Stat.new(path)
  BrowseEverything::FileEntry.new(
    make_pathname(path),
    [key, path].join(':'),
    display,
    info.size,
    info.mtime,
    info.directory?
  )
end
icon() click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 6
def icon
  'file'
end
validate_config() click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 10
def validate_config
  raise BrowseEverything::InitializationError, 'FileSystem driver requires a :home argument' if config[:home].blank?

  unless config[:home].start_with?("/") || config[:allow_relative_home] == true
    raise BrowseEverything::InitializationError, 'FileSystem driver :home argument must be absolute unless :allow_relative_home is set'
  end
end

Private Instance Methods

file_size(path) click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 84
def file_size(path)
  File.size(path).to_i
rescue StandardError => error
  Rails.logger.error "Failed to find the file size for #{path}: #{error}"
  0
end
home_path() click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 62
def home_path
  @home_path ||= if config[:allow_relative_home] == true
                   # expand relative to Rails.root, mainly test CI use-case
                   File.expand_path(config[:home], Rails.root)
                 else
                   config[:home]
                 end
end
make_directory_entry(real_path) click to toggle source

Construct an array of FileEntry objects for the contents of a directory @param real_path [String] path to the file system directory @return [Array<BrowseEverything::FileEntry>]

# File lib/browse_everything/driver/file_system.rb, line 75
def make_directory_entry(real_path)
  entries = []
  entries + Dir[File.join(real_path, '*')].collect { |f| details(f) }
end
make_pathname(path) click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 80
def make_pathname(path)
  Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(home_path))
end