import ipfshttpclient
import pbkdf2
import pyaes
# from blockchainsetting import *
import requests
UPLOAD_URL ="https://uploadfile.mynextfilm.in/upload"
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")  

def uploadDataToIPFSNode(file):
    # Define the file path
    # file_path = '/home/user/mnf/project/MNF/media/Blockchaainscripts/storage.txt'

    # Open the file in write mode ('w' for text files)
    # with open(file_path, 'wb') as file:
    # # Write data to the file    
    #     file.write(files)
    try:
        # client = ipfshttpclient.connect('/dns/localhost/tcp/5001/http')
        upload_url = UPLOAD_URL
        files = {'files': file}
        # hash = client.add(files)['Hash']
        # hash = res['Hash']
        # client.pin.add(hash)
        r=requests.post(upload_url,files=files)
        hash = r.json()['hash']
        hash1= encryptionOfPrivate(hash,"MyNextFilm")
        return  hash1.hex()
    except requests.exceptions.RequestException as e:
        print(f"Error uploading file: {e}")
        return None