forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_icon_manager.rb
122 lines (107 loc) · 2.69 KB
/
site_icon_manager.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
# frozen_string_literal: true
module SiteIconManager
extend GlobalPath
@cache = DistributedCache.new("icon_manager")
SKETCH_LOGO_ID = -6
ICONS = {
digest_logo: {
width: nil,
height: nil,
settings: %i[digest_logo logo],
fallback_to_sketch: false,
resize_required: false,
},
mobile_logo: {
width: nil,
height: nil,
settings: %i[mobile_logo logo],
fallback_to_sketch: false,
resize_required: false,
},
large_icon: {
width: nil,
height: nil,
settings: %i[large_icon logo_small],
fallback_to_sketch: true,
resize_required: false,
},
manifest_icon: {
width: 512,
height: 512,
settings: %i[manifest_icon large_icon logo_small],
fallback_to_sketch: true,
resize_required: true,
},
favicon: {
width: 32,
height: 32,
settings: %i[favicon large_icon logo_small],
fallback_to_sketch: true,
resize_required: false,
},
apple_touch_icon: {
width: 180,
height: 180,
settings: %i[apple_touch_icon large_icon logo_small],
fallback_to_sketch: true,
resize_required: false,
},
opengraph_image: {
width: nil,
height: nil,
settings: %i[opengraph_image large_icon logo_small logo],
fallback_to_sketch: true,
resize_required: false,
},
}
WATCHED_SETTINGS = ICONS.keys + %i[logo logo_small]
def self.clear_cache!
@cache.clear
end
def self.ensure_optimized!
unless @disabled
ICONS.each do |name, info|
icon = resolve_original(info)
if info[:height] && info[:width]
OptimizedImage.create_for(icon, info[:width], info[:height])
end
end
end
@cache.clear
end
ICONS.each do |name, info|
define_singleton_method(name) do
icon = resolve_original(info)
if info[:height] && info[:width]
result = OptimizedImage.find_by(upload: icon, height: info[:height], width: info[:width])
end
result = icon if !result && !info[:resize_required]
result
end
define_singleton_method("#{name}_url") do
get_set_cache("#{name}_url") do
icon = self.public_send(name)
icon ? full_cdn_url(icon.url) : ""
end
end
end
# Used in test mode
def self.disable
@disabled = true
end
def self.enable
@disabled = false
end
private
def self.get_set_cache(key, &block)
@cache.defer_get_set(key, &block)
end
def self.resolve_original(info)
info[:settings].each do |setting_name|
value = SiteSetting.get(setting_name)
return value if value
end
return Upload.find(SKETCH_LOGO_ID) if info[:fallback_to_sketch]
nil
end
end