Skip to content

Commit

Permalink
Create api.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 11, 2024
1 parent 914f686 commit 573789a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/routes/api.py
Original file line number Diff line number Diff line change
@@ -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/<string:ipfs_hash>', 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

0 comments on commit 573789a

Please sign in to comment.