Sha256: 816f32bcfec2987fab474cc674014d27f300f3688f58fd298d89b6aeb87e06cc

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'fog/core/collection'
require 'fog/aliyun/models/storage/directory'

module Fog
  module Storage
    class Aliyun
      class Directories < Fog::Collection
        model Fog::Storage::Aliyun::Directory

        def all
          containers = service.get_containers
          return nil if containers.nil?
          data = []
          i = 0
          containers.each do |entry|
            key = entry['Prefix'][0]
            key[-1] = ''
            data[i] = { key: key }
            i += 1
          end

          load(data)
        end

        # get method used to get a specified directory.
        # If the directory is not exist, this method will create a new with 'key'
        # In order to support multi-buckets scenario which making bucket as a solo directory, it have been expanded.
        # If key is a directory(including /), return an existed or a new one;
        # If key does not contain /, if bucket, return '', else return an existed or a new one directory;
        def get(key, options = {})
          if !key.nil? && key != '' && key != '.'
            key = key.chomp('/')
            if key.include? '/'
              dir = key + '/'
              ret = service.head_object(dir, options)
              new(key: key) if ret.data[:status] == 200
            else
              data = service.get_bucket(key)
              puts "[DEBUG] Getting the bucket named with #{key}..."
              puts data
              if data.class == Hash && data.key?('Code') && !data['Code'].nil? && !data['Code'].empty?
                dir = key + '/'
                ret = service.head_object(dir, options)
                new(key: key) if ret.data[:status] == 200
              else
                new(key: '')
              end
            end
          else
            new(key: '')
          end
        rescue Fog::Storage::Aliyun::NotFound
          nil
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fog-aliyun-0.3.7 lib/fog/aliyun/models/storage/directories.rb