File: put_object_acl.rb

package info (click to toggle)
ruby-fog-google 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,568 kB
  • sloc: ruby: 16,775; makefile: 3
file content (58 lines) | stat: -rw-r--r-- 1,731 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Fog
  module Storage
    class GoogleXML
      class Real
        # TODO: move this methods to helper to use them with put_bucket_acl request
        def tag(name, value)
          "<#{name}>#{value}</#{name}>"
        end

        def scope_tag(scope)
          if %w(AllUsers AllAuthenticatedUsers).include?(scope["type"])
            "<Scope type='#{scope['type']}'/>"
          else
            "<Scope type='#{scope['type']}'>" +
              scope.to_a.reject { |pair| pair[0] == "type" }.map { |pair| tag(pair[0], pair[1]) }.join("\n") +
              "</Scope>"
          end
        end

        def entries_list(access_control_list)
          access_control_list.map do |entry|
            tag("Entry", scope_tag(entry["Scope"]) + tag("Permission", entry["Permission"]))
          end.join("\n")
        end

        def put_object_acl(bucket_name, object_name, acl)
          headers = {}
          data = ""

          if acl.is_a?(Hash)
            data = <<-DATA
<AccessControlList>
  <Owner>
    #{tag('ID', acl['Owner']['ID'])}
  </Owner>
  <Entries>
    #{entries_list(acl['AccessControlList'])}
  </Entries>
</AccessControlList>
DATA
          elsif acl.is_a?(String) && Utils::VALID_ACLS.include?(acl)
            headers["x-goog-acl"] = acl
          else
            raise Excon::Errors::BadRequest.new("invalid x-goog-acl")
          end

          request(:body     => data,
                  :expects  => 200,
                  :headers  => headers,
                  :host     => "#{bucket_name}.#{@host}",
                  :method   => "PUT",
                  :query    => { "acl" => nil },
                  :path     => Fog::Google.escape(object_name))
        end
      end
    end
  end
end