-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatar.py
50 lines (38 loc) · 1.38 KB
/
avatar.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
import os
import dotenv
import db
import random
import json
import varo
import re
IMAGE_LOCATION = os.getenv('IMAGE_LOCATION')
if IMAGE_LOCATION == None:
IMAGE_LOCATION = "/data"
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
MAIN_DOMAIN = os.getenv('MAIN_DOMAIN')
if MAIN_DOMAIN == None:
MAIN_DOMAIN = "shakecities.com"
if MAIN_DOMAIN == "127.0.0.1:5000":
MAIN_DOMAIN = f"http://{MAIN_DOMAIN}"
else:
MAIN_DOMAIN = f"https://{MAIN_DOMAIN}"
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def save_avatar(file,owner):
filename = re.sub(r'[^a-zA-Z0-9.]', '', file.filename).lower()
while os.path.exists(f"{IMAGE_LOCATION}/{filename}"):
filename = f"{random.randint(0,1000000)}-{filename}"
file.save(f"{IMAGE_LOCATION}/{filename}")
user_data = db.get_website_data_raw(owner)
user_data['avatar'] = MAIN_DOMAIN + "/avatar/" + filename
db.update_website_data_raw(owner,json.dumps(user_data))
varo.update_avatar(user_data['avatar'],owner)
return filename
def clear(owner):
user_data = db.get_website_data_raw(owner)
filename = user_data['avatar'].split('/')[-1]
if filename != "":
os.remove(f"{IMAGE_LOCATION}/{filename}")
user_data['avatar'] = ""
db.update_website_data_raw(owner,json.dumps(user_data))
varo.update_avatar(user_data['avatar'],owner)