1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| import os import subprocess from flask import Flask, request, render_template, jsonify, send_from_directory
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.before_request def before_request(): try: os.remove('archive.tar') except Exception as e: print(f"Error occurred while removing tar file: {e}")
@app.route('/upload', methods=['POST']) def upload_file(): if 'file' not in request.files: return 'No file part'
file = request.files['file']
if file.filename == '': return 'No selected file'
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename)) return f'File {file.filename} uploaded successfully!'
@app.route('/list') def list_files(): try: result = subprocess.run( ['sh', '-c', f'cd {app.config["UPLOAD_FOLDER"]} && find *'], stdout = subprocess.PIPE, text = True ) return jsonify({'files': result.stdout.splitlines()}) except subprocess.CalledProcessError as e: return f"Error occurred while listing files: {e}"
@app.route('/delete', methods=['POST']) def delete_files(): try: subprocess.run( ['sh', '-c', f'cd {app.config["UPLOAD_FOLDER"]} && rm ./*'] ) return 'All files deleted successfully!' except Exception as e: return f"Error occurred while deleting files: {e}"
@app.route('/download', methods=['GET']) def download_files(): subprocess.run( ['sh', '-c', f'cd {app.config["UPLOAD_FOLDER"]} && tar -cvf ../archive.tar *'] )
return send_from_directory( directory='.', path='archive.tar', as_attachment=True )
@app.route('/') def home(): return render_template('index.html')
if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)
|