File size: 4,031 Bytes
657223f
 
 
 
 
 
 
d3c7334
657223f
 
 
de32a26
657223f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de32a26
 
 
 
 
 
 
 
 
 
 
 
 
 
657223f
 
 
 
 
 
 
 
 
 
 
 
d3c7334
 
 
 
 
657223f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1809453
657223f
 
 
 
 
 
 
 
 
 
 
 
 
 
7bddad0
899b52b
 
657223f
 
 
 
 
 
 
 
 
 
35d396a
657223f
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import ipfsClient
from dotenv import load_dotenv
import datetime
import asyncio
import uvicorn
load_dotenv()
import os
import subprocess
# import socketio
import requests
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import time
# from explict import parallel
from pydantic import BaseModel
import urllib.parse
# import rich

class Item(BaseModel):
    filename: str
    directory: str
    username: str
    fileURI: str
    
class DeleteItem(BaseModel):
    CID: str


def get_folder_size(folder_path):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(folder_path):
        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            total_size += os.path.getsize(filepath)
    return total_size


def info(message: str):
    print(f"[INFO] <{datetime.datetime.now()}> - {message}")

def network(message: str):
    print(f"[NET] <{datetime.datetime.now()}> - {message}")

NODE_ID = os.getenv("NODE_ID")
URL = os.getenv("MASTER_URL")

# @parallel
def startup():
    info("Spinning up...")
    
    #TODO: Query the database from nodejs server instead because I cannot get the db here fuck
    safe_node_id = urllib.parse.quote(NODE_ID, safe='')
    info(safe_node_id)
    req = requests.get(URL + f"/bee3hive/internal/files/{safe_node_id}")
    
    if req.status_code != 200:
        info("Failed to get file from node")
        return
    
    data = req.json()

    # print(data) #! debug only
    
    for file in data:
        info(f"Caching file -- {file["CID"]}")
        network(ipfsClient.add_pin(file["CID"])["Pins"])
    
    info("Finished")

app = FastAPI()

origins = [
    "https://quanvndzai-bee3hiveapi.hf.space",
    "http://localhost",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.get("/healthcheck")
async def health():
    return {"message": "Ok!"}

@app.post("/delete")
async def fetch(data: DeleteItem):
    network("Reciving file deletion")
    return ipfsClient.unpin(data.CID)

@app.get("/id")
async def id():
    return subprocess.check_output(['ipfs', 'id'])


@app.post("/fetch")
async def fetch(data: Item):
    network("Reciving file assignment")
    # File is an json object containing the filename and part of the file
    print(data)
    
    network(f"Fetching file {data.filename}")
    url = URL + "/upload_queue/" + data.fileURI
    
    print(url)
    
    r = requests.get(url=url, stream=True)
    total_length = r.headers.get('content-length')

    if r.status_code != 200:
        return {"message": "Failed to fetch file"}    
    
    if not os.path.exists(".ipfs/upload_queue"):
        info("Create folder")
        os.chdir(".ipfs")
        os.mkdir("upload_queue")
        os.chdir("..")

    info("Writing temp file to disk")
    relative_path = f".ipfs/upload_queue/{data.fileURI}"
    with open(relative_path, "wb") as f:
        dl = 0
        total_length = int(total_length)
        for d in r.iter_content(chunk_size=4096):
            dl += len(d)
            f.write(d)
            done = int(100 * dl / total_length)
            # info(f"Downloaded {done}%")

        # f.write(r.content)
        
    info("Uploading file to ipfs")
    ipfs = ipfsClient.add(relative_path, data.filename, data.directory) # double check this
    
    pined = ipfsClient.add_pin(ipfs[0]["Hash"])
    print(pined)
    
    info("Done!")
    return ipfs # This is a json
    
@app.get("/availible")
async def availible():
    print(os.getcwd())
    print(f"Checking availibility... {get_folder_size("../.ipfs")}bytes")
    if get_folder_size("../.ipfs") >= 40 * 1024 * 1024 * 1024:
        return "Unavailible"
    else:
        return "Ok!"


# @parallel
# def run_web_app():
#     uvicorn.run(app, host="0.0.0.0", port=7861)

if __name__ == "__main__":
    # startup()
    uvicorn.run(app, host="0.0.0.0", port=7860)