Module: Iceberg

Defined in:
lib/ibg.rb,
lib/ibg/web.rb,
lib/ibg/storage.rb,
lib/ibg/version.rb

Defined Under Namespace

Classes: FileObject, Storage, WebApp

Constant Summary collapse

HOME_DIR =
ENV['HOME']
SETTING_DIR =
File.join(HOME_DIR, '.iceberg')
SETTING_FILE =
File.join(SETTING_DIR, 'settings.yaml')
SETTING =
YAML.load(File.read(SETTING_FILE))
IBDB_RECENT =
'iceberg:recent'
IBDB_TRIPCODE =
'iceberg:tripcode:'
IBDB_TRIPCODE_SET =
'iceberg:tripcode:set'
IBDB_TRIPCODE_FUND =
'iceberg:tripcode:fund:'
IBDB_RECENT_PEERS =
'iceberg:recentpeers'
IBDB_PEERS =
'iceberg:peers'
IBDB_SHORT_URL =
'iceberg:shorturl:'
ALGORITHM =
'AES-128-CBC'
REDIS =
if ENV['DB_PORT_6379_TCP_PORT']
  host = ENV['DB_PORT_6379_TCP_ADDR']
  port = ENV['DB_PORT_6379_TCP_PORT'].to_i
  Redis.new(:host => host, :port => port)
else
  Redis.new
end
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.download(name, filename, hexdigest) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ibg.rb', line 146

def self.download(name, filename, hexdigest)
  ctype, disp = case filename ? filename.downcase : nil
    when /\.jpg$/ ; ['image/jpeg', 'inline']
    when /\.png$/ ; ['image/png', 'inline']
    when /\.gif$/ ; ['image/gif', 'inline']
    when /\.mp3$/ ; ['audio/mpeg', 'inline']
    when /\.m4a$/ ; ['audio/mpeg', 'inline']
    when /\.ogg$/ ; ['audio/ogg', 'inline']
    when /\.flac$/ ; ['audio/x-flac', 'inline']
    when /\.alac$/ ; ['audio/alac', 'inline']
    when /\.webm$/ ; ['video/webm', 'inline']
    when /\.txt$/ ; ['text/plain', 'inline']
    else ['application/octet-stream', 'attachment']
  end
  if hexdigest
    digest = [hexdigest].pack('H*')
    cipher = OpenSSL::Cipher::Cipher.new(ALGORITHM).decrypt
    cipher.key = digest[0, 16]
    cipher.iv = digest[4, 16]
  end
  b = Iceberg::Storage.new
  file = b.getobject(name)
  [ctype, disp, file, cipher]
end

.ip2digest(ip) ⇒ Object



171
172
173
174
# File 'lib/ibg.rb', line 171

def self.ip2digest(ip)
  salt = SETTING['local']['salt'] || ''
  Digest::SHA1.hexdigest(salt + ip)
end

.key2url(key) ⇒ Object



198
199
200
201
# File 'lib/ibg.rb', line 198

def self.key2url(key)
  dbkey = IBDB_SHORT_URL + key
  REDIS.get(dbkey)
end

.recordip(ip) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ibg.rb', line 176

def self.recordip(ip)
  info = JSON.parse(REDIS.hget(IBDB_PEERS, ip2digest(ip)) || '{}')
  info['download'] ||= 0
  info['download'] += 1
  REDIS.hset(IBDB_PEERS, ip2digest(ip), info.to_json)
  REDIS.lrem(IBDB_RECENT_PEERS, 1, ip)
  REDIS.lpush(IBDB_RECENT_PEERS, ip)
  if REDIS.llen(IBDB_RECENT_PEERS) > 10 # TODO
    REDIS.rpop(IBDB_RECENT_PEERS)
  end
end

.upload(path, tripkey, filesize, filemax, text = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ibg.rb', line 86

def self.upload(path, tripkey, filesize, filemax, text = nil)
  tripkey = nil if tripkey.empty?
  if path
    raise 'size over' if File.new(path).size > filesize
    alldata = File.open(path, 'rb'){|fd| fd.read}
  else
    raise 'size over' if text.size > filesize # TODO convert to binary size
    alldata = text
  end
  digest = Digest::SHA1.digest(alldata)
  hexdigest = digest.unpack('H*')[0]
  cipher = OpenSSL::Cipher::Cipher.new(ALGORITHM).encrypt
  cipher.key = digest[0, 16]
  cipher.iv = digest[4, 16]
  encdata = cipher.update(alldata) + cipher.final
  encdigest = Digest::SHA1.hexdigest(encdata)

  b = Iceberg::Storage.new
  dest = b.getobject(encdigest)
  unless dest.exists?
    dest.write(encdata)
    dest.close rescue nil # TODO
    uploadimpl2(filemax, encdigest)
  end
  if tripkey
    tripcode = Base64.encode64(Digest::SHA1.digest(tripkey))[0, 12]
    tripcode = tripcode.tr('/', '.')
    REDIS.sadd(IBDB_TRIPCODE_SET, tripcode)
    REDIS.sadd(IBDB_TRIPCODE + tripcode, encdigest)
    # TODO test (initial bonus)
    v = REDIS.get(IBDB_TRIPCODE_FUND + tripcode)
    if v
      REDIS.incrby(IBDB_TRIPCODE_FUND + tripcode, 1)
    else
      REDIS.set(IBDB_TRIPCODE_FUND + tripcode, 2)
    end
  end
  {
    :digest => hexdigest,
    :encdigest => encdigest,
    :tripcode => tripcode,
  }
end

.uploadimpl2(filemax, encdigest) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ibg.rb', line 57

def self.uploadimpl2(filemax, encdigest)
  n = REDIS.lpush(IBDB_RECENT, encdigest)
  tripcodelist = REDIS.smembers(IBDB_TRIPCODE_SET) || []
  while n.size > filemax
    n = REDIS.llen(IBDB_RECENT)
    dropdigest = REDIS.rpop(IBDB_RECENT)
    b = Iceberg::Storage.new
    dropfile = b.getobject(dropdigest)
    dropsize = dropfile.content_length / (1024 * 1024) # MiB
    dropsize = 1 if dropsize <= 0 # TODO handle under 1 MiB
    found = false
    tripcodelist.each do |tripcode|
      next unless REDIS.sismember(IBDB_TRIPCODE + tripcode, dropdigest)
      v = REDIS.get(IBDB_TRIPCODE_FUND + tripcode)
      if v.to_i > 0
        REDIS.decrby(IBDB_TRIPCODE_FUND + tripcode, dropsize)
        found = true
        break
      end
    end
    unless found
      # TODO do not remove small files (for now)
      dropfile.delete if dropfile.content_length > 64 * 1024 # 64 KiB
      break
    end
    REDIS.lpush(IBDB_RECENT, dropdigest)
  end
end

.uploadraw(path, filesize, filemax) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ibg.rb', line 130

def self.uploadraw(path, filesize, filemax)
  raise 'size over' if File.new(path).size > filesize
  encdata = File.open(path, 'rb'){|fd| fd.read} # TODO large file
  encdigest = Digest::SHA1.hexdigest(encdata)
  # TODO check filename
  b = Iceberg::Storage.new
  dest = b.getobject(encdigest)
  unless dest.exists?
    dest.write(encdata)
    dest.close rescue nil # TODO
    uploadimpl2(filemax, encdigest)
  end
  {
  }
end

.url2key(url, seconds) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/ibg.rb', line 188

def self.url2key(url, seconds)
  key = "%010d" % rand(10000000000)
  dbkey = IBDB_SHORT_URL + key
  unless REDIS.exists(dbkey) # TODO retry
    REDIS.set(dbkey, url)
    REDIS.expire(dbkey, seconds)
    key
  end
end