Sha256: b1a7fcbcd296c9c6317606e98cb5644acdb643d235d115585b0580e629658729

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

require 'fog/collection'
require 'fog/local/models/storage/file'

module Fog
  module Local
    class Storage

      class Files < Fog::Collection

        attribute :directory

        model Fog::Local::Storage::File

        def all
          requires :directory
          if directory.collection.get(directory.key)
            data = Dir.entries(connection.path_to(directory.key)).select do |key|
              key[0...1] != '.' && !::File.directory?(connection.path_to(key))
            end.map do |key|
              path = file_path(key)
              {
                :content_length => ::File.size(path),
                :key            => key,
                :last_modified  => ::File.mtime(path)
              }
            end
            load(data)
          else
            nil
          end
        end

        def get(key, &block)
          requires :directory
          path = file_path(key)
          if ::File.exists?(path)
            data = {
              :content_length => ::File.size(path),
              :key            => key,
              :last_modified  => ::File.mtime(path)
            }
            if block_given?
              file = ::File.open(path)
              while (chunk = file.read(Excon::CHUNK_SIZE)) && yield(chunk); end
              file.close
              new(data)
            else
              body = nil
              ::File.open(path) do |file|
                body = file.read
              end
              new(data.merge!(:body => body))
            end
          else
            nil
          end
        end

        def new(attributes = {})
          requires :directory
          super({ :directory => directory }.merge!(attributes))
        end

        private

        def file_path(key)
          connection.path_to(::File.join(directory.key, key))
        end

      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fog-0.3.7 lib/fog/local/models/storage/files.rb
fog-0.3.6 lib/fog/local/models/storage/files.rb
fog-0.3.5 lib/fog/local/models/storage/files.rb
fog-0.3.4 lib/fog/local/models/storage/files.rb
fog-0.3.3 lib/fog/local/models/storage/files.rb
fog-0.3.2 lib/fog/local/models/storage/files.rb
fog-0.3.1 lib/fog/local/models/storage/files.rb
fog-0.3.0 lib/fog/local/models/storage/files.rb