# from fastapi import FastAPI, UploadFile
# from pydantic import BaseModel
# import ipfshttpclient
# import pbkdf2
# import pyaes

# app = FastAPI()

# def encryptionOfPrivate(CID,key):
#     try:
#         url_string = CID
#         iv = 77423458040705335549997212640120530171624814583885731800099110782272387303263
#         passwordSalt = b"\xd3\x06b\xdf\xdc\xd1u\xd0V\xb2\xd8\xbci\xe9\xc8\r"
#         password = str(key)
#         keyutf = pbkdf2.PBKDF2(password, passwordSalt).read(32)
#         # ciphertext = encrypted url string
#         aes = pyaes.AESModeOfOperationCTR(keyutf, pyaes.Counter(iv))
#         ciphertext = aes.encrypt(str(url_string))
#         return ciphertext
#     except:
#         print("Somthing went worng") 

# def UploadingFiles(file):
#     client = ipfshttpclient.connect('/dns/localhost/tcp/5001/http')
#     res = client.add(file)
#     return res

# class File(BaseModel):
#     File:str
# @app.post("/UploadFile")
# async def owner(file:File):
#     try:

#         file = file.File
#         # file = await file.read()
#         # file.save('text.txt')  # Save the uploaded file
#         res = UploadingFiles(file)
#         hash1 = res['Hash']
#         encryHash = encryptionOfPrivate(hash1,"MyNextFilm")
#         print("::::::::::::::::::", encryHash)
        

#         return {'hash': encryHash.hex()}
#     except Exception as e:
#         return {'error': str(e)}
    
#     # try:
#     #     client = ipfshttpclient.connect('/dns/localhost/tcp/5001/http')

#     #     file = request.files['file']
#     #     # file.save('text.txt')  # Save the uploaded file

#     #     res = client.add(file)
#     #     hash1 = res['Hash']
#     #     print("::::::::::::::::::", hash1)

#     #     return jsonify({'hash': hash1}), 200
#     # except Exception as e:
#     #     return jsonify({'error': str(e)}), 500

# if __name__ == "__main__":
#     uvicorn.run(app, host="115.245.192.138", port=8002)


from flask_cors import CORS

from flask import Flask, request, jsonify
import ipfshttpclient
import pbkdf2
import pyaes
import time

app = Flask(__name__)
CORS(app)
def encryptionOfPrivate(CID,key):
    try:
        url_string = CID
        iv = 77423458040705335549997212640120530171624814583885731800099129467854893567836
        passwordSalt = b"\xd3\x06b\xdf\xdc\xd1u\xd0V\xb2\xd8\xbci\xe9\xc8\r"
        password = str(key)
        keyutf = pbkdf2.PBKDF2(password, passwordSalt).read(32)
        # ciphertext = encrypted url string
        aes = pyaes.AESModeOfOperationCTR(keyutf, pyaes.Counter(iv))
        ciphertext = aes.encrypt(str(url_string))
        return ciphertext
    except:
        print("Somthing went worng")  

@app.route('/upload', methods=['POST'])
def upload_file():
    try:
        client = ipfshttpclient.connect('/dns/localhost/tcp/5001/http')
        file = request.files['files']
        # file.save('text.txt')  # Save the uploaded file
        res = client.add(file)
        hash = res['Hash']
        client.pin.add(hash)
        hash1= encryptionOfPrivate(hash,"MyNextFilm")
        return jsonify({'hash': hash1}), 200
    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True,port=5002)