class BrowseEverything::Driver::FileSystem
Public Instance Methods
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 17 def contents(path = '') real_path = File.join(config[:home], 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 43 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
link_for(path)
click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 29 def link_for(path) full_path = File.expand_path(path) file_size = file_size(full_path) ["file://#{full_path}", { file_name: File.basename(path), file_size: file_size }] 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? end
Private Instance Methods
file_size(path)
click to toggle source
# File lib/browse_everything/driver/file_system.rb, line 71 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
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 62 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 67 def make_pathname(path) Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(config[:home])) end