1
0
forked from cgcristi/aCloud

added download all to folder view which zips then downloads refactored the whole sotring mechanisms now it stores it in a db NOT IN A FUCKING HTML FILE THATS LIKE SO OPTIMAL also you can add /raw to any text and get it raw uwu :3 major refactor :333

This commit is contained in:
spitkov 2024-09-09 21:23:37 +02:00
parent 25c0f89486
commit c1201dd7a6
6 changed files with 207 additions and 70 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

191
app.py
View File

@ -4,15 +4,73 @@ import shortuuid
import os
from datetime import datetime
import zipfile
import sqlite3
import threading
import time
import shutil
from datetime import timedelta
app = Flask(__name__)
UPLOAD_FOLDER = './uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
DATABASE = 'data.db'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
data_store = {}
# Database setup and helper functions
def get_db():
db = getattr(threading.current_thread(), '_database', None)
if db is None:
db = threading.current_thread()._database = sqlite3.connect(DATABASE)
return 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()
@app.teardown_appcontext
def close_connection(exception):
db = getattr(threading.current_thread(), '_database', None)
if db is not None:
db.close()
# Initialize database
init_db()
# Add this function to delete old files
def delete_old_files():
while True:
db = get_db()
cursor = db.cursor()
# Delete files older than 30 days
thirty_days_ago = datetime.now() - timedelta(days=30)
cursor.execute("SELECT vanity, type, data FROM content WHERE created_at < ?", (thirty_days_ago,))
old_files = cursor.fetchall()
for vanity, content_type, data in old_files:
if content_type == 'file':
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{data}')
if os.path.exists(file_path):
os.remove(file_path)
elif content_type == 'folder':
folder_path = os.path.join(app.config['UPLOAD_FOLDER'], vanity)
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
cursor.execute("DELETE FROM content WHERE created_at < ?", (thirty_days_ago,))
db.commit()
time.sleep(86400) # Sleep for 24 hours
# Start the cleanup thread
cleanup_thread = threading.Thread(target=delete_old_files)
cleanup_thread.daemon = True
cleanup_thread.start()
@app.route('/')
def index():
@ -20,28 +78,36 @@ def index():
@app.route('/content/<vanity>')
def content(vanity):
target = data_store.get(vanity)
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM content WHERE vanity = ?", (vanity,))
target = cursor.fetchone()
if target:
if target['type'] == 'pastebin':
return render_template('content.html', content=target['content'], created_at=target['created_at'])
elif target['type'] == 'file':
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{target["filename"]}')
content_type, content_data = target[1], target[2]
if content_type == 'pastebin':
return render_template('content.html', content=content_data, created_at=target[3])
elif content_type == 'file':
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{content_data}')
file_info = {
'name': target['filename'],
'name': content_data,
'size': os.path.getsize(file_path),
'modified_at': datetime.fromtimestamp(os.path.getmtime(file_path)).strftime('%Y-%m-%d %H:%M:%S'),
'url': url_for('download_file', vanity=vanity)
}
return render_template('file.html', **file_info)
elif target['type'] == 'url':
return render_template('content.html', url=target['url'])
elif content_type == 'url':
return render_template('content.html', url=content_data)
return 'Not Found', 404
@app.route('/download/<vanity>', methods=['GET'])
def download_file(vanity):
target = data_store.get(vanity)
if target and target['type'] == 'file':
filename = f'{vanity}_{target["filename"]}'
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM content WHERE vanity = ? AND type = 'file'", (vanity,))
target = cursor.fetchone()
if target:
filename = f'{vanity}_{target[2]}'
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
return 'Not Found', 404
@ -50,12 +116,12 @@ def upload_pastebin():
content = request.form['content']
vanity = shortuuid.uuid()[:6]
created_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
data_store[vanity] = {'type': 'pastebin', 'content': content, 'created_at': created_at}
html_content = render_template('content.html', content=content, created_at=created_at)
html_file_path = os.path.join('templates', f'{vanity}.html')
with open(html_file_path, 'w') as f:
f.write(html_content)
db = get_db()
cursor = db.cursor()
cursor.execute("INSERT INTO content (vanity, type, data, created_at) VALUES (?, ?, ?, ?)",
(vanity, 'pastebin', content, created_at))
db.commit()
return jsonify({'vanity': vanity})
@ -71,18 +137,12 @@ def upload_file():
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{filename}')
file.save(filepath)
data_store[vanity] = {'type': 'file', 'filename': filename}
file_info = {
'name': filename,
'size': os.path.getsize(filepath),
'modified_at': datetime.fromtimestamp(os.path.getmtime(filepath)).strftime('%Y-%m-%d %H:%M:%S'),
'url': url_for('download_file', vanity=vanity)
}
html_content = render_template('file.html', **file_info)
html_file_path = os.path.join('templates', f'{vanity}.html')
with open(html_file_path, 'w') as f:
f.write(html_content)
db = get_db()
cursor = db.cursor()
cursor.execute("INSERT INTO content (vanity, type, data) VALUES (?, ?, ?)",
(vanity, 'file', filename))
db.commit()
return jsonify({'vanity': vanity})
@ -115,7 +175,11 @@ def upload_folder():
handle_uploaded_folder(files, folder_path)
data_store[vanity] = {'type': 'folder', 'files': [file.filename for file in files]}
db = get_db()
cursor = db.cursor()
cursor.execute("INSERT INTO content (vanity, type, data) VALUES (?, ?, ?)",
(vanity, 'folder', ','.join([file.filename for file in files])))
db.commit()
return jsonify({'vanity': vanity})
@ -123,51 +187,59 @@ def upload_folder():
def shorten_url():
original_url = request.form['url']
vanity = shortuuid.uuid()[:6]
data_store[vanity] = {'type': 'url', 'url': original_url}
html_content = f'<html><body><script>window.location.href="{original_url}";</script></body></html>'
html_file_path = os.path.join('templates', f'{vanity}.html')
with open(html_file_path, 'w') as f:
f.write(html_content)
db = get_db()
cursor = db.cursor()
cursor.execute("INSERT INTO content (vanity, type, data) VALUES (?, ?, ?)",
(vanity, 'url', original_url))
db.commit()
return jsonify({'vanity': vanity})
@app.route('/<vanity>', methods=['GET'])
def redirect_vanity(vanity):
target = data_store.get(vanity)
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM content WHERE vanity = ?", (vanity,))
target = cursor.fetchone()
if target:
if target['type'] == 'pastebin':
return render_template(f'{vanity}.html')
elif target['type'] == 'file':
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{target["filename"]}')
content_type, content_data = target[1], target[2]
if content_type == 'pastebin':
return render_template('content.html', content=content_data, created_at=target[3])
elif content_type == 'file':
file_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}_{content_data}')
file_info = {
'name': target['filename'],
'name': content_data,
'size': os.path.getsize(file_path),
'modified_at': datetime.fromtimestamp(os.path.getmtime(file_path)).strftime('%Y-%m-%d %H:%M:%S'),
'url': url_for('download_file', vanity=vanity)
}
return render_template('file.html', **file_info)
elif target['type'] == 'folder':
elif content_type == 'folder':
return redirect(url_for('folder_content', vanity=vanity))
elif target['type'] == 'url':
return render_template('content.html', url=target['url'])
elif content_type == 'url':
return render_template('content.html', url=content_data)
return render_template('404.html'), 404
@app.route('/<vanity>/raw', methods=['GET'])
def raw_vanity(vanity):
target = data_store.get(vanity)
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM content WHERE vanity = ? AND type = 'pastebin'", (vanity,))
target = cursor.fetchone()
if target:
if target['type'] == 'pastebin':
return render_template(f'{vanity}raw.html')
return render_template('404.html'), 404
return target[2], 200, {'Content-Type': 'text/plain; charset=utf-8'}
return 'Not Found', 404
@app.route('/folder/<vanity>', methods=['GET'])
def folder_content(vanity):
target = data_store.get(vanity)
if target and target['type'] == 'folder':
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM content WHERE vanity = ? AND type = 'folder'", (vanity,))
target = cursor.fetchone()
if target:
folder_path = os.path.join(app.config['UPLOAD_FOLDER'], vanity)
files = []
for root, _, filenames in os.walk(folder_path):
@ -188,14 +260,27 @@ def folder_content(vanity):
prev_url = url_for('folder_content', vanity=vanity, page=page-1) if page > 1 else None
next_url = url_for('folder_content', vanity=vanity, page=page+1) if end < total_files else None
return render_template('folder.html', files=files, prev_url=prev_url, next_url=next_url)
download_all_url = url_for('download_folder_as_zip', vanity=vanity)
# Get the current folder name
current_folder = os.path.basename(folder_path)
return render_template('folder.html',
files=files,
prev_url=prev_url,
next_url=next_url,
download_all_url=download_all_url,
current_folder=current_folder)
return 'Not Found', 404
@app.route('/folder/<vanity>/download', methods=['GET'])
def download_folder_as_zip(vanity):
target = data_store.get(vanity)
if target and target['type'] == 'folder':
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM content WHERE vanity = ? AND type = 'folder'", (vanity,))
target = cursor.fetchone()
if target:
folder_path = os.path.join(app.config['UPLOAD_FOLDER'], vanity)
zip_path = os.path.join(app.config['UPLOAD_FOLDER'], f'{vanity}.zip')

6
schema.sql Normal file
View File

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS content (
vanity TEXT PRIMARY KEY,
type TEXT NOT NULL,
data TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

View File

@ -3,37 +3,43 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Folder</title>
<title>Folder Contents</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #121212;
color: #e0e0e0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.container {
background: #1e1e1e;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
padding: 20px;
width: 400px;
text-align: center;
max-width: 800px;
margin: 0 auto;
}
h1 {
margin-bottom: 20px;
color: #f5f5f5;
text-align: center;
}
ul {
.folder-structure {
background: #2a2a2a;
border-radius: 4px;
padding: 10px;
margin-bottom: 20px;
}
.file-list {
list-style-type: none;
padding: 0;
}
li {
.file-list li {
margin: 10px 0;
padding: 5px;
background: #333;
border-radius: 4px;
}
a {
color: #61afef;
@ -42,16 +48,56 @@
a:hover {
text-decoration: underline;
}
.download-all-btn {
display: block;
width: 200px;
margin: 20px auto;
padding: 10px;
background-color: #4CAF50;
color: white;
text-align: center;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
}
.download-all-btn:hover {
background-color: #45a049;
}
.pagination {
text-align: center;
margin-top: 20px;
}
.pagination a {
margin: 0 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Folder</h1>
<ul>
<h1>Folder Contents</h1>
<div class="folder-structure">
<!-- You can add a tree-like structure here if needed -->
<p>Current folder: /{{ current_folder }}</p>
</div>
<ul class="file-list">
{% for file in files %}
<li><a href="{{ file.url }}">{{ file.name }}</a></li>
{% endfor %}
</ul>
<a href="{{ download_all_url }}" class="download-all-btn">Download All</a>
<div class="pagination">
{% if prev_url %}
<a href="{{ prev_url }}">Previous</a>
{% endif %}
{% if next_url %}
<a href="{{ next_url }}">Next</a>
{% endif %}
</div>
</div>
</body>
</html>

BIN
uploads/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB