Skip to content

Commit db43fee

Browse files
committed
break out deploy
1 parent 3ec4869 commit db43fee

File tree

2 files changed

+66
-54
lines changed

2 files changed

+66
-54
lines changed

backend/deploy.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Standard library imports
2+
import os
3+
import shutil
4+
import zipfile
5+
6+
# Third-party imports
7+
from flask import request
8+
from flask_restful import Resource
9+
10+
# Our imports
11+
from main import basedir
12+
13+
class DeployResource(Resource):
14+
def post(self):
15+
"""Upload and extract a zip file to the deploy directory"""
16+
if 'file' not in request.files:
17+
return {'error': 'No file provided'}, 400
18+
19+
file = request.files['file']
20+
21+
if file.filename == '':
22+
return {'error': 'No file selected'}, 400
23+
24+
if not file.filename.endswith('.zip'):
25+
return {'error': 'Only zip files are allowed'}, 400
26+
27+
try:
28+
# Create deploy directory if it doesn't exist
29+
deploy_dir = os.path.join(basedir, 'deploy')
30+
31+
# Clear existing deploy directory
32+
if os.path.exists(deploy_dir):
33+
shutil.rmtree(deploy_dir)
34+
os.makedirs(deploy_dir)
35+
36+
# Save the zip file temporarily
37+
temp_zip_path = os.path.join(basedir, 'temp_deploy.zip')
38+
file.save(temp_zip_path)
39+
40+
# Extract the zip file
41+
with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
42+
zip_ref.extractall(deploy_dir)
43+
44+
# Remove the temporary zip file
45+
os.remove(temp_zip_path)
46+
47+
# List extracted files
48+
extracted_files = []
49+
for root, dirs, files in os.walk(deploy_dir):
50+
for filename in files:
51+
rel_path = os.path.relpath(os.path.join(root, filename), deploy_dir)
52+
extracted_files.append(rel_path)
53+
54+
return {
55+
'message': 'Deployment successful',
56+
'deploy_directory': deploy_dir,
57+
'files_extracted': len(extracted_files),
58+
'files': extracted_files[:20] # Show first 20 files
59+
}
60+
except zipfile.BadZipFile:
61+
return {'error': 'Invalid zip file'}, 400
62+
except Exception as e:
63+
return {'error': f'Deployment failed: {str(e)}'}, 500

backend/main.py

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
import argparse
33
import json
44
import os
5-
import shutil
6-
import zipfile
75

86
# Third-party imports
97
from flask import Flask, jsonify, request, send_from_directory
108
from flask_restful import Api, Resource
119
from flask_sqlalchemy import SQLAlchemy
1210
from werkzeug.utils import secure_filename
1311

12+
# Our imports
13+
from deploy import DeployResource
14+
1415
app = Flask(__name__, static_folder='../dist', static_url_path='')
1516
app.url_map.merge_slashes = False # Don't merge consecutive slashes
1617
api = Api(app)
@@ -232,58 +233,6 @@ def get(self):
232233

233234
return {'files': sorted(list(children))}
234235

235-
class DeployResource(Resource):
236-
def post(self):
237-
"""Upload and extract a zip file to the deploy directory"""
238-
if 'file' not in request.files:
239-
return {'error': 'No file provided'}, 400
240-
241-
file = request.files['file']
242-
243-
if file.filename == '':
244-
return {'error': 'No file selected'}, 400
245-
246-
if not file.filename.endswith('.zip'):
247-
return {'error': 'Only zip files are allowed'}, 400
248-
249-
try:
250-
# Create deploy directory if it doesn't exist
251-
deploy_dir = os.path.join(basedir, 'deploy')
252-
253-
# Clear existing deploy directory
254-
if os.path.exists(deploy_dir):
255-
shutil.rmtree(deploy_dir)
256-
os.makedirs(deploy_dir)
257-
258-
# Save the zip file temporarily
259-
temp_zip_path = os.path.join(basedir, 'temp_deploy.zip')
260-
file.save(temp_zip_path)
261-
262-
# Extract the zip file
263-
with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
264-
zip_ref.extractall(deploy_dir)
265-
266-
# Remove the temporary zip file
267-
os.remove(temp_zip_path)
268-
269-
# List extracted files
270-
extracted_files = []
271-
for root, dirs, files in os.walk(deploy_dir):
272-
for filename in files:
273-
rel_path = os.path.relpath(os.path.join(root, filename), deploy_dir)
274-
extracted_files.append(rel_path)
275-
276-
return {
277-
'message': 'Deployment successful',
278-
'deploy_directory': deploy_dir,
279-
'files_extracted': len(extracted_files),
280-
'files': extracted_files[:20] # Show first 20 files
281-
}
282-
except zipfile.BadZipFile:
283-
return {'error': 'Invalid zip file'}, 400
284-
except Exception as e:
285-
return {'error': f'Deployment failed: {str(e)}'}, 500
286-
287236
# Register API routes
288237
# Storage API routes (more specific routes first)
289238
api.add_resource(StorageEntryResource, '/entries/<path:entry_key>')

0 commit comments

Comments
 (0)