Sha256: f3b0295364c4990bf3635b305d7a946f4abdaf76a078c06108f2321c193b3ccc

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module BrowseEverything
  module Driver
    class FileSystem < Base
      def icon
        'file'
      end

      def validate_config
        raise BrowseEverything::InitializationError, 'FileSystem driver requires a :home argument' if config[:home].blank?
      end

      def contents(path = '')
        real_path = File.join(config[:home], path)
        @entries = if File.directory?(real_path)
                     make_directory_entry real_path
                   else
                     [details(real_path)]
                   end

        @sorter.call(@entries)
      end

      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

      def authorized?
        true
      end

      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

      private

        def make_directory_entry(real_path)
          entries = []
          entries + Dir[File.join(real_path, '*')].collect { |f| details(f) }
        end

        def make_pathname(path)
          Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(config[:home]))
        end

        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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
browse-everything-1.0.0 lib/browse_everything/driver/file_system.rb
browse-everything-1.0.0.rc2 lib/browse_everything/driver/file_system.rb
browse-everything-1.0.0.rc1 lib/browse_everything/driver/file_system.rb
browse-everything-0.16.1 lib/browse_everything/driver/file_system.rb
browse-everything-0.16.0 lib/browse_everything/driver/file_system.rb