This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.py
356 lines (255 loc) · 10.6 KB
/
main.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from utils.downloader import (
download_file,
get_file_info_from_url,
)
import asyncio
from pathlib import Path
from contextlib import asynccontextmanager
import aiofiles
from fastapi import FastAPI, HTTPException, Request, File, UploadFile, Form, Response
from fastapi.responses import FileResponse, JSONResponse
from config import ADMIN_PASSWORD, MAX_FILE_SIZE, STORAGE_CHANNEL
from utils.clients import initialize_clients
from utils.directoryHandler import getRandomID
from utils.extra import auto_ping_website, convert_class_to_dict, reset_cache_dir
from utils.streamer import media_streamer
from utils.uploader import start_file_uploader
from utils.logger import Logger
import urllib.parse
# Startup Event
@asynccontextmanager
async def lifespan(app: FastAPI):
# Reset the cache directory, delete cache files
reset_cache_dir()
# Initialize the clients
await initialize_clients()
# Start the website auto ping task
asyncio.create_task(auto_ping_website())
yield
app = FastAPI(docs_url=None, redoc_url=None, lifespan=lifespan)
logger = Logger(__name__)
@app.get("/")
async def home_page():
return FileResponse("website/home.html")
@app.get("/stream")
async def home_page():
return FileResponse("website/VideoPlayer.html")
@app.get("/static/{file_path:path}")
async def static_files(file_path):
if "apiHandler.js" in file_path:
with open(Path("website/static/js/apiHandler.js")) as f:
content = f.read()
content = content.replace("MAX_FILE_SIZE__SDGJDG", str(MAX_FILE_SIZE))
return Response(content=content, media_type="application/javascript")
return FileResponse(f"website/static/{file_path}")
@app.get("/file")
async def dl_file(request: Request):
from utils.directoryHandler import DRIVE_DATA
path = request.query_params["path"]
file = DRIVE_DATA.get_file(path)
return await media_streamer(STORAGE_CHANNEL, file.file_id, file.name, request)
# Api Routes
@app.post("/api/checkPassword")
async def check_password(request: Request):
data = await request.json()
if data["pass"] == ADMIN_PASSWORD:
return JSONResponse({"status": "ok"})
return JSONResponse({"status": "Invalid password"})
@app.post("/api/createNewFolder")
async def api_new_folder(request: Request):
from utils.directoryHandler import DRIVE_DATA
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"createNewFolder {data}")
folder_data = DRIVE_DATA.get_directory(data["path"]).contents
for id in folder_data:
f = folder_data[id]
if f.type == "folder":
if f.name == data["name"]:
return JSONResponse(
{
"status": "Folder with the name already exist in current directory"
}
)
DRIVE_DATA.new_folder(data["path"], data["name"])
return JSONResponse({"status": "ok"})
@app.post("/api/getDirectory")
async def api_get_directory(request: Request):
from utils.directoryHandler import DRIVE_DATA
data = await request.json()
if data["password"] == ADMIN_PASSWORD:
is_admin = True
else:
is_admin = False
auth = data.get("auth")
logger.info(f"getFolder {data}")
if data["path"] == "/trash":
data = {"contents": DRIVE_DATA.get_trashed_files_folders()}
folder_data = convert_class_to_dict(data, isObject=False, showtrash=True)
elif "/search_" in data["path"]:
query = urllib.parse.unquote(data["path"].split("_", 1)[1])
print(query)
data = {"contents": DRIVE_DATA.search_file_folder(query)}
print(data)
folder_data = convert_class_to_dict(data, isObject=False, showtrash=False)
print(folder_data)
elif "/share_" in data["path"]:
path = data["path"].split("_", 1)[1]
folder_data, auth_home_path = DRIVE_DATA.get_directory(path, is_admin, auth)
auth_home_path= auth_home_path.replace("//", "/") if auth_home_path else None
folder_data = convert_class_to_dict(folder_data, isObject=True, showtrash=False)
return JSONResponse(
{"status": "ok", "data": folder_data, "auth_home_path": auth_home_path}
)
else:
folder_data = DRIVE_DATA.get_directory(data["path"])
folder_data = convert_class_to_dict(folder_data, isObject=True, showtrash=False)
return JSONResponse({"status": "ok", "data": folder_data, "auth_home_path": None})
SAVE_PROGRESS = {}
@app.post("/api/upload")
async def upload_file(
file: UploadFile = File(...),
path: str = Form(...),
password: str = Form(...),
id: str = Form(...),
total_size: str = Form(...),
):
global SAVE_PROGRESS
if password != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
total_size = int(total_size)
SAVE_PROGRESS[id] = ("running", 0, total_size)
ext = file.filename.lower().split(".")[-1]
cache_dir = Path("./cache")
cache_dir.mkdir(parents=True, exist_ok=True)
file_location = cache_dir / f"{id}.{ext}"
file_size = 0
async with aiofiles.open(file_location, "wb") as buffer:
while chunk := await file.read(1024 * 1024): # Read file in chunks of 1MB
SAVE_PROGRESS[id] = ("running", file_size, total_size)
file_size += len(chunk)
if file_size > MAX_FILE_SIZE:
await buffer.close()
file_location.unlink() # Delete the partially written file
raise HTTPException(
status_code=400,
detail=f"File size exceeds {MAX_FILE_SIZE} bytes limit",
)
await buffer.write(chunk)
SAVE_PROGRESS[id] = ("completed", file_size, file_size)
asyncio.create_task(
start_file_uploader(file_location, id, path, file.filename, file_size)
)
return JSONResponse({"id": id, "status": "ok"})
@app.post("/api/getSaveProgress")
async def get_save_progress(request: Request):
global SAVE_PROGRESS
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"getUploadProgress {data}")
try:
progress = SAVE_PROGRESS[data["id"]]
return JSONResponse({"status": "ok", "data": progress})
except:
return JSONResponse({"status": "not found"})
@app.post("/api/getUploadProgress")
async def get_upload_progress(request: Request):
from utils.uploader import PROGRESS_CACHE
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"getUploadProgress {data}")
try:
progress = PROGRESS_CACHE[data["id"]]
return JSONResponse({"status": "ok", "data": progress})
except:
return JSONResponse({"status": "not found"})
@app.post("/api/cancelUpload")
async def cancel_upload(request: Request):
from utils.uploader import STOP_TRANSMISSION
from utils.downloader import STOP_DOWNLOAD
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"cancelUpload {data}")
STOP_TRANSMISSION.append(data["id"])
STOP_DOWNLOAD.append(data["id"])
return JSONResponse({"status": "ok"})
@app.post("/api/renameFileFolder")
async def rename_file_folder(request: Request):
from utils.directoryHandler import DRIVE_DATA
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"renameFileFolder {data}")
DRIVE_DATA.rename_file_folder(data["path"], data["name"])
return JSONResponse({"status": "ok"})
@app.post("/api/trashFileFolder")
async def trash_file_folder(request: Request):
from utils.directoryHandler import DRIVE_DATA
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"trashFileFolder {data}")
DRIVE_DATA.trash_file_folder(data["path"], data["trash"])
return JSONResponse({"status": "ok"})
@app.post("/api/deleteFileFolder")
async def delete_file_folder(request: Request):
from utils.directoryHandler import DRIVE_DATA
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"deleteFileFolder {data}")
DRIVE_DATA.delete_file_folder(data["path"])
return JSONResponse({"status": "ok"})
@app.post("/api/getFileInfoFromUrl")
async def getFileInfoFromUrl(request: Request):
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"getFileInfoFromUrl {data}")
try:
file_info = await get_file_info_from_url(data["url"])
return JSONResponse({"status": "ok", "data": file_info})
except Exception as e:
return JSONResponse({"status": str(e)})
@app.post("/api/startFileDownloadFromUrl")
async def startFileDownloadFromUrl(request: Request):
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"startFileDownloadFromUrl {data}")
try:
id = getRandomID()
asyncio.create_task(
download_file(data["url"], id, data["path"], data["filename"], data["singleThreaded"])
)
return JSONResponse({"status": "ok", "id": id})
except Exception as e:
return JSONResponse({"status": str(e)})
@app.post("/api/getFileDownloadProgress")
async def getFileDownloadProgress(request: Request):
from utils.downloader import DOWNLOAD_PROGRESS
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"getFileDownloadProgress {data}")
try:
progress = DOWNLOAD_PROGRESS[data["id"]]
return JSONResponse({"status": "ok", "data": progress})
except:
return JSONResponse({"status": "not found"})
@app.post("/api/getFolderShareAuth")
async def getFolderShareAuth(request: Request):
from utils.directoryHandler import DRIVE_DATA
data = await request.json()
if data["password"] != ADMIN_PASSWORD:
return JSONResponse({"status": "Invalid password"})
logger.info(f"getFolderShareAuth {data}")
try:
auth = DRIVE_DATA.get_folder_auth(data["path"])
return JSONResponse({"status": "ok", "auth": auth})
except:
return JSONResponse({"status": "not found"})