2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
OnlyLegs - Setup
|
|
|
|
Runs when the app detects that there is no user directory
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
import os
|
2023-03-04 13:45:26 +00:00
|
|
|
import sys
|
|
|
|
import platformdirs
|
2023-03-10 11:10:43 +00:00
|
|
|
import logging
|
2023-03-02 17:00:54 +00:00
|
|
|
import yaml
|
2023-03-12 12:29:29 +00:00
|
|
|
import re
|
2023-03-02 17:00:54 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
USER_DIR = platformdirs.user_config_dir('onlylegs')
|
2023-03-02 17:00:54 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
|
2023-03-12 12:29:29 +00:00
|
|
|
class Configuration:
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Setup the application on first run
|
|
|
|
"""
|
2023-03-12 13:12:38 +00:00
|
|
|
def __init__(self):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Main setup function
|
|
|
|
"""
|
2023-03-12 13:12:38 +00:00
|
|
|
print("Running startup checks...")
|
2023-03-10 11:10:43 +00:00
|
|
|
|
2023-03-12 12:29:29 +00:00
|
|
|
# Check if the user directory exists
|
2023-03-04 13:45:26 +00:00
|
|
|
if not os.path.exists(USER_DIR):
|
2023-03-02 17:00:54 +00:00
|
|
|
self.make_dir()
|
2023-03-12 12:29:29 +00:00
|
|
|
|
|
|
|
# Check if the .env file exists
|
2023-03-04 13:45:26 +00:00
|
|
|
if not os.path.exists(os.path.join(USER_DIR, '.env')):
|
2023-03-02 17:00:54 +00:00
|
|
|
self.make_env()
|
2023-03-12 12:29:29 +00:00
|
|
|
|
|
|
|
# Check if the conf.yml file exists
|
2023-03-04 13:45:26 +00:00
|
|
|
if not os.path.exists(os.path.join(USER_DIR, 'conf.yml')):
|
2023-03-02 17:00:54 +00:00
|
|
|
self.make_yaml()
|
2023-03-10 11:10:43 +00:00
|
|
|
|
2023-03-12 12:29:29 +00:00
|
|
|
# Load the config files
|
|
|
|
self.logging_config()
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def make_dir():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create the user directory
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
try:
|
2023-03-04 13:45:26 +00:00
|
|
|
os.makedirs(USER_DIR)
|
|
|
|
os.makedirs(os.path.join(USER_DIR, 'instance'))
|
|
|
|
except Exception as err:
|
|
|
|
print("Error creating user directory:", err)
|
2023-03-10 11:10:43 +00:00
|
|
|
sys.exit(1)
|
2023-03-12 12:29:29 +00:00
|
|
|
|
|
|
|
print("Created user directory at:", USER_DIR)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def make_env():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create the .env file with default values
|
|
|
|
"""
|
2023-03-02 17:00:54 +00:00
|
|
|
env_conf = {
|
2023-03-12 12:29:29 +00:00
|
|
|
'FLASK_SECRET': os.urandom(32).hex(),
|
2023-03-02 17:00:54 +00:00
|
|
|
}
|
2023-03-12 12:29:29 +00:00
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
try:
|
2023-03-11 23:56:48 +00:00
|
|
|
with open(os.path.join(USER_DIR, '.env'), encoding='utf-8', mode='w+') as file:
|
2023-03-02 17:00:54 +00:00
|
|
|
for key, value in env_conf.items():
|
2023-03-04 13:45:26 +00:00
|
|
|
file.write(f"{key}={value}\n")
|
|
|
|
except Exception as err:
|
|
|
|
print("Error creating environment variables:", err)
|
|
|
|
sys.exit(1)
|
2023-03-12 12:29:29 +00:00
|
|
|
|
|
|
|
print("""
|
2023-03-12 13:12:38 +00:00
|
|
|
####################################################
|
|
|
|
# PLEASE NOTE DOWN THE FLASK_SECRET KEY LOCARED IN #
|
|
|
|
# YOUR .config/onlylegs/.env FILE! A NEW KEY WAS #
|
|
|
|
# GENERATED FOR YOU! #
|
|
|
|
####################################################
|
2023-03-12 12:29:29 +00:00
|
|
|
""")
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def make_yaml():
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create the YAML config file with default values
|
|
|
|
"""
|
2023-03-12 12:29:29 +00:00
|
|
|
is_correct = False
|
|
|
|
email_regex = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b')
|
|
|
|
username_regex = re.compile(r'\b[A-Za-z0-9._%+-]+\b')
|
|
|
|
|
|
|
|
print("No config file found, please enter the following information:")
|
|
|
|
while not is_correct:
|
|
|
|
try:
|
|
|
|
username = input("Admin username: ")
|
|
|
|
name = input("Admin name: ")
|
|
|
|
email = input("Admin email: ")
|
|
|
|
except ValueError:
|
|
|
|
print("Please enter valid values!")
|
|
|
|
|
|
|
|
# Check if the values are valid
|
|
|
|
if not username or not username_regex.match(username):
|
|
|
|
print("Username is invalid!")
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not name:
|
|
|
|
print("Name is invalid!")
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not email or not email_regex.match(email):
|
|
|
|
print("Email is invalid!")
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Check if user is happy with the values
|
2023-03-12 15:08:49 +00:00
|
|
|
if input("Is this correct? (y/n): ").lower() == 'y':
|
2023-03-12 12:29:29 +00:00
|
|
|
is_correct = True
|
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
yaml_conf = {
|
|
|
|
'admin': {
|
2023-03-12 15:08:49 +00:00
|
|
|
'name': name,
|
|
|
|
'username': username,
|
|
|
|
'email': email,
|
2023-03-02 17:00:54 +00:00
|
|
|
},
|
|
|
|
'upload': {
|
|
|
|
'allowed-extensions': {
|
|
|
|
'jpg': 'jpeg',
|
|
|
|
'jpeg': 'jpeg',
|
|
|
|
'png': 'png',
|
2023-03-12 12:29:29 +00:00
|
|
|
'webp': 'webp',
|
2023-03-02 17:00:54 +00:00
|
|
|
},
|
|
|
|
'max-size': 69,
|
2023-03-12 12:29:29 +00:00
|
|
|
'rename': 'GWA_{{username}}_{{time}}',
|
2023-03-02 17:00:54 +00:00
|
|
|
},
|
|
|
|
'website': {
|
|
|
|
'name': 'OnlyLegs',
|
2023-03-12 12:29:29 +00:00
|
|
|
'motto': 'A gallery built for fast and simple image management. You can change this in the settings',
|
|
|
|
'language': 'en',
|
2023-03-12 00:04:58 +00:00
|
|
|
}
|
2023-03-02 17:00:54 +00:00
|
|
|
}
|
2023-03-12 12:29:29 +00:00
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
try:
|
2023-03-11 23:56:48 +00:00
|
|
|
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8', mode='w+') as file:
|
2023-03-04 13:45:26 +00:00
|
|
|
yaml.dump(yaml_conf, file, default_flow_style=False)
|
|
|
|
except Exception as err:
|
|
|
|
print("Error creating default gallery config:", err)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2023-03-12 12:29:29 +00:00
|
|
|
print("Generated config file, you can change these values in the settings of the app")
|
2023-03-10 11:10:43 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def logging_config():
|
|
|
|
logs_path = os.path.join(platformdirs.user_config_dir('onlylegs'), 'logs')
|
2023-03-10 11:10:43 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
if not os.path.isdir(logs_path):
|
|
|
|
os.mkdir(logs_path)
|
|
|
|
print("Created logs directory at:", logs_path)
|
2023-03-10 11:10:43 +00:00
|
|
|
|
|
|
|
logging.getLogger('werkzeug').disabled = True
|
|
|
|
logging.basicConfig(
|
2023-03-11 22:14:03 +00:00
|
|
|
filename=os.path.join(logs_path, 'only.log'),
|
2023-03-10 11:10:43 +00:00
|
|
|
level=logging.INFO,
|
|
|
|
datefmt='%Y-%m-%d %H:%M:%S',
|
|
|
|
format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s',
|
2023-03-11 22:14:03 +00:00
|
|
|
encoding='utf-8')
|