This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
62 lines (48 loc) · 1.41 KB
/
app.rb
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
59
60
61
62
require 'rack-cache'
require 'sinatra'
require 'faraday'
require 'RMagick'
require 'librato-rack'
def skin_url(username)
"http://s3.amazonaws.com/MinecraftSkins/#{username}.png"
end
configure do
STDOUT.sync = true
set :ttl, 24 * 60 * 60
end
# Preload default avatar
configure do
response = Faraday.get(skin_url('char'))
skin = Magick::ImageList.new
skin.from_blob(response.body)
helm = skin.crop(40, 8, 8, 8)
head = skin.crop(8, 8, 8, 8)
avatar = head.composite(helm, 0, 0, Magick::AtopCompositeOp)
set :default_avatar, avatar
end
use Librato::Rack
use Rack::Cache, verbose: true,
default_ttl: settings.ttl,
allow_revalidate: true,
allow_reload: true
get '/helm/:username/:size.png' do |username, size|
response = Librato.measure('mojang.skin.request.time') do
Faraday.get(skin_url(username))
end
avatar = Librato.measure('mojang.skin.composite.time') do
if response.status == 200
Librato.increment 'mojang.skin.custom'
skin = Magick::ImageList.new
skin.from_blob(response.body)
helm = skin.crop(40, 8, 8, 8)
head = skin.crop(8, 8, 8, 8)
head.composite(helm, 0, 0, Magick::AtopCompositeOp)
else
Librato.increment 'mojang.skin.default'
settings.default_avatar
end.sample(size.to_i, size.to_i)
end
headers 'Etag' => response.headers['ETag']
content_type :png
avatar.to_blob
end