From 573789a89906b9853de2e57696f5bfd6a9de1c8d Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sat, 11 May 2024 14:53:09 +0700 Subject: [PATCH] Create api.py --- app/routes/api.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 app/routes/api.py diff --git a/app/routes/api.py b/app/routes/api.py new file mode 100644 index 0000000..41d40cc --- /dev/null +++ b/app/routes/api.py @@ -0,0 +1,29 @@ +# API routes +from flask import Blueprint, request, jsonify +from app.services.ipfs import IPFS +from app.services.encryption import encrypt_file +from app.models import File, User + +api = Blueprint('api', __name__) + +@api.route('/files', methods=['POST']) +def upload_file(): + file = request.files['file'] + user = User.query.filter_by(username=request.form['username']).first() + if user: + encrypted_file = encrypt_file(file) + ipfs_hash = IPFS.add(encrypted_file) + file = File(name=file.filename, content=ipfs_hash, user=user) + db.session.add(file) + db.session.commit() + return jsonify({'message': 'File uploaded successfully'}) + return jsonify({'message': 'User not found'}), 404 + +@api.route('/files/', methods=['GET']) +def download_file(ipfs_hash): + file = File.query.filter_by(ipfs_hash=ipfs_hash).first() + if file: + encrypted_file = IPFS.get(ipfs_hash) + decrypted_file = decrypt_file(encrypted_file) + return send_file(decrypted_file, as_attachment=True) + return jsonify({'message': 'File not found'}), 404