forked from spitkov/sxbin
fix v2
This commit is contained in:
parent
5751297239
commit
5c0d0413ef
84
app.py
84
app.py
@ -1,4 +1,4 @@
|
||||
from flask import Flask, request, jsonify, send_from_directory, render_template, url_for, redirect, send_file, session, make_response, flash
|
||||
from flask import Flask, request, jsonify, send_from_directory, render_template, url_for, redirect, send_file, session, make_response, flash, g
|
||||
from werkzeug.utils import secure_filename
|
||||
import shortuuid
|
||||
import os
|
||||
@ -29,9 +29,9 @@ if not os.path.exists(UPLOAD_FOLDER):
|
||||
|
||||
# Database setup and helper functions
|
||||
def get_db():
|
||||
db = getattr(threading.current_thread(), '_database', None)
|
||||
db = getattr(g, '_database', None)
|
||||
if db is None:
|
||||
db = threading.current_thread()._database = sqlite3.connect(DATABASE)
|
||||
db = g._database = sqlite3.connect(DATABASE)
|
||||
return db
|
||||
|
||||
def init_db():
|
||||
@ -39,16 +39,30 @@ def init_db():
|
||||
db = get_db()
|
||||
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())
|
||||
# Create users table
|
||||
cursor.execute('''
|
||||
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 content table
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS content (
|
||||
vanity TEXT PRIMARY KEY,
|
||||
type TEXT NOT NULL,
|
||||
data TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
user_id INTEGER,
|
||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
||||
)
|
||||
''')
|
||||
|
||||
db.commit()
|
||||
print("Database initialized with users table.")
|
||||
else:
|
||||
print("Users table already exists.")
|
||||
print("Database initialized with users and content tables.")
|
||||
|
||||
# Call init_db() when the application starts
|
||||
with app.app_context():
|
||||
@ -69,7 +83,7 @@ def migrate_db():
|
||||
|
||||
@app.teardown_appcontext
|
||||
def close_connection(exception):
|
||||
db = getattr(threading.current_thread(), '_database', None)
|
||||
db = getattr(g, '_database', None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
@ -85,6 +99,7 @@ def get_username(user_id):
|
||||
|
||||
# Add this function to delete old files
|
||||
def delete_old_files():
|
||||
with app.app_context():
|
||||
while True:
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
@ -236,6 +251,7 @@ def serve_user_page(username, filename=None):
|
||||
current_folder=current_folder)
|
||||
|
||||
@app.route('/<vanity>')
|
||||
@app.route('/<vanity>/download')
|
||||
def redirect_vanity(vanity):
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
@ -248,7 +264,20 @@ def redirect_vanity(vanity):
|
||||
if content_type == 'file':
|
||||
file_path = os.path.join(app.config['UPLOAD_FOLDER'], content_data)
|
||||
if os.path.exists(file_path):
|
||||
return send_file(file_path)
|
||||
if '/download' in request.path:
|
||||
return send_file(file_path, as_attachment=True)
|
||||
else:
|
||||
# Embed the file if it's an image, video, or audio
|
||||
file_extension = os.path.splitext(content_data)[1].lower()
|
||||
if file_extension in ['.jpg', '.jpeg', '.png', '.gif', '.svg']:
|
||||
return send_file(file_path, mimetype=f'image/{file_extension[1:]}')
|
||||
elif file_extension in ['.mp3', '.wav']:
|
||||
return send_file(file_path, mimetype=f'audio/{file_extension[1:]}')
|
||||
elif file_extension in ['.mp4', '.webm']:
|
||||
return send_file(file_path, mimetype=f'video/{file_extension[1:]}')
|
||||
else:
|
||||
# For other file types, send as attachment
|
||||
return send_file(file_path, as_attachment=True)
|
||||
else:
|
||||
return "File not found", 404
|
||||
elif content_type == 'url':
|
||||
@ -791,11 +820,6 @@ def api_upload():
|
||||
|
||||
url = url_for('redirect_vanity', vanity=vanity, _external=True)
|
||||
delete_url = url_for('delete_content', vanity=vanity, _external=True)
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'url': url,
|
||||
'deletion_url': delete_url,
|
||||
})
|
||||
else:
|
||||
# Handle other file types
|
||||
vanity = shortuuid.uuid()[:8]
|
||||
@ -809,9 +833,10 @@ def api_upload():
|
||||
|
||||
url = url_for('redirect_vanity', vanity=new_filename, _external=True)
|
||||
delete_url = url_for('delete_content', vanity=new_filename, _external=True)
|
||||
return jsonify({
|
||||
|
||||
return json.dumps({
|
||||
'status': 'success',
|
||||
'url': url,
|
||||
'url': url.replace('/download', ''),
|
||||
'deletion_url': delete_url,
|
||||
})
|
||||
elif 'text' in request.form:
|
||||
@ -824,9 +849,10 @@ def api_upload():
|
||||
|
||||
url = url_for('redirect_vanity', vanity=vanity, _external=True)
|
||||
delete_url = url_for('delete_content', vanity=vanity, _external=True)
|
||||
return jsonify({
|
||||
|
||||
return json.dumps({
|
||||
'status': 'success',
|
||||
'url': url,
|
||||
'url': url.replace('/download', ''),
|
||||
'deletion_url': delete_url,
|
||||
})
|
||||
elif 'url' in request.form:
|
||||
@ -839,9 +865,10 @@ def api_upload():
|
||||
|
||||
short_url = url_for('redirect_vanity', vanity=vanity, _external=True)
|
||||
delete_url = url_for('delete_content', vanity=vanity, _external=True)
|
||||
return jsonify({
|
||||
|
||||
return json.dumps({
|
||||
'status': 'success',
|
||||
'url': short_url,
|
||||
'url': short_url.replace('/download', ''),
|
||||
'deletion_url': delete_url,
|
||||
})
|
||||
|
||||
@ -877,4 +904,9 @@ def create_new_file(username):
|
||||
return redirect(url_for('user_files', username=username, subpath=subpath))
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True,host='0.0.0.0',port=7123)
|
||||
# Start the cleanup thread
|
||||
cleanup_thread = threading.Thread(target=delete_old_files)
|
||||
cleanup_thread.daemon = True
|
||||
cleanup_thread.start()
|
||||
|
||||
app.run(debug=True, host='0.0.0.0', port=7123)
|
@ -49,12 +49,14 @@
|
||||
{% if info.file_size %}
|
||||
<div class="info-item"><strong>File Size:</strong> {{ info.file_size }} bytes</div>
|
||||
{% endif %}
|
||||
<a href="{{ url_for('redirect_vanity', vanity=info.vanity)|replace('/download', '') }}" class="btn">View/Embed</a>
|
||||
<a href="{{ url_for('redirect_vanity', vanity=info.vanity) ~ ('/download' if '/download' not in url_for('redirect_vanity', vanity=info.vanity) else '') }}" class="btn">Download</a>
|
||||
{% elif info.type == 'url' %}
|
||||
<div class="info-item"><strong>Target URL:</strong> {{ info.data }}</div>
|
||||
{% endif %}
|
||||
<a href="{{ url_for('redirect_vanity', vanity=info.vanity) }}" class="btn">View Content</a>
|
||||
{% if info.type == 'file' %}
|
||||
<a href="{{ url_for('redirect_vanity', vanity=info.vanity) }}/download" class="btn">Download</a>
|
||||
<a href="{{ url_for('redirect_vanity', vanity=info.vanity) }}" class="btn">Visit URL</a>
|
||||
{% elif info.type == 'pastebin' %}
|
||||
<a href="{{ url_for('redirect_vanity', vanity=info.vanity) }}" class="btn">View Pastebin</a>
|
||||
<a href="{{ url_for('raw_vanity', vanity=info.vanity) }}" class="btn">View Raw</a>
|
||||
{% endif %}
|
||||
{% if current_user.is_authenticated and current_user.username == info.username %}
|
||||
{% if info.type != 'file' or not info.is_media %}
|
||||
|
Loading…
Reference in New Issue
Block a user