Class: Bit

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/sinatra-s3/models/bit.rb

Direct Known Subclasses

Bucket, Slot

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.acl_textObject



70
71
72
# File 'lib/sinatra-s3/models/bit.rb', line 70

def self.acl_text
  { 0 => "NONE", 1 => "NONE", 2 => "NONE", 3 => "NONE", 4 => "READ", 5 => "READ_ACP", 6 => "WRITE", 7 => "WRITE_ACP" }
end

.diff(from, to) ⇒ Object



44
45
46
47
48
# File 'lib/sinatra-s3/models/bit.rb', line 44

def self.diff(from,to)
  from = Bit.find_by_version(from) if from.instance_of?(String)
  to = Bit.find_by_version(to) if to.instance_of?(String)
  from.git_repository.diff(from.objectish, to.objectish)
end

Instance Method Details

#access_readableObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sinatra-s3/models/bit.rb', line 85

def access_readable
  name, _ = S3::CANNED_ACLS.find { |k, v| v == self.access }
  if name
    name
  else
    [0100, 0010, 0001].map do |i|
  [[4, 'r'], [2, 'w'], [1, 'x']].map do |k, v|
 (self.access & (i * k) == 0 ? '-' : v )
  end
    end.join
  end
end

#acl_label(num) ⇒ Object



74
75
76
# File 'lib/sinatra-s3/models/bit.rb', line 74

def acl_label(num)
  Bit.acl_text[num.to_i]
end

#acl_listObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/sinatra-s3/models/bit.rb', line 50

def acl_list
  bit_perms = self.access.nil? ? "000" : self.access.to_s(8)
  acls = { :owner => { :id => self.owner.key, :accessnum => 7, :type => "CanonicalUser", :name => self.owner., :access => "FULL_ACCESS" },
    :anonymous => { :id => nil, :accessnum => bit_perms[2,1], :access => acl_label(bit_perms[2,1]),
  :type => "Group", :uri => "http://acs.amazonaws.com/groups/global/AllUsers" },
  :authenticated => { :id => nil, :accessnum => bit_perms[1,1], :access => acl_label(bit_perms[1,1]),
 :type => "Group", :uri => "http://acs.amazonaws.com/groups/global/AuthenticatedUsers" }
  }.merge(get_acls_for_bin)
  acls.delete_if { |key,value| value[:access] == "NONE" || (key == :authenticated && (!acls[:anonymous].nil? && value[:accessnum] <= acls[:anonymous][:accessnum])) }
end

#acp_readable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
# File 'lib/sinatra-s3/models/bit.rb', line 98

def acp_readable_by? user
  # if owner
  return true if user && user == owner
  # if can write or better
  return true if user && acl_list[user.key] && acl_list[user.key][:accessnum].to_i >= 5
  # if authenticated
  return true if user && acl_list[:authenticated] && acl_list[:authenticated][:accessnum].to_i >= 5
  # if anonymous
  return true if acl_list[:anonymous] && acl_list[:anonymous][:accessnum].to_i >= 5
end

#acp_writable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
# File 'lib/sinatra-s3/models/bit.rb', line 109

def acp_writable_by? user
  # if owner
  return true if user && user == owner
  # if can write or better
  return true if user && acl_list[user.key] && acl_list[user.key][:accessnum].to_i == 7
  # if authenticated
  return true if user && acl_list[:authenticated] && acl_list[:authenticated][:accessnum].to_i == 7
  # if anonymous
  return true if acl_list[:anonymous] && acl_list[:anonymous][:accessnum].to_i == 7
end

#check_access(user, group_perm, user_perm) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/sinatra-s3/models/bit.rb', line 130

def check_access user, group_perm, user_perm
  !!( if owned_by?(user) or (user and access & group_perm > 0) or (access & user_perm > 0)
     true
  elsif user
    acl = users.find(user.id) rescue nil
    acl and acl.access.to_i & user_perm
  end )
end

#diff(to) ⇒ Object



39
40
41
42
# File 'lib/sinatra-s3/models/bit.rb', line 39

def diff(to)
  to = Bit.find_by_version(to) if to.instance_of?(String)
  git_repository.diff(objectish, to.objectish)
end

#each_piece(files, length) {|buf| ... } ⇒ Object

Yields:

  • (buf)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/sinatra-s3/models/bit.rb', line 157

def each_piece(files, length)
   buf = ""
   files.each do |f|
       File.open(f) do |fh|
           begin
               read = fh.read(length - buf.length)
               if (buf.length + read.length) == length
                   yield(buf + read)
                   buf = ""
               else
                   buf += read
               end
           end until fh.eof?
       end
   end
   yield buf
end

#fullpathObject



19
# File 'lib/sinatra-s3/models/bit.rb', line 19

def fullpath; File.join(S3::STORAGE_PATH, name) end

#get_acls_for_binObject



61
62
63
64
65
66
67
68
# File 'lib/sinatra-s3/models/bit.rb', line 61

def get_acls_for_bin
  ret = {}
  for a in self.bits_users
    ret[a.user.key] = { :type => "CanonicalUser", :id => a.user.key, :name => a.user., :access => acl_label(a.access.to_s(8)[0,1]),
  :accessnum => a.access.to_s(8)[0,1] }
  end
  ret
end

#git_objectObject



31
32
33
# File 'lib/sinatra-s3/models/bit.rb', line 31

def git_object
  git_repository.log.path(File.basename(self.obj.path)).first if versioning_enabled? && self.obj
end

#git_repositoryObject



15
16
17
# File 'lib/sinatra-s3/models/bit.rb', line 15

def git_repository
  versioning_enabled? ? Git.open(git_repository_path) : nil
end

#git_repository_pathObject



21
22
23
# File 'lib/sinatra-s3/models/bit.rb', line 21

def git_repository_path
  self.obj ? File.join(File.dirname(self.fullpath)) : self.fullpath
end

#git_updateObject



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sinatra-s3/models/bit.rb', line 143

def git_update
  # update git info so we can serve it over http
  base_dir = File.join(self.git_repository_path,'.git')
  if File.exists?(base_dir)
    File.open(File.join(base_dir,'HEAD'),'w') { |f| f.write("ref: refs/heads/master") }
    if File.exists?(File.join(base_dir,'refs/heads/master'))
      File.open(File.join(base_dir,'info/refs'),'w') { |f|
    ref = File.open(File.join(base_dir,'refs/heads/master')) { |re| re.read }
 f.write("#{ref.chomp}\trefs/heads/master\n")
      }
    end
  end
end

#grant(hsh) ⇒ Object



78
79
80
81
82
83
# File 'lib/sinatra-s3/models/bit.rb', line 78

def grant hsh
  if hsh[:access]
    self.access = hsh[:access]
    self.save
  end
end

#objectishObject



35
36
37
# File 'lib/sinatra-s3/models/bit.rb', line 35

def objectish
  git_repository.gcommit(version).gtree.blobs[File.basename(fullpath)].objectish
end

#owned_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/sinatra-s3/models/bit.rb', line 139

def owned_by? user
  user and owner_id == user.id
end

#readable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/sinatra-s3/models/bit.rb', line 120

def readable_by? user
  return true if user && acl_list[user.key] && acl_list[user.key][:accessnum].to_i >= 4
  check_access(user, S3::READABLE_BY_AUTH, S3::READABLE)
end

#versioning_enabled?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/sinatra-s3/models/bit.rb', line 25

def versioning_enabled?
  return false if !defined?(Git) || !File.exists?(File.join(git_repository_path,'.git'))
  return false if File.exists?(File.join(git_repository_path,'.git', 'disable_versioning'))
  return true
end

#writable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
# File 'lib/sinatra-s3/models/bit.rb', line 125

def writable_by? user
  return true if user && acl_list[user.key] && acl_list[user.key][:accessnum].to_i >= 6
  check_access(user, S3::WRITABLE_BY_AUTH, S3::WRITABLE)
end