-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
daemon_get_images.py
767 lines (722 loc) · 28.5 KB
/
daemon_get_images.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
__filename__ = "daemon_get_images.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.5.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Core GET"
import os
import datetime
import time
from shutil import copyfile
from media import path_is_video
from media import path_is_transcript
from media import path_is_audio
from httpcodes import write2
from httpcodes import http_304
from httpcodes import http_404
from httpheaders import set_headers_etag
from utils import data_dir
from utils import get_nickname_from_actor
from utils import media_file_mime_type
from utils import get_image_mime_type
from utils import get_image_extensions
from utils import acct_dir
from flags import is_image_file
from daemon_utils import etag_exists
from fitnessFunctions import fitness_performance
from person import save_person_qrcode
def show_avatar_or_banner(self, referer_domain: str, path: str,
base_dir: str, domain: str,
getreq_start_time, fitness: {},
debug: bool) -> bool:
"""Shows an avatar or banner or profile background image
"""
if '/users/' not in path:
if '/system/accounts/avatars/' not in path and \
'/system/accounts/headers/' not in path and \
'/accounts/avatars/' not in path and \
'/accounts/headers/' not in path:
return False
if not is_image_file(path):
return False
if '/system/accounts/avatars/' in path:
avatar_str = path.split('/system/accounts/avatars/')[1]
elif '/accounts/avatars/' in path:
avatar_str = path.split('/accounts/avatars/')[1]
elif '/system/accounts/headers/' in path:
avatar_str = path.split('/system/accounts/headers/')[1]
elif '/accounts/headers/' in path:
avatar_str = path.split('/accounts/headers/')[1]
else:
avatar_str = path.split('/users/')[1]
if not ('/' in avatar_str and '.temp.' not in path):
return False
avatar_nickname = avatar_str.split('/')[0]
avatar_file = avatar_str.split('/')[1]
avatar_file_ext = avatar_file.split('.')[-1]
# remove any numbers, eg. avatar123.png becomes avatar.png
if avatar_file.startswith('avatar'):
avatar_file = 'avatar.' + avatar_file_ext
elif avatar_file.startswith('banner'):
avatar_file = 'banner.' + avatar_file_ext
elif avatar_file.startswith('search_banner'):
avatar_file = 'search_banner.' + avatar_file_ext
elif avatar_file.startswith('image'):
avatar_file = 'image.' + avatar_file_ext
elif avatar_file.startswith('left_col_image'):
avatar_file = 'left_col_image.' + avatar_file_ext
elif avatar_file.startswith('right_col_image'):
avatar_file = 'right_col_image.' + avatar_file_ext
elif avatar_file.startswith('watermark_image'):
avatar_file = 'watermark_image.' + avatar_file_ext
avatar_filename = \
acct_dir(base_dir, avatar_nickname, domain) + '/' + avatar_file
if not os.path.isfile(avatar_filename):
original_ext = avatar_file_ext
original_avatar_file = avatar_file
alt_ext = get_image_extensions()
alt_found = False
for alt in alt_ext:
if alt == original_ext:
continue
avatar_file = \
original_avatar_file.replace('.' + original_ext,
'.' + alt)
avatar_filename = \
acct_dir(base_dir, avatar_nickname, domain) + \
'/' + avatar_file
if os.path.isfile(avatar_filename):
alt_found = True
break
if not alt_found:
return False
if etag_exists(self, avatar_filename):
# The file has not changed
http_304(self)
return True
avatar_tm = os.path.getmtime(avatar_filename)
last_modified_time = \
datetime.datetime.fromtimestamp(avatar_tm, datetime.timezone.utc)
last_modified_time_str = \
last_modified_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
media_image_type = get_image_mime_type(avatar_file)
media_binary = None
try:
with open(avatar_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read avatar ' + avatar_filename)
if media_binary:
set_headers_etag(self, avatar_filename, media_image_type,
media_binary, None,
referer_domain, True,
last_modified_time_str)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_avatar_or_banner',
debug)
return True
def show_cached_avatar(self, referer_domain: str, path: str,
base_dir: str, getreq_start_time,
fitness: {}, debug: bool) -> None:
"""Shows an avatar image obtained from the cache
"""
media_filename = base_dir + '/cache' + path
if os.path.isfile(media_filename):
if etag_exists(self, media_filename):
# The file has not changed
http_304(self)
return
media_binary = None
try:
with open(media_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read cached avatar ' + media_filename)
if media_binary:
mime_type = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type,
media_binary, None,
referer_domain,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_cached_avatar',
debug)
return
http_404(self, 46)
def show_help_screen_image(self, path: str,
base_dir: str, getreq_start_time,
theme_name: str, domain_full: str,
fitness: {}, debug: bool) -> None:
"""Shows a help screen image
"""
if not is_image_file(path):
return
media_str = path.split('/helpimages/')[1]
if '/' not in media_str:
if not theme_name:
theme = 'default'
else:
theme = theme_name
icon_filename = media_str
else:
theme = media_str.split('/')[0]
icon_filename = media_str.split('/')[1]
media_filename = \
base_dir + '/theme/' + theme + '/helpimages/' + icon_filename
# if there is no theme-specific help image then use the default one
if not os.path.isfile(media_filename):
media_filename = \
base_dir + '/theme/default/helpimages/' + icon_filename
if etag_exists(self, media_filename):
# The file has not changed
http_304(self)
return
if os.path.isfile(media_filename):
media_binary = None
try:
with open(media_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read help image ' + media_filename)
if media_binary:
mime_type = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_help_screen_image',
debug)
return
http_404(self, 43)
def show_manual_image(self, path: str,
base_dir: str, getreq_start_time,
icons_cache: {}, domain_full: str,
fitness: {}, debug: bool) -> None:
"""Shows an image within the manual
"""
image_filename = path.split('/', 1)[1]
if '/' in image_filename:
http_404(self, 41)
return
media_filename = \
base_dir + '/manual/' + image_filename
if etag_exists(self, media_filename):
# The file has not changed
http_304(self)
return
if icons_cache.get(media_filename):
media_binary = icons_cache[media_filename]
mime_type_str = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type_str,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_manual_image',
debug)
return
if os.path.isfile(media_filename):
media_binary = None
try:
with open(media_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read manual image ' +
media_filename)
if media_binary:
mime_type = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
icons_cache[media_filename] = media_binary
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_manual_image',
debug)
return
http_404(self, 42)
def show_specification_image(self, path: str,
base_dir: str, getreq_start_time,
icons_cache: {}, domain_full: str,
fitness: {}, debug: bool) -> None:
"""Shows an image within the ActivityPub specification document
"""
image_filename = path.split('/', 1)[1]
if '/' in image_filename:
http_404(self, 39)
return
media_filename = \
base_dir + '/specification/' + image_filename
if etag_exists(self, media_filename):
# The file has not changed
http_304(self)
return
if icons_cache.get(media_filename):
media_binary = icons_cache[media_filename]
mime_type_str = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type_str,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_specification_image',
debug)
return
if os.path.isfile(media_filename):
media_binary = None
try:
with open(media_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read specification image ' +
media_filename)
if media_binary:
mime_type = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
icons_cache[media_filename] = media_binary
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_specification_image',
debug)
return
http_404(self, 40)
def show_share_image(self, path: str,
base_dir: str, getreq_start_time,
domain_full: str, fitness: {},
debug: bool) -> bool:
"""Show a shared item image
"""
if not is_image_file(path):
http_404(self, 101)
return True
media_str = path.split('/sharefiles/')[1]
media_filename = base_dir + '/sharefiles/' + media_str
if not os.path.isfile(media_filename):
http_404(self, 102)
return True
if etag_exists(self, media_filename):
# The file has not changed
http_304(self)
return True
media_file_type = get_image_mime_type(media_filename)
media_binary = None
try:
with open(media_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read binary ' + media_filename)
if media_binary:
set_headers_etag(self, media_filename,
media_file_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_share_image',
debug)
return True
def show_icon(self, path: str,
base_dir: str, getreq_start_time,
theme_name: str,
icons_cache: {}, domain_full: str,
fitness: {}, debug: bool) -> None:
"""Shows an icon
"""
if not path.endswith('.png'):
http_404(self, 37)
return
media_str = path.split('/icons/')[1]
if '/' not in media_str:
if not theme_name:
theme = 'default'
else:
theme = theme_name
icon_filename = media_str
else:
theme = media_str.split('/')[0]
icon_filename = media_str.split('/')[1]
media_filename = \
base_dir + '/theme/' + theme + '/icons/' + icon_filename
if etag_exists(self, media_filename):
# The file has not changed
http_304(self)
return
if icons_cache.get(media_str):
media_binary = icons_cache[media_str]
mime_type_str = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type_str,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_icon', debug)
return
if os.path.isfile(media_filename):
media_binary = None
try:
with open(media_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read icon image ' + media_filename)
if media_binary:
mime_type = media_file_mime_type(media_filename)
set_headers_etag(self, media_filename,
mime_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
icons_cache[media_str] = media_binary
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_icon', debug)
return
http_404(self, 38)
def show_media(self, path: str, base_dir: str,
getreq_start_time, fitness: {},
debug: bool) -> None:
"""Returns a media file
"""
if is_image_file(path) or \
path_is_video(path) or \
path_is_transcript(path) or \
path_is_audio(path):
media_str = path.split('/media/')[1]
media_filename = base_dir + '/media/' + media_str
if os.path.isfile(media_filename):
if etag_exists(self, media_filename):
# The file has not changed
http_304(self)
return
media_file_type = media_file_mime_type(media_filename)
media_tm = os.path.getmtime(media_filename)
last_modified_time = \
datetime.datetime.fromtimestamp(media_tm,
datetime.timezone.utc)
last_modified_time_str = \
last_modified_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
if media_filename.endswith('.vtt'):
media_transcript = None
try:
with open(media_filename, 'r',
encoding='utf-8') as fp_vtt:
media_transcript = fp_vtt.read()
media_file_type = 'text/vtt; charset=utf-8'
except OSError:
print('EX: unable to read media binary ' +
media_filename)
if media_transcript:
media_transcript = media_transcript.encode('utf-8')
set_headers_etag(self, media_filename, media_file_type,
media_transcript, None,
None, True,
last_modified_time_str)
write2(self, media_transcript)
fitness_performance(getreq_start_time,
fitness,
'_GET', 'show_media',
debug)
return
http_404(self, 32)
return
media_binary = None
try:
with open(media_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read media binary ' + media_filename)
if media_binary:
set_headers_etag(self, media_filename, media_file_type,
media_binary, None,
None, True,
last_modified_time_str)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_media', debug)
return
http_404(self, 33)
def show_qrcode(self, calling_domain: str, path: str,
base_dir: str, domain: str, domain_full: str,
onion_domain: str, i2p_domain: str,
port: int, getreq_start_time,
fitness: {}, debug: bool) -> bool:
"""Shows a QR code for an account
"""
nickname = get_nickname_from_actor(path)
if not nickname:
http_404(self, 93)
return True
if onion_domain:
qrcode_domain = onion_domain
port = 80
elif i2p_domain:
qrcode_domain = i2p_domain
port = 80
else:
qrcode_domain = domain
save_person_qrcode(base_dir, nickname, domain, qrcode_domain, port)
qr_filename = \
acct_dir(base_dir, nickname, domain) + '/qrcode.png'
if os.path.isfile(qr_filename):
if etag_exists(self, qr_filename):
# The file has not changed
http_304(self)
return True
tries = 0
media_binary = None
while tries < 5:
try:
with open(qr_filename, 'rb') as fp_av:
media_binary = fp_av.read()
break
except OSError as ex:
print('EX: _show_qrcode ' + str(tries) + ' ' + str(ex))
time.sleep(1)
tries += 1
if media_binary:
mime_type = media_file_mime_type(qr_filename)
set_headers_etag(self, qr_filename, mime_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_qrcode',
debug)
return True
http_404(self, 94)
return True
def search_screen_banner(self, path: str,
base_dir: str, domain: str,
getreq_start_time,
domain_full: str,
fitness: {}, debug: bool) -> bool:
"""Shows a banner image on the search screen
"""
nickname = get_nickname_from_actor(path)
if not nickname:
http_404(self, 95)
return True
banner_filename = \
acct_dir(base_dir, nickname, domain) + '/search_banner.png'
if not os.path.isfile(banner_filename):
if os.path.isfile(base_dir + '/theme/default/search_banner.png'):
copyfile(base_dir + '/theme/default/search_banner.png',
banner_filename)
if os.path.isfile(banner_filename):
if etag_exists(self, banner_filename):
# The file has not changed
http_304(self)
return True
tries = 0
media_binary = None
while tries < 5:
try:
with open(banner_filename, 'rb') as fp_av:
media_binary = fp_av.read()
break
except OSError as ex:
print('EX: _search_screen_banner ' +
str(tries) + ' ' + str(ex))
time.sleep(1)
tries += 1
if media_binary:
mime_type = media_file_mime_type(banner_filename)
set_headers_etag(self, banner_filename, mime_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'search_screen_banner',
debug)
return True
http_404(self, 96)
return True
def column_image(self, side: str, path: str, base_dir: str, domain: str,
getreq_start_time, domain_full: str,
fitness: {}, debug: bool) -> bool:
"""Shows an image at the top of the left/right column
"""
nickname = get_nickname_from_actor(path)
if not nickname:
http_404(self, 97)
return True
banner_filename = \
acct_dir(base_dir, nickname, domain) + '/' + \
side + '_col_image.png'
if os.path.isfile(banner_filename):
if etag_exists(self, banner_filename):
# The file has not changed
http_304(self)
return True
tries = 0
media_binary = None
while tries < 5:
try:
with open(banner_filename, 'rb') as fp_av:
media_binary = fp_av.read()
break
except OSError as ex:
print('EX: _column_image ' + str(tries) + ' ' + str(ex))
time.sleep(1)
tries += 1
if media_binary:
mime_type = media_file_mime_type(banner_filename)
set_headers_etag(self, banner_filename, mime_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'column_image ' + side,
debug)
return True
http_404(self, 98)
return True
def show_default_profile_background(self, base_dir: str, theme_name: str,
getreq_start_time,
domain_full: {},
fitness: {}, debug: bool) -> bool:
"""If a background image is missing after searching for a handle
then substitute this image
"""
image_extensions = get_image_extensions()
for ext in image_extensions:
bg_filename = \
base_dir + '/theme/' + theme_name + '/image.' + ext
if os.path.isfile(bg_filename):
if etag_exists(self, bg_filename):
# The file has not changed
http_304(self)
return True
tries = 0
bg_binary = None
while tries < 5:
try:
with open(bg_filename, 'rb') as fp_av:
bg_binary = fp_av.read()
break
except OSError as ex:
print('EX: _show_default_profile_background ' +
str(tries) + ' ' + str(ex))
time.sleep(1)
tries += 1
if bg_binary:
if ext == 'jpg':
ext = 'jpeg'
set_headers_etag(self, bg_filename,
'image/' + ext,
bg_binary, None,
domain_full,
False, None)
write2(self, bg_binary)
fitness_performance(getreq_start_time, fitness,
'_GET',
'show_default_profile_background',
debug)
return True
break
http_404(self, 100)
return True
def show_background_image(self, path: str,
base_dir: str, getreq_start_time,
domain_full: str, fitness: {},
debug: bool) -> bool:
"""Show a background image
"""
image_extensions = get_image_extensions()
for ext in image_extensions:
for bg_im in ('follow', 'options', 'login', 'welcome'):
# follow screen background image
if path.endswith('/' + bg_im + '-background.' + ext):
bg_filename = \
data_dir(base_dir) + '/' + \
bg_im + '-background.' + ext
if os.path.isfile(bg_filename):
if etag_exists(self, bg_filename):
# The file has not changed
http_304(self)
return True
tries = 0
bg_binary = None
while tries < 5:
try:
with open(bg_filename, 'rb') as fp_av:
bg_binary = fp_av.read()
break
except OSError as ex:
print('EX: _show_background_image ' +
str(tries) + ' ' + str(ex))
time.sleep(1)
tries += 1
if bg_binary:
if ext == 'jpg':
ext = 'jpeg'
set_headers_etag(self, bg_filename,
'image/' + ext,
bg_binary, None,
domain_full,
False, None)
write2(self, bg_binary)
fitness_performance(getreq_start_time, fitness,
'_GET',
'show_background_image',
debug)
return True
http_404(self, 99)
return True
def show_emoji(self, path: str,
base_dir: str, getreq_start_time,
domain_full: {}, fitness: {},
debug: bool) -> None:
"""Returns an emoji image
"""
if is_image_file(path):
emoji_str = path.split('/emoji/')[1]
emoji_filename = base_dir + '/emoji/' + emoji_str
if not os.path.isfile(emoji_filename):
emoji_filename = base_dir + '/emojicustom/' + emoji_str
if os.path.isfile(emoji_filename):
if etag_exists(self, emoji_filename):
# The file has not changed
http_304(self)
return
media_image_type = get_image_mime_type(emoji_filename)
media_binary = None
try:
with open(emoji_filename, 'rb') as fp_av:
media_binary = fp_av.read()
except OSError:
print('EX: unable to read emoji image ' + emoji_filename)
if media_binary:
set_headers_etag(self, emoji_filename,
media_image_type,
media_binary, None,
domain_full,
False, None)
write2(self, media_binary)
fitness_performance(getreq_start_time, fitness,
'_GET', 'show_emoji', debug)
return
http_404(self, 36)