sharexfix

This commit is contained in:
spitkov 2024-09-14 21:00:33 +02:00
parent c76ff11c26
commit cd2c8323d5

50
app.py
View File

@ -234,13 +234,23 @@ def serve_user_page(username, filename=None):
@app.route('/<vanity>/download', methods=['GET', 'POST']) @app.route('/<vanity>/download', methods=['GET', 'POST'])
@app.route('/<vanity>/download/<password>', methods=['GET', 'POST']) @app.route('/<vanity>/download/<password>', methods=['GET', 'POST'])
def redirect_vanity(vanity, password=None): def redirect_vanity(vanity, password=None):
app.logger.info(f"Accessing vanity: {vanity}, password: {password}")
db = get_db() db = get_db()
cursor = db.cursor() cursor = db.cursor()
# First, try to find the content with the full vanity (including extension)
cursor.execute("SELECT content.*, users.username FROM content LEFT JOIN users ON content.user_id = users.id WHERE content.vanity = ?", (vanity,)) cursor.execute("SELECT content.*, users.username FROM content LEFT JOIN users ON content.user_id = users.id WHERE content.vanity = ?", (vanity,))
content = cursor.fetchone() content = cursor.fetchone()
# If not found, try without the extension
if not content:
vanity_without_extension = os.path.splitext(vanity)[0]
cursor.execute("SELECT content.*, users.username FROM content LEFT JOIN users ON content.user_id = users.id WHERE content.vanity LIKE ?", (f"{vanity_without_extension}%",))
content = cursor.fetchone()
if content: if content:
content_type, content_data, created_at, user_id, is_private, stored_password, username = content[1], content[2], content[3], content[4], content[5], content[6], content[7] content_type, content_data, created_at, user_id, is_private, stored_password, username = content[1], content[2], content[3], content[4], content[5], content[6], content[7]
app.logger.info(f"Content found: type={content_type}, data={content_data}, is_private={is_private}")
if is_private and stored_password: if is_private and stored_password:
if password: if password:
@ -253,23 +263,23 @@ def redirect_vanity(vanity, password=None):
else: else:
return render_template('password_prompt.html', vanity=vanity, error=None) return render_template('password_prompt.html', vanity=vanity, error=None)
# Remove '/download' from the content_data if present
content_data = content_data.replace('/download', '')
if content_type == 'url': if content_type == 'url':
return redirect(content_data) return redirect(content_data)
elif content_type == 'file': elif content_type == 'file':
file_path = os.path.join(app.config['UPLOAD_FOLDER'], content_data) file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], content_data)
app.logger.info(f"Attempting to serve file: {file_path}")
if os.path.exists(file_path): if os.path.exists(file_path):
if 'download' in request.path: if 'download' in request.path:
return send_file(file_path, as_attachment=True) return send_file(file_path, as_attachment=True)
else: else:
return send_file(file_path) return send_file(file_path)
else: else:
app.logger.error(f"File not found: {file_path}")
return "File not found", 404 return "File not found", 404
elif content_type == 'pastebin': elif content_type == 'pastebin':
return render_pastebin(content_data, created_at, user_id, username, vanity, is_private) return render_pastebin(content_data, created_at, user_id, username, vanity, is_private)
app.logger.error(f"Content not found for vanity: {vanity}")
return "Not found", 404 return "Not found", 404
def render_pastebin(content_data, created_at, user_id, username, vanity, is_private): def render_pastebin(content_data, created_at, user_id, username, vanity, is_private):
@ -1059,20 +1069,34 @@ def upload_file():
db.commit() db.commit()
short_url = url_for('redirect_vanity', vanity=vanity_with_extension, _external=True) short_url = url_for('redirect_vanity', vanity=vanity_with_extension, _external=True)
# Remove '/download' suffix if it exists
short_url = short_url.replace('/download', '') short_url = short_url.replace('/download', '')
download_url = short_url + '/download' download_url = short_url + '/download'
deletion_url = url_for('delete_content', vanity=vanity_with_extension, _external=True) deletion_url = url_for('delete_content', vanity=vanity_with_extension, _external=True)
return jsonify({ # Add debug logging
'success': True, app.logger.info(f"File uploaded: {new_filename}")
'vanity': vanity_with_extension, app.logger.info(f"File path: {file_path}")
'url': short_url, app.logger.info(f"Short URL: {short_url}")
'download_url': download_url, app.logger.info(f"Download URL: {download_url}")
'deletion_url': deletion_url,
'filename': new_filename # Check if the request is from ShareX
}) if 'X-API-Key' in request.headers:
return json.dumps({
'status': 'success',
'url': short_url,
'deletion_url': deletion_url,
})
else:
return jsonify({
'success': True,
'vanity': vanity_with_extension,
'url': short_url,
'download_url': download_url,
'deletion_url': deletion_url,
'filename': new_filename
})
except Exception as e: except Exception as e:
app.logger.error(f"Error uploading file: {str(e)}")
return jsonify({'success': False, 'error': str(e)}), 500 return jsonify({'success': False, 'error': str(e)}), 500
return jsonify({'success': False, 'error': 'Unknown error occurred'}), 500 return jsonify({'success': False, 'error': 'Unknown error occurred'}), 500