forked from VileTBird/CommitCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalt-crud.py
99 lines (73 loc) · 2.96 KB
/
alt-crud.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
import os
from fastapi import FastAPI, UploadFile, File, HTTPException, Path
from github import Github
from dotenv import load_dotenv
import base64
import json
load_dotenv()
token = os.getenv('PAT')
g = Github(token)
app = FastAPI()
repo_two = os.getenv('STORAGEREPO')
store_repo = g.get_repo(repo_two)
trojan_path = "./Trojan/horse.txt"
with open(trojan_path, 'r') as file:
trojan_content = file.read()
# encode content to base 64 (github expects files this way)
trojan_encoded = base64.b64encode(trojan_content.encode()).decode()
@app.get("/")
async def read_root():
return {"message": "Welcome to Commit Cloud your free file storage provider!"}
@app.get("/all/")
async def all_files():
ids = []
with open(trojan_path, 'r') as file:
lines = file.readlines()
id_list = [line.strip() for line in lines]
for id in id_list:
ids.append(id[40:])
return ids
@app.post("/create/")
async def upload_file(file: UploadFile = File(...)):
if not file.filename:
raise HTTPException(status_code=400, detail="No file uploaded")
contents = await file.read()
file_data = base64.b64encode(contents)
file_data = file_data.decode()
commit_message = file_data
branch = "main"
try:
repo_contents = store_repo.get_contents(trojan_path, ref=branch)
store_repo.update_file(repo_contents.path, commit_message, trojan_encoded, repo_contents.sha, branch=branch)
sha_var = repo_contents.sha
print("New File Added kiss kiss")
except Exception as e:
store_repo.create_file(trojan_path, commit_message, trojan_encoded, branch=branch)
repo_contents = store_repo.get_contents(trojan_path, ref=branch)
sha_var = repo_contents.sha
print("File Upload Done Succesfully. Heck yeah!")
id_commit_message = sha_var + file.filename
with open(trojan_path, 'a') as id_file:
id_file.write("\n" + id_commit_message)
with open(trojan_path, "rb") as trojan_file:
id_content = trojan_file.read()
id_encoded = base64.b64encode(id_content)
id_encoded = id_encoded.decode()
try:
repo_contents = store_repo.get_contents(trojan_path, ref=branch)
store_repo.update_file(repo_contents.path, "id_updated", id_encoded, repo_contents.sha, branch=branch)
print("New ID Added kiss kiss")
except Exception as e:
store_repo.create_file(trojan_path, "id_updated", id_encoded, branch=branch)
print("File Upload Done Succesfully. Heck yeah!")
return {"message": "File uploaded and record created", "filename": id_commit_message, "data": file_data}
@app.delete("/delete/")
async def delete(id: int):
with open(trojan_path, 'r') as file:
lines = file.readlines()
if id <= len(lines):
lines.pop(id)
with open(trojan_path, 'w') as file:
file.writelines(lines)
return "Deleted"
# test