feat: Add zip uploads

This commit is contained in:
Nathan Woodburn 2024-02-23 15:15:34 +11:00
parent 072d7c7b58
commit aadaf58e45
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
3 changed files with 29 additions and 6 deletions

23
main.py
View File

@ -123,9 +123,18 @@ def upload_site(name):
if not os.path.isdir('/var/www/{id}'.format(id=site['id'])): if not os.path.isdir('/var/www/{id}'.format(id=site['id'])):
os.mkdir('/var/www/{id}'.format(id=site['id'])) os.mkdir('/var/www/{id}'.format(id=site['id']))
filename = file.filename filename = file.filename
file.save('/var/www/{id}/{filename}'.format(id=site['id'], filename=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." return "File uploaded successfully."
@app.route('/manage/<name>/download/<file>') @app.route('/manage/<name>/download/<file>')
@ -134,7 +143,13 @@ def download_site(name, file):
if not site: if not site:
return "Error: Site not found." 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/<name>/delete/<file>') @app.route('/manage/<name>/delete/<file>')
@ -143,6 +158,10 @@ def delete_site(name, file):
if not site: if not site:
return "Error: Site not found." return "Error: Site not found."
# 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)) os.remove('/var/www/{id}/{file}'.format(id=site['id'], file=file))
return redirect('/manage/' + name) return redirect('/manage/' + name)

View File

@ -57,13 +57,17 @@ def add_site(name, domain):
if not tlsa: if not tlsa:
return False return False
id = len(sites)
for site in sites:
if site['id'] >= id:
id = site['id'] + 1
sites.append({ sites.append({
'name': name, 'name': name,
'domain': domain, 'domain': domain,
'active': False, 'active': False,
'tlsa': tlsa, 'tlsa': tlsa,
'id': len(sites) 'id': id
}) })
with open('sites.json', 'w') as file: with open('sites.json', 'w') as file:

View File

@ -113,7 +113,7 @@
<div class="col" style="margin-top: 25px;"> <div class="col" style="margin-top: 25px;">
<h1>Content</h1> <h1>Content</h1>
<div id="dropZone" class="drop-zone"> <div id="dropZone" class="drop-zone">
<p>Click or drop files here</p><input type="file" id="fileInput" multiple=""> <p>Click or drop files here. Drop a zip to include folders</p><input type="file" id="fileInput" multiple="">
</div><ul class="list-group"> </div><ul class="list-group">
{{files|safe}} {{files|safe}}
</ul> </ul>