Sha256: b0e4c800570221c3cbfb4b19dd473e023e372c2e465682773608d9e117a6cb00

Contents?: true

Size: 1.75 KB

Versions: 18

Compression:

Stored size: 1.75 KB

Contents

require 'fog/core/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 = ::File.read(path)
              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

18 entries across 18 versions & 2 rubygems

Version Path
fog-0.3.24 lib/fog/local/models/storage/files.rb
bbcloud-0.8.1 lib/bbcloud/vendor/fog-0.3.23/lib/fog/local/models/storage/files.rb
fog-0.3.23 lib/fog/local/models/storage/files.rb
fog-0.3.22 lib/fog/local/models/storage/files.rb
fog-0.3.21 lib/fog/local/models/storage/files.rb
fog-0.3.20 lib/fog/local/models/storage/files.rb
fog-0.3.19 lib/fog/local/models/storage/files.rb
fog-0.3.18 lib/fog/local/models/storage/files.rb
fog-0.3.17 lib/fog/local/models/storage/files.rb
fog-0.3.16 lib/fog/local/models/storage/files.rb
fog-0.3.15 lib/fog/local/models/storage/files.rb
fog-0.3.14 lib/fog/local/models/storage/files.rb
fog-0.3.13 lib/fog/local/models/storage/files.rb
fog-0.3.12 lib/fog/local/models/storage/files.rb
fog-0.3.11 lib/fog/local/models/storage/files.rb
fog-0.3.10 lib/fog/local/models/storage/files.rb
fog-0.3.9 lib/fog/local/models/storage/files.rb
fog-0.3.8 lib/fog/local/models/storage/files.rb