51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
from flask import Flask, request, render_template, abort, redirect
|
|
import secrets
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Directory to store paste files
|
|
paste_dir = os.environ['PASTE_DIR']
|
|
|
|
if not os.path.exists(paste_dir):
|
|
os.makedirs(paste_dir)
|
|
|
|
# Route for the main page
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def index():
|
|
if request.method == 'POST':
|
|
# Get content and language from the form
|
|
content = request.form['content']
|
|
# Generate a unique ID for the paste
|
|
paste_id = secrets.token_hex(8)
|
|
# Create the file path for the paste
|
|
file_path = os.path.join(paste_dir, paste_id)
|
|
|
|
# Save the paste content to a file
|
|
with open(file_path, 'w') as f:
|
|
f.write(f"{content}")
|
|
|
|
# Generate the URL for the new paste
|
|
paste_url = request.url_root + paste_id
|
|
return render_template('bin.j2', paste_url=paste_url)
|
|
|
|
# Render the form with available languages
|
|
return render_template('bin.j2')
|
|
|
|
# Route to view a specific paste by its ID
|
|
@app.route('/<paste_id>')
|
|
def view_paste(paste_id):
|
|
# Create the file path for the paste
|
|
file_path = os.path.join(paste_dir, paste_id)
|
|
if not os.path.exists(file_path):
|
|
return redirect("http://localhost:5000", code=302) # Return a 404 error if the paste does not exist
|
|
|
|
|
|
with open(file_path, 'r') as f:
|
|
content = f.read()
|
|
|
|
# Render the paste with syntax highlighting
|
|
return render_template('bin.j2', paste_content=content )
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=os.environ['DEBUG_MODE'])
|