-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
1044 lines (842 loc) · 31.9 KB
/
app.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# //////////////////////////////////////////////////////////////////////////// #
# IMPORT EVERYTHING
# //////////////////////////////////////////////////////////////////////////// #
# Builtin imports
import functools
import random
import sqlite3
import string
import sys
import time
from datetime import datetime
from pathlib import Path
import shutil
# 3rd party imports
import requests
import ujson as json
from flask import *
from flask_compress import Compress
from flask_login import LoginManager, current_user, login_user, logout_user
from loguru import logger
from oauthlib.oauth2 import WebApplicationClient
from pdf2image import convert_from_path
from werkzeug import exceptions
# local imports
import scan # A custom lib to scan files for danger. So you guys can't go find exploits, this will be hidden
import searchhelper
from secret_data import *
from user import User
# //////////////////////////////////////////////////////////////////////////// #
# IMPORT EVERYTHING
# //////////////////////////////////////////////////////////////////////////// #
# this gets the directory this script is in. Makes it much easier to transfer between systems.
filepath = os.path.abspath(os.path.dirname(__file__))
# Adds a logger to catch errors
logger.add(f"{filepath}/deeplogs.log", backtrace=False, enqueue=True, format=("="*20 + "\n"*5 +
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<blue>{line}</blue> - <level>{message}</level>"))
# //////////////////////////////////////////////////////////////////////////// #
# Flask config
# //////////////////////////////////////////////////////////////////////////// #
# Detects if we are running the actual version that others will be using
prod = (os.name == "posix")
app = Flask("ASMShare")
app.config['UPLOAD_PATH'] = f"{filepath}/files/"
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024
app.config['MINIFY_HTML'] = True
login_manager = LoginManager()
login_manager.init_app(app)
client = WebApplicationClient(GOOGLE_CLIENT_ID)
app.secret_key = appkey
login_needed = True
compress = Compress()
app.config["COMPRESS_ALGORITHM"] = "br"
app.config["COMPRESS_BR_LEVEL"] = 11
Compress(app)
# //////////////////////////////////////////////////////////////////////////// #
# SQL config
# //////////////////////////////////////////////////////////////////////////// #
# Connect to the db file and try create a table. If the table exists already it just does nothing. If the file does not exist, it creates one then adds the table
db = sqlite3.connect("database.db")
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS filemapping (name TEXT, shortdesc TEXT, longdesc TEXT, uploader TEXT, subject TEXT, tags TEXT, time TIME, score INT, veryshort TEXT, id INT);")
db.commit()
db.close()
# //////////////////////////////////////////////////////////////////////////// #
# Varibles definition
# //////////////////////////////////////////////////////////////////////////// #
# region 404 errors
fourohfour = ["Welp. This is awkward",
"Nice One",
"This isn't great. In the meantime, drink some water",
"THIS ERROR PAGE IS SPONSORED BY RAID SHADOW LEGENDS, ONE OF THE BIGGEST MOBILE ROLE PLAYING GAMES OF 2019 AND IT'S TOTALLY FREE!",
"Real smooth",
"Honestly, the home depot song is a banger",
"Does anyone even read these?",
"This isn't ideal",
"So either you or I messed up and as the dude who made this thing, it's probably you",
"Did you ever hear the story of Darth Plaguis the wise?",
"Whoops",
"What the gosh diddly darn heck did you do?",
"Oopsy daises",
"Get 404'd",
"Ya done messed up",
"GET OUT MY ROOM I'M PLAYING MINECRAFT",
"Server hamster needs more vodka",
"This isn't the url your looking for"
]
random.shuffle(fourohfour)
cached = {}
validids = []
# //////////////////////////////////////////////////////////////////////////// #
# Helper functions
# //////////////////////////////////////////////////////////////////////////// #
@logger.catch
def convert_bytes(num):
"""convert_bytes
\n
Turns a bytecount into a nicely readable string
Parameters
----------
num : int
Number of bytes to evaluate
Returns
-------
str
Human readable string
"""
step_unit = 1024
for x in ['B', 'KB', 'MB', 'GB', 'TB']:
if num < step_unit:
return f"{round(num)} {x}"
num /= step_unit
@logger.catch
def log(message, level=2):
"""log\n
Writes the input to the log file to
Parameters
----------
message : str
The message to log to the file
level : int, optional
What level of logging to use, by default 2
"""
with open(f"{filepath}/logs.log", mode="a") as f:
f.write(
f"\n{(datetime.now()).strftime('%d/%m/%Y- %I:%M:%S %p')}: {message}")
@logger.catch
def notfoundmessage():
"""notfoundmessage\n
Gets a message to display on the 404 page.
Returns
-------
str
The message to use
"""
# Check if the variable choices exists. If not, create it. Pylance has a stroke if I use global vars so I have to do this
# to stop my IDE yelling slurs at me
if 'choices' not in locals():
choices = random.choices(fourohfour, k=5)
if len(choices) > 0:
msg = choices[-1]
else:
choices = random.choices(fourohfour, k=5)
msg = choices[-1]
choices.pop(-1)
return msg
@logger.catch
def shred(d, n=2):
"""shred\n
Cuts a dict into a list of n sized dicts
Parameters
----------
d : dict
The dict to cut
n : int, optional
Size of each chunk, by default 2
Returns
-------
list
List of dicts
"""
c = 0
y = []
g = {}
for x in d:
g[x] = d[x]
c = c + 1
if c == n:
y.append(g)
g = {}
c = 0
if g != {}:
y.append(g)
return y
@logger.catch
def updatestats(type=None):
"""updatestats\n
Adds 1 to the specified stat
Parameters
----------
type : str, optional
What stat to modify, by default None
"""
create_stats()
db = sqlite3.connect("database.db")
cursor = db.cursor()
cursor.execute("select * from stats")
if type == None:
print("No stat specified")
else:
if type == "logins":
cursor.execute("update stats set logins = logins + 1")
elif type == "downloads":
cursor.execute("update stats set downloads = downloads + 1")
db.commit()
db.close()
@logger.catch
def findfileicon(filename):
"""findfileicon\n
Gets an icon for each type of file
Parameters
----------
filename : str
The name of the file to get a file
Returns
-------
str
Path to image to use for the file
"""
ext = filename.split(".")[-1]
# if the file is an image, just use the image
if ext in ["jpg", "png", "jpeg", "gif", "svg", "webp", "bmp"] and prod:
# I would just return the path to the file, but noooooooo, flask has to be special, so instead we clone it to the directory with the rest of the stuff
# First we check if the file is already there cos why not
if (filename) not in os.listdir(f"{filepath}/static/file-images/images"):
# Copy the binary data and save to f the variable f
with open(f"{filepath}/files/{filename}", "rb") as f:
data = f.read()
# Now go write f to a new file with the same name in the static directory
with open(f"{filepath}/static/file-images/images/{filename}", "wb") as f:
f.write(data)
# Now return the path to the new file
fileicon = r"""../static/file-images/images/""" + filename
# if it is a video, use this format
elif ext in ["mov", "mkv", "mp4"]:
fileicon = r"""../static/file-images/video.jpg"""
# now this is funky. If the file is a pdf, create and show a preview of the file
elif ext in ["mp3", "ogg", "wav"]:
fileicon = r"""../static/file-images/audio.jpg"""
elif ext in ["pdf"]:
# Stick it inside a try cos someone is gunna upload a .exe after renaming it to a .pdf (Duncan I'm looking at you)
# only create a preview if it does not exist
if not prod:
fileicon = r"""../static/file-images/pdf.jpg"""
else:
if (filename + ".jpg") not in os.listdir(f"{filepath}/static/file-images/pdfs"):
convert_from_path(f'{filepath}/files/{filename}', output_folder=f"{filepath}/static/file-images/pdfs",
fmt="jpeg", single_file=True, output_file=filename)
fileicon = r"""../static/file-images/pdfs/""" + filename + ".jpg"
# if the library nopes out, just keep going and use the normal pdf icon
else:
# if it fits into none of the above catagories, search for a jpg named the file type (docx.jpg, txt.jpg, ect)
if (ext + ".jpg") in os.listdir(f"{filepath}/static/file-images"):
fileicon = r"""../static/file-images/""" + ext + ".jpg"
else:
# if it still can't find it, use a unknown icon
fileicon = r"""../static/file-images/unknown.jpg"""
return fileicon
@logger.catch
def getname(email=None):
"""getname\n
Gets the first name of the user
Returns
-------
str
The name to use for the person
"""
db = sqlite3.connect("sqlite_db")
cursor = db.cursor()
if email == None:
email = current_user.email
cursor.execute("Select name from user where email = ?", (email, ))
res = cursor.fetchone()
if res != None:
namebyemail = res[0]
with open("names.json", "r") as names:
names = json.load(names)
db.commit()
db.close()
if email in names:
return names[email]
elif "namebyemail" in locals():
return namebyemail
else:
return email
cards = {}
imgfolder = r"""../static/file-images/"""
@logger.catch
def compileimages():
"""compileimages\n
Recompile the image dict
"""
global cards
cards = {}
db = sqlite3.connect("database.db")
cursor = db.cursor()
cursor.execute('SELECT * from filemapping')
# loops through all the entries in the db then adds the specified type to the dict
for x in cursor.fetchall():
# creates the dict entry for the file
cards[x[9]] = {}
# Data structure is fun (No it's not please help me)
cards[x[9]]["name"] = x[0]
# adds the short description
cards[x[9]]["short"] = x[1]
# adds the long description
cards[x[9]]["long"] = x[2]
# adds uploader's name
cards[x[9]]["uploader"] = getname(x[3])
# subject is added
cards[x[9]]["subject"] = x[4]
# tags
if x[5] == None:
cards[x[9]]["tags"] = "None"
else:
cards[x[9]]["tags"] = x[5].split(",")
cards[x[9]]["date"] = x[6]
cards[x[9]]["score"] = x[7]
# uses the above function to get/create an image for the thumbnail
cards[x[9]]["image"] = findfileicon(x[9])
# Shorter short description
cards[x[9]]["veryshort"] = x[8]
db.close()
compileimages()
stats = {}
@logger.catch
def create_stats():
"""create_stats\n
Pulls stats from the db
"""
db = sqlite3.connect("database.db")
cursor = db.cursor()
cursor.execute("SELECT COUNT(1) FROM filemapping;")
stats["uploads"] = cursor.fetchone()[0]
db.close()
db = sqlite3.connect("sqlite_db")
cursor = db.cursor()
cursor.execute("SELECT COUNT(1) FROM user;")
stats["users"] = cursor.fetchone()[0]
db.close()
rec = 0
paths = [f"{filepath}/files", f"{filepath}/static/file-images/images",
f"{filepath}/static/file-images/pdfs"]
for x in paths:
root_directory = Path(x)
b = sum(f.stat().st_size for f in root_directory.glob(
'**/*') if f.is_file())
rec += b
stats["size"] = convert_bytes(rec).split(" ")
db = sqlite3.connect("database.db")
cursor = db.cursor()
cursor.execute("SELECT logins FROM stats;")
stats["logins"] = cursor.fetchone()[0]
db.close()
tags = json.load(open(f"{filepath}/tags.json"))
top = max(tags.items(), key=lambda k: k[1])
stats["toptag"] = top[0]
create_stats()
@logger.catch
def is_admin():
"""is_admin\n
Checks if the user is admin or in the admins file
Returns
-------
bool
Whether or not they are an admin
"""
if any(i.isdigit() for i in current_user.email) and current_user.email not in admins:
return False
else:
return True
def updateall():
"""updateall\n
Updates all dictionaries
"""
create_stats()
compileimages()
# //////////////////////////////////////////////////////////////////////////// #
# Errors
# //////////////////////////////////////////////////////////////////////////// #
@app.errorhandler(404)
@logger.catch
def not_found(e):
return render_template('errors/404.html', message=notfoundmessage()), 404
@app.errorhandler(403)
@logger.catch
def fourohthree(e):
return render_template('errors/403.html'), 403
@app.route("/403")
@logger.catch
def fourohthreepage():
return render_template('errors/403.html')
@app.errorhandler(406)
@logger.catch
def fourohsix(e):
return render_template('errors/406.html'), 406
@app.route("/406")
@logger.catch
def fourohsixpage():
return render_template('errors/406.html')
@app.errorhandler(500)
@logger.catch
def fivehundred(e):
return render_template('errors/500.html'), 500
@app.route("/500")
@logger.catch
def fivehundredpage():
return render_template('errors/500.html')
@app.route("/553")
@logger.catch
def fivethreethreepage():
return render_template('errors/553.html')
@app.errorhandler(408)
@logger.catch
def fouroheight(e):
return render_template('errors/408.html'), 408
@app.route("/408")
@logger.catch
def fouroheightpage():
return render_template('errors/408.html')
@app.errorhandler(400)
@logger.catch
def fourhundred(e):
return render_template('errors/400.html'), 400
@app.route("/400")
@logger.catch
def fourhundredpage():
return render_template('errors/400.html')
@app.errorhandler(413)
@logger.catch
def fouronethree(e):
return render_template('errors/413.html'), 413
@app.route("/413")
@logger.catch
def fouronethreepage():
return render_template('errors/413.html')
@app.errorhandler(429)
@logger.catch
def fourytwentynine(e):
return render_template('errors/413.html'), 413
@app.route("/429")
@logger.catch
def fourytwentyninepage():
return render_template('errors/429.html')
# //////////////////////////////////////////////////////////////////////////// #
# Login based black magic
# //////////////////////////////////////////////////////////////////////////// #
def get_google_provider_cfg():
return requests.get(GOOGLE_DISCOVERY_URL).json()
# Even god does not know what this bit does
@login_manager.user_loader
def load_user(user_id):
return User.get(user_id)
def improved_login(func):
"""improved_login\n
Custom login Manager
Parameters
----------
func : function
The page to attach to and check if user is logged in
Returns
-------
func
The page to send them to (either the page they were trying to visit or the login page)
"""
@functools.wraps(func)
def page():
if current_user.is_authenticated:
return func()
else:
res = make_response(redirect(url_for("login")))
if func.__name__ == "entry":
res.set_cookie("return-page", "home", 60 * 5)
else:
res.set_cookie("return-page", func.__name__, 60 * 5)
return res
return page
# //////////////////////////////////////////////////////////////////////////// #
# Page functions
# //////////////////////////////////////////////////////////////////////////// #
# Main page. If the user is logged in, shows that on the login button and the user's name
@app.route("/")
@logger.catch
def entry():
"""Main Page"""
if current_user.is_authenticated:
return render_template('index.html', redir="/home", logged=f"Logged In: {getname()}")
else:
return render_template('index.html', redir="/home", logged="Log in")
# This will be the main page for logged in users. Will show the exemplars
@app.route("/home", methods=["GET", "POST"])
@logger.catch
@improved_login
def home():
"""Home page for user"""
updateall()
if request.method == "GET":
return render_template('land.html', stats=stats)
elif request.method == "POST":
query = request.form["query"]
return redirect(url_for('search', query=query))
@app.route("/rickroll")
@logger.catch
def rickroll():
""":)"""
return render_template('rickroll.html')
@app.route("/mobile")
@logger.catch
def mobile():
""":)"""
return render_template('mobile.html')
@app.route("/info")
@logger.catch
def info():
""":)"""
return render_template('info.html')
@app.route("/errors")
@logger.catch
def errors():
""":)"""
return render_template('errorsexaplained.html')
@app.route("/error")
@logger.catch
def error():
"""A page that only exists to raise a 500 error"""
a = 10
b = 0
a/b
@app.route("/feedback")
@logger.catch
def feedback():
"Feedback form"
return render_template('feedback.html')
# A full screen view of the examplars
@app.route('/items/<item>')
@logger.catch
def fullcard(item):
"Fullscreen display of an item"
taglist = cards[item]["tags"]
if item in cards:
return render_template('cardview.html', card=cards[item], download=item, taglist=taglist)
else:
return redirect(url_for("not_found", message=notfoundmessage())), 404
@app.route("/logout")
@logger.catch
@improved_login
def logout():
"Logs the user out"
updateall()
logout_user()
return redirect(url_for("entry"))
@app.route("/logs")
@logger.catch
@improved_login
def logs():
"A page to show the logs"
if is_admin():
with open(f'{filepath}/logs.log', 'r') as f:
return render_template('logs.html', logfile=f.readlines())
else:
return redirect(url_for("fourohthreepage"))
@app.route("/deeplogs")
@logger.catch
@improved_login
def deeplogs():
"A page to show the deeplogs"
if is_admin():
with open(f'{filepath}/deeplogs.log', 'r') as f:
return render_template('deeplogs.html', logfile=f.read())
else:
return redirect(url_for("fourohthreepage"))
@app.route("/change", methods=["POST", "GET"])
@logger.catch
@improved_login
def changename():
"Page that allows you to change your name"
updateall()
if request.method == "GET":
return render_template('change.html')
elif request.method == "POST":
rename = request.form["name"]
log(f"User {current_user.email} changed their name to {rename} (was {getname()})")
with open(f"{filepath}/names.json") as j:
j = json.load(j)
if rename == "clear":
j.pop(current_user.email)
else:
j[current_user.email] = rename
json.dump(j, open(f"{filepath}/names.json", "w"))
return redirect(url_for("home"))
# even i don't know what the next few bits do. google auth is a pain
@login_manager.unauthorized_handler
@app.route("/login")
@logger.catch
def login():
"Either send you to the login handler or logs you in with dummy data"
if login_needed:
# Find out what URL to hit for Google login
google_provider_cfg = get_google_provider_cfg()
authorization_endpoint = google_provider_cfg["authorization_endpoint"]
# Use library to construct the request for Google login and provide
# scopes that let you retrieve user's profile from Google
request_uri = client.prepare_request_uri(
authorization_endpoint,
redirect_uri=request.base_url + "/callback",
scope=["openid", "email", "profile"],
)
return redirect(request_uri)
# If the login_needed var is off, use a dummy account
else:
unique_id = u"0"
users_email = "[email protected]"
picture = "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-gear-512.png"
users_name = "Test User"
user = User(
id_=unique_id, name=users_name, email=users_email, profile_pic=picture
)
# Doesn't exist? Add it to the database.
if not User.get(unique_id):
User.create(unique_id, users_name, users_email, picture)
login_user(user)
if request.cookies.get("return-page") == None or request.cookies.get("return-page") == "None":
return redirect(url_for("home"))
else:
return redirect(url_for(request.cookies.get("return-page")))
# ALL of the callbacks
@app.route("/login/callback")
@app.route('/upload/callback')
@app.route('/home/callback')
@logger.catch
def callback():
"Blasphemy"
# Get authorization code Google sent back to you
code = request.args.get("code")
# Find out what URL to hit to get tokens that allow you to ask for
# things on behalf of a user
google_provider_cfg = get_google_provider_cfg()
token_endpoint = google_provider_cfg["token_endpoint"]
# Prepare and send a request to get tokens! Yay tokens!
token_url, headers, body = client.prepare_token_request(
token_endpoint,
authorization_response=request.url,
redirect_url=request.base_url,
code=code
)
token_response = requests.post(
token_url,
headers=headers,
data=body,
auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
)
# Parse the tokens!
client.parse_request_body_response(json.dumps(token_response.json()))
# Now that you have tokens (yay) let's find and hit the URL
# from Google that gives you the user's profile information,
# including their Google profile image and email
userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
uri, headers, body = client.add_token(userinfo_endpoint)
userinfo_response = requests.get(uri, headers=headers, data=body)
# You want to make sure their email is verified.
# The user authenticated with Google, authorized your
# app, and now you've verified their email through Google!
if userinfo_response.json().get("email_verified"):
unique_id = userinfo_response.json()["sub"]
users_email = userinfo_response.json()["email"]
picture = userinfo_response.json()["picture"]
users_name = userinfo_response.json()["given_name"]
if "@asms.sa.edu.au" in users_email:
user = User(
id_=unique_id, name=users_name, email=users_email, profile_pic=picture
)
# Doesn't exist? Add it to the database.
if not User.get(unique_id):
User.create(unique_id, users_name, users_email, picture)
login_user(user)
log(
f"User {getname()} {userinfo_response.json()['family_name']} has logged in")
print(f"usr: {current_user.email}")
hold = random.randint(1, 10000)
if hold == 1:
# has a 1 in 10000 chance to rickroll the user on login
return redirect(url_for("rickroll"))
else:
updatestats("logins")
if request.cookies.get("return-page") == None or request.cookies.get("return-page") == "None":
return redirect(url_for("home"))
else:
return redirect(url_for(request.cookies.get("return-page")))
else:
return redirect(url_for("fivethreethreepage"))
else:
return "User email not available or not verified by Google.", 400
# Page where you upload your files
@app.route('/upload')
@logger.catch
@improved_login
def upload():
# TODO: Fix this bit. It causes error randomly. Dunno why
checkid = (''.join(random.choice(string.ascii_letters) for i in range(30)))
# validids.append(checkid)
"The upload page"
# Retrieves the tags from a file
tagfile = json.load(open(f"{filepath}/tags.json", "r"))
# Sorts the tags by their usage
tagsdict = {k: v for k, v in sorted(
tagfile.items(), key=lambda item: item[1], reverse=True)}
# Turns them into a list of keys
tags = list(tagsdict.keys())
req = True
return render_template("upload.html", tags=tags, req=req, checkid=checkid, banned=banned)
# If the upload goes well, send it here to actually be stored
@app.route('/success', methods=['POST'])
@logger.catch
def success():
"A page that you get sent to when you upload something"
# Oh boy, this one is gunna be a pain to explain
# We assume the request will be a GET request. If not, idk. I'll deal with that later
if request.method == 'POST':
# Assign the file to the variable f. I'm not sure what data type this is, but I assume binary
try:
f = request.files['file']
except exceptions.RequestEntityTooLarge:
return abort(413)
# Split the name at the last . to get the file extension. This is to make sure we ignore any other .'s in tha name
ext = (f.filename).split(".")[-1]
# Check if the attached upload ID is valid. If not, 429 them.
form = request.form
"""
if form["check"] not in validids:
abort(429)
else:
# Remove the ID if it works
validids.remove(form["check"])
"""
# get the short description and name from the form, then assign them to their own variables. I should probs do it with the rest but eh
short = form["short"]
fname = form["name"]
# If the short description if over 35 characters, only add the first 32 and then add a "..." on the end
if len(short) > 35:
veryshort = "".join([short[:32]]) + "..."
else:
# If the short description is not, leave it as it is
veryshort = short
# Honestly I don't know why I have this. So far it hasn't caused any issues so I'll leave it
if len([fname, form["short"], form["long"], getname(), form["subject"], form["tags"]]) == 6:
# SQL stuff
db = sqlite3.connect("database.db")
cursor = db.cursor()
# get all the ids from the database
cursor.execute("select id from filemapping")
# add em to a list
usedids = [int(x[0].split(".")[0]) for x in cursor.fetchall()]
# If the idlist is empty, create the first id (0)
if usedids == []:
tryid = 0
else:
# Get the largest id and add one
tryid = max(usedids) + 1
# If for some reason it already exists, try again
while tryid in usedids:
tryid = max(usedids) + 1
# Save the file in the folder
f.save(f"{app.config['UPLOAD_PATH']}/{tryid}.{ext}")
scanres = scan.check(f"{app.config['UPLOAD_PATH']}/{tryid}.{ext}")
if scanres[0]:
if len(form["tags"]) > 0:
# Now we do some funky stuff with tags
tags = json.loads(form["tags"])
# max of 5 tags. You can enter more, but they won't show up
tags = [x["value"].replace(" ", "-") for x in tags[:5]]
prettytags = ",".join(tags)
# This bit just pulls apart the list into it's indvidual tags, then takes apart the tag, putting it back together but only with alphanumetic characters or hyphens
tags = [x for x in [ch.lower()
for ch in tags if ch.isalnum() or "-"]]
# Opens a json file to store the tag frequency
tagfile = json.load(open(f"{filepath}/tags.json", "r"))
# Loops though all the tags
for x in tags:
# If the tag already exists in the json file, just add 1 to it's frequency
if x in tagfile:
tagfile[x] = tagfile[x] + 1
# Otherwise just set the frequency to 1
else:
tagfile[x] = 1
json.dump(tagfile, open(f"{filepath}/tags.json", "w"))
else:
prettytags = None
# Add all the stuff the the database
cursor.execute(f'INSERT INTO filemapping VALUES (?, ?, ?, ?, ?, ?, ?, 1, ?, ?)',
(fname, short, form["long"], current_user.email, form["subject"], prettytags, ((datetime.now()).strftime('%d/%m/%Y')), veryshort, f"{tryid}.{ext}"))
# Log the upload
log(f"User {getname()} has uploaded '{fname}' ({tryid})")
db.commit()
db.close()
else:
log(
f"User {getname()} has tried to upload '{fname}' ({tryid}) but it was caught by the scanner ({scanres[1]})")
shutil.move(f"{app.config['UPLOAD_PATH']}/{tryid}.{ext}",
f"{filepath}/quarantine/{tryid}.{ext}_")
# Reload the image cache to account for the new uploads
updateall()
else:
return redirect(url_for("fourohsixpage"))
# Head back home
if not prod:
time.sleep(3)
updateall()
return redirect(url_for("home"))
@app.route("/search/")
@logger.catch
def emptysearch():
"If there is no search query, show this"
return render_template("search.html", query="")
@app.route("/search/<query>")
@logger.catch
def search(query):
"Shows searched stuff"
global cards