userbymahadi commited on
Commit
f77d1e4
·
verified ·
1 Parent(s): 830e6fd

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -0
  2. app.py +96 -0
  3. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . .
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, UploadFile, File
2
+ from fastapi.responses import FileResponse
3
+ import threading
4
+ import os
5
+ import zipfile
6
+ import time
7
+
8
+ app = FastAPI()
9
+ lock = threading.Lock()
10
+
11
+ # -------- OLD (keep unchanged) --------
12
+ channels = {}
13
+ update_ids = {}
14
+
15
+ # -------- NEW STORAGE --------
16
+ file_channels = {} # channel অনুযায়ী file list
17
+
18
+ UPLOAD_DIR = "uploads"
19
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
20
+
21
+
22
+ # -------- OLD TEXT --------
23
+ @app.post("/sendMessage/{channel}")
24
+ async def send_message(channel: str, req: Request):
25
+ data = await req.body()
26
+ text = data.decode("utf-8").strip()
27
+
28
+ with lock:
29
+ if channel not in channels:
30
+ channels[channel] = []
31
+ update_ids[channel] = 0
32
+
33
+ update_ids[channel] += 1
34
+
35
+ channels[channel].append({
36
+ "update_id": update_ids[channel],
37
+ "text": text
38
+ })
39
+
40
+ return {"ok": True}
41
+
42
+
43
+ # -------- NEW FILE UPLOAD --------
44
+ @app.post("/sendFile/{channel}")
45
+ async def send_file(channel: str, files: list[UploadFile] = File(...)):
46
+ with lock:
47
+ if channel not in file_channels:
48
+ file_channels[channel] = []
49
+
50
+ saved_files = []
51
+
52
+ for file in files:
53
+ filename = f"{int(time.time())}_{file.filename}"
54
+ filepath = os.path.join(UPLOAD_DIR, filename)
55
+
56
+ with open(filepath, "wb") as f:
57
+ f.write(await file.read())
58
+
59
+ with lock:
60
+ file_channels[channel].append(filepath)
61
+
62
+ saved_files.append(filename)
63
+
64
+ return {"ok": True, "files": saved_files}
65
+
66
+
67
+ # -------- MODIFIED GET (text + file zip) --------
68
+ @app.get("/getUpdates/{channel}")
69
+ def get_updates(channel: str):
70
+ with lock:
71
+ # text messages
72
+ msgs = channels.get(channel, []).copy()
73
+ channels[channel] = []
74
+
75
+ # files
76
+ files = file_channels.get(channel, []).copy()
77
+ file_channels[channel] = []
78
+
79
+ # যদি file থাকে → zip বানাও
80
+ if files:
81
+ zip_name = f"{channel}_files.zip"
82
+ zip_path = os.path.join(UPLOAD_DIR, zip_name)
83
+
84
+ with zipfile.ZipFile(zip_path, "w") as zipf:
85
+ for file_path in files:
86
+ if os.path.exists(file_path):
87
+ zipf.write(file_path, os.path.basename(file_path))
88
+
89
+ return FileResponse(
90
+ zip_path,
91
+ media_type="application/zip",
92
+ filename=zip_name
93
+ )
94
+
95
+ # না থাকলে text return
96
+ return {"ok": True, "result": msgs}
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn