-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
daemon_get_favicon.py
155 lines (150 loc) · 5.94 KB
/
daemon_get_favicon.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
__filename__ = "daemon_get_favicon.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.5.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Core GET"
import os
import urllib.parse
from fitnessFunctions import fitness_performance
from httpheaders import set_headers_etag
from httpcodes import write2
from httpcodes import http_304
from httpcodes import http_404
from daemon_utils import has_accept
from daemon_utils import etag_exists
from utils import get_config_param
from utils import media_file_mime_type
from utils import binary_is_image
def get_favicon(self, calling_domain: str,
base_dir: str, debug: bool,
fav_filename: str,
icons_cache: {}, domain_full: str) -> None:
"""Return the site favicon or default newswire favicon
"""
fav_type = 'image/x-icon'
if has_accept(self, calling_domain):
if 'image/webp' in self.headers['Accept']:
fav_type = 'image/webp'
fav_filename = fav_filename.split('.')[0] + '.webp'
if 'image/avif' in self.headers['Accept']:
fav_type = 'image/avif'
fav_filename = fav_filename.split('.')[0] + '.avif'
if 'image/heic' in self.headers['Accept']:
fav_type = 'image/heic'
fav_filename = fav_filename.split('.')[0] + '.heic'
if 'image/jxl' in self.headers['Accept']:
fav_type = 'image/jxl'
fav_filename = fav_filename.split('.')[0] + '.jxl'
if not self.server.theme_name:
self.theme_name = get_config_param(base_dir, 'theme')
if not self.server.theme_name:
self.server.theme_name = 'default'
# custom favicon
favicon_filename = \
base_dir + '/theme/' + self.server.theme_name + \
'/icons/' + fav_filename
if not fav_filename.endswith('.ico'):
if not os.path.isfile(favicon_filename):
if fav_filename.endswith('.webp'):
fav_filename = fav_filename.replace('.webp', '.ico')
elif fav_filename.endswith('.avif'):
fav_filename = fav_filename.replace('.avif', '.ico')
elif fav_filename.endswith('.heic'):
fav_filename = fav_filename.replace('.heic', '.ico')
elif fav_filename.endswith('.jxl'):
fav_filename = fav_filename.replace('.jxl', '.ico')
if not os.path.isfile(favicon_filename):
# default favicon
favicon_filename = \
base_dir + '/theme/default/icons/' + fav_filename
if etag_exists(self, favicon_filename):
# The file has not changed
if debug:
print('favicon icon has not changed: ' + calling_domain)
http_304(self)
return
if icons_cache.get(fav_filename):
fav_binary = icons_cache[fav_filename]
set_headers_etag(self, favicon_filename,
fav_type,
fav_binary, None,
domain_full,
False, None)
write2(self, fav_binary)
if debug:
print('Sent favicon from cache: ' + calling_domain)
return
if os.path.isfile(favicon_filename):
fav_binary = None
try:
with open(favicon_filename, 'rb') as fp_fav:
fav_binary = fp_fav.read()
except OSError:
print('EX: unable to read favicon ' + favicon_filename)
if fav_binary:
set_headers_etag(self, favicon_filename,
fav_type,
fav_binary, None,
domain_full,
False, None)
write2(self, fav_binary)
icons_cache[fav_filename] = fav_binary
if debug:
print('Sent favicon from file: ' + calling_domain)
return
if debug:
print('favicon not sent: ' + calling_domain)
http_404(self, 17)
def show_cached_favicon(self, referer_domain: str, path: str,
base_dir: str, getreq_start_time,
favicons_cache: {},
fitness: {}, debug: bool) -> None:
"""Shows a favicon image obtained from the cache
"""
fav_file = path.replace('/favicons/', '')
fav_filename = base_dir + urllib.parse.unquote_plus(path)
print('showCachedFavicon: ' + fav_filename)
if favicons_cache.get(fav_file):
media_binary = favicons_cache[fav_file]
mime_type = media_file_mime_type(fav_filename)
set_headers_etag(self, fav_filename,
mime_type,
media_binary, None,
referer_domain,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', '_show_cached_favicon2', debug)
return
if not os.path.isfile(fav_filename):
http_404(self, 44)
return
if etag_exists(self, fav_filename):
# The file has not changed
http_304(self)
return
media_binary = None
try:
with open(fav_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read cached favicon ' + fav_filename)
if media_binary:
if binary_is_image(fav_filename, media_binary):
mime_type = media_file_mime_type(fav_filename)
set_headers_etag(self, fav_filename,
mime_type,
media_binary, None,
referer_domain,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', '_show_cached_favicon', debug)
favicons_cache[fav_file] = media_binary
return
else:
print('WARN: favicon is not an image ' + fav_filename)
http_404(self, 45)