1
0
forked from spitkov/sxbin

sharex fix

This commit is contained in:
spitkov 2024-09-10 19:56:15 +02:00
parent 4b0ab996ab
commit 5751297239
2 changed files with 29 additions and 26 deletions

41
app.py
View File

@ -37,9 +37,22 @@ def get_db():
def init_db():
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
cursor = db.cursor()
# Check if users table exists
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users'")
if not cursor.fetchone():
# If it doesn't exist, create it
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
print("Database initialized with users table.")
else:
print("Users table already exists.")
# Call init_db() when the application starts
with app.app_context():
init_db()
def migrate_db():
db = get_db()
@ -54,10 +67,6 @@ def migrate_db():
cursor.execute("ALTER TABLE users ADD COLUMN api_key TEXT")
db.commit()
# Call this function after init_db()
init_db()
migrate_db()
@app.teardown_appcontext
def close_connection(exception):
db = getattr(threading.current_thread(), '_database', None)
@ -315,25 +324,19 @@ def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
form = RegistrationForm(username, password)
api_key = User.generate_api_key() # Generate API key
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM users WHERE username = ?", (form.username,))
cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
if cursor.fetchone():
return "Username already exists"
hashed_password = User.hash_password(form.password)
api_key = User.generate_api_key()
try:
cursor.execute("INSERT INTO users (username, password_hash, api_key) VALUES (?, ?, ?)",
(form.username, hashed_password, api_key))
except sqlite3.OperationalError:
# If api_key column doesn't exist, insert without it
cursor.execute("INSERT INTO users (username, password_hash) VALUES (?, ?)",
(form.username, hashed_password))
hashed_password = User.hash_password(password)
cursor.execute("INSERT INTO users (username, password_hash, api_key) VALUES (?, ?, ?)",
(username, hashed_password, api_key))
db.commit()
# Create user directory
user_folder = os.path.join(app.config['UPLOAD_FOLDER'], form.username)
user_folder = os.path.join(app.config['UPLOAD_FOLDER'], username)
if not os.path.exists(user_folder):
os.makedirs(user_folder)

View File

@ -1,3 +1,10 @@
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
api_key TEXT
);
CREATE TABLE IF NOT EXISTS content (
vanity TEXT PRIMARY KEY,
type TEXT NOT NULL,
@ -5,11 +12,4 @@ CREATE TABLE IF NOT EXISTS content (
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_id INTEGER,
FOREIGN KEY (user_id) REFERENCES users (id)
);
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
api_key TEXT
);