Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,6 +96,58 @@ def remove_file():
|
|
| 96 |
logging.info("All uploaded files removed")
|
| 97 |
return redirect(url_for('index'))
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
@app.route('/process', methods=['POST'])
|
| 100 |
def process_file():
|
| 101 |
uploaded_files = session.get('uploaded_files', [])
|
|
|
|
| 96 |
logging.info("All uploaded files removed")
|
| 97 |
return redirect(url_for('index'))
|
| 98 |
|
| 99 |
+
from flask import jsonify
|
| 100 |
+
|
| 101 |
+
@app.route('/download_and_store', methods=['GET'])
|
| 102 |
+
def download_and_store():
|
| 103 |
+
file_url = request.args.get('file_url')
|
| 104 |
+
|
| 105 |
+
if not file_url:
|
| 106 |
+
response = {
|
| 107 |
+
'status': 'error',
|
| 108 |
+
'message': 'No file URL provided'
|
| 109 |
+
}
|
| 110 |
+
logging.warning("No file URL provided")
|
| 111 |
+
return jsonify(response), 400
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
# Fetch the file from the provided URL
|
| 115 |
+
response = requests.get(file_url)
|
| 116 |
+
if response.status_code == 200:
|
| 117 |
+
# Extract filename from the URL
|
| 118 |
+
output_filename = os.path.basename(file_url)
|
| 119 |
+
output_path = os.path.join(UPLOAD_FOLDER, output_filename)
|
| 120 |
+
|
| 121 |
+
# Save the file to the output folder
|
| 122 |
+
with open(output_path, 'wb') as f:
|
| 123 |
+
f.write(response.content)
|
| 124 |
+
|
| 125 |
+
logging.info(f"File {output_filename} downloaded and saved to {output_path}")
|
| 126 |
+
|
| 127 |
+
# JSON response with file details
|
| 128 |
+
response_data = {
|
| 129 |
+
'status': 'success',
|
| 130 |
+
'file_name': output_filename,
|
| 131 |
+
'file_path': output_path,
|
| 132 |
+
'message': f'File {output_filename} downloaded and stored successfully'
|
| 133 |
+
}
|
| 134 |
+
return jsonify(response_data), 200
|
| 135 |
+
else:
|
| 136 |
+
response_data = {
|
| 137 |
+
'status': 'error',
|
| 138 |
+
'message': f"Failed to download file from {file_url}. Status code: {response.status_code}"
|
| 139 |
+
}
|
| 140 |
+
logging.error(f"Failed to download file from {file_url}. Status code: {response.status_code}")
|
| 141 |
+
return jsonify(response_data), 400
|
| 142 |
+
|
| 143 |
+
except Exception as e:
|
| 144 |
+
logging.error(f"Error downloading file from {file_url}: {e}")
|
| 145 |
+
response_data = {
|
| 146 |
+
'status': 'error',
|
| 147 |
+
'message': f"Error downloading file from {file_url}: {str(e)}"
|
| 148 |
+
}
|
| 149 |
+
return jsonify(response_data), 500
|
| 150 |
+
|
| 151 |
@app.route('/process', methods=['POST'])
|
| 152 |
def process_file():
|
| 153 |
uploaded_files = session.get('uploaded_files', [])
|