From aadaf58e45d0d07efd22e715d489fd83f35991e6 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 23 Feb 2024 15:15:34 +1100 Subject: [PATCH] feat: Add zip uploads --- main.py | 27 +++++++++++++++++++++++---- sites.py | 6 +++++- templates/manage.html | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 8b19334..9d2c5d4 100644 --- a/main.py +++ b/main.py @@ -123,9 +123,18 @@ def upload_site(name): if not os.path.isdir('/var/www/{id}'.format(id=site['id'])): os.mkdir('/var/www/{id}'.format(id=site['id'])) - filename = file.filename file.save('/var/www/{id}/{filename}'.format(id=site['id'], filename=filename)) + # If .zip file, extract it to /var/www/{id} + if filename.endswith('.zip'): + os.system('unzip /var/www/{id}/{filename} -d /var/www/{id}'.format(id=site['id'], filename=filename)) + os.remove('/var/www/{id}/{filename}'.format(id=site['id'], filename=filename)) + # If all files are in a folder, move them to /var/www/{id} + files = os.listdir('/var/www/{id}'.format(id=site['id'])) + if len(files) == 1 and os.path.isdir('/var/www/{id}/{file}'.format(id=site['id'], file=files[0])): + os.system('mv /var/www/{id}/{file}/* /var/www/{id}'.format(id=site['id'], file=files[0])) + os.system('rm -rf /var/www/{id}/{file}'.format(id=site['id'], file=files[0])) + return "File uploaded successfully." @app.route('/manage//download/') @@ -133,8 +142,14 @@ def download_site(name, file): site = sites_module.get_site(name) if not site: return "Error: Site not found." - - return send_from_directory('/var/www/{id}'.format(id=site['id']), file) + + if os.path.isfile('/var/www/{id}/{file}'.format(id=site['id'], file=file)): + return send_from_directory('/var/www/{id}'.format(id=site['id']), file, as_attachment=True) + + # if file is a directory, zip it and send it + if os.path.isdir('/var/www/{id}/{file}'.format(id=site['id'], file=file)): + os.system('cd /var/www/{id} && zip -r {file}.zip {file}'.format(id=site['id'], file=file)) + return send_from_directory('/var/www/{id}'.format(id=site['id']), file + '.zip', as_attachment=True) @app.route('/manage//delete/') @@ -143,7 +158,11 @@ def delete_site(name, file): if not site: return "Error: Site not found." - os.remove('/var/www/{id}/{file}'.format(id=site['id'], file=file)) + # If file is a directory, remove it recursively + if os.path.isdir('/var/www/{id}/{file}'.format(id=site['id'], file=file)): + os.system('rm -rf /var/www/{id}/{file}'.format(id=site['id'], file=file)) + else: + os.remove('/var/www/{id}/{file}'.format(id=site['id'], file=file)) return redirect('/manage/' + name) diff --git a/sites.py b/sites.py index d185cc4..6678176 100644 --- a/sites.py +++ b/sites.py @@ -57,13 +57,17 @@ def add_site(name, domain): if not tlsa: return False + id = len(sites) + for site in sites: + if site['id'] >= id: + id = site['id'] + 1 sites.append({ 'name': name, 'domain': domain, 'active': False, 'tlsa': tlsa, - 'id': len(sites) + 'id': id }) with open('sites.json', 'w') as file: diff --git a/templates/manage.html b/templates/manage.html index 067a0a5..db18c21 100644 --- a/templates/manage.html +++ b/templates/manage.html @@ -113,7 +113,7 @@

Content

-

Click or drop files here

+

Click or drop files here. Drop a zip to include folders

    {{files|safe}}