userbymahadi commited on
Commit
5e8aaea
·
verified ·
1 Parent(s): 2ad7a65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -27
app.py CHANGED
@@ -1,29 +1,29 @@
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:
@@ -40,57 +40,74 @@ async def send_message(channel: str, req: Request):
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}
 
 
 
 
1
  from fastapi import FastAPI, Request, UploadFile, File
2
+ from fastapi.responses import StreamingResponse
3
  import threading
4
  import os
5
  import zipfile
6
  import time
7
+ from io import BytesIO
8
 
9
  app = FastAPI()
10
  lock = threading.Lock()
11
 
12
+ # -------- STORAGE --------
13
  channels = {}
14
  update_ids = {}
15
 
16
+ file_channels = {}
 
17
 
18
  UPLOAD_DIR = "uploads"
19
  os.makedirs(UPLOAD_DIR, exist_ok=True)
20
 
21
 
22
+ # -------- SEND 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", errors="ignore").strip()
27
 
28
  with lock:
29
  if channel not in channels:
 
40
  return {"ok": True}
41
 
42
 
43
+ # -------- SEND FILE --------
44
  @app.post("/sendFile/{channel}")
45
  async def send_file(channel: str, files: list[UploadFile] = File(...)):
46
+ saved_files = []
47
+
48
  with lock:
49
  if channel not in file_channels:
50
  file_channels[channel] = []
51
 
 
 
52
  for file in files:
53
+ filename = f"{int(time.time()*1000)}_{file.filename}"
54
  filepath = os.path.join(UPLOAD_DIR, filename)
55
 
56
+ try:
57
+ content = await file.read()
58
+ with open(filepath, "wb") as f:
59
+ f.write(content)
60
+
61
+ with lock:
62
+ file_channels[channel].append(filepath)
63
 
64
+ saved_files.append(filename)
 
65
 
66
+ except Exception as e:
67
+ saved_files.append(f"error:{file.filename}")
68
 
69
  return {"ok": True, "files": saved_files}
70
 
71
 
72
+ # -------- GET UPDATES (TEXT + FILE) --------
73
  @app.get("/getUpdates/{channel}")
74
  def get_updates(channel: str):
75
+
76
  with lock:
 
77
  msgs = channels.get(channel, []).copy()
78
  channels[channel] = []
79
 
 
80
  files = file_channels.get(channel, []).copy()
81
  file_channels[channel] = []
82
 
83
+ # -------- CASE 1: FILE EXISTS --------
84
  if files:
85
+ mem_zip = BytesIO()
86
+
87
+ with zipfile.ZipFile(mem_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
88
 
89
+ # add files
90
  for file_path in files:
91
  if os.path.exists(file_path):
92
  zipf.write(file_path, os.path.basename(file_path))
93
 
94
+ # add text হিসেবে messages.txt
95
+ if msgs:
96
+ text_content = "\n".join([m.get("text", "") for m in msgs])
97
+ zipf.writestr("messages.txt", text_content)
98
+
99
+ mem_zip.seek(0)
100
+
101
+ return StreamingResponse(
102
+ mem_zip,
103
  media_type="application/zip",
104
+ headers={
105
+ "Content-Disposition": f"attachment; filename={channel}_data.zip"
106
+ }
107
  )
108
 
109
+ # -------- CASE 2: ONLY TEXT --------
110
+ return {
111
+ "ok": True,
112
+ "result": msgs if msgs else []
113
+ }