spmoisa548 commited on
Commit
986c3be
·
verified ·
1 Parent(s): 8539c65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -38
app.py CHANGED
@@ -1,48 +1,56 @@
 
1
  import os
 
2
  from http.server import BaseHTTPRequestHandler, HTTPServer
3
 
4
- DATA_DIR = "/data"
5
-
6
- def list_files(base):
7
- found = []
8
- for root, dirs, files in os.walk(base):
9
- for name in files:
10
- full = os.path.join(root, name)
11
- rel = os.path.relpath(full, base)
12
- found.append(rel)
13
- return sorted(found)
14
-
15
- print("=== STARTUP ===")
16
- print(f"Checking {DATA_DIR}")
17
-
18
- if os.path.exists(DATA_DIR):
19
- files = list_files(DATA_DIR)
20
- if files:
21
- print("=== FILE NAMES ===")
22
- for f in files:
23
- print(f"[FILE] {f}")
24
- else:
25
- print("No files found in /data")
26
- else:
27
- print("/data does not exist")
 
 
 
 
28
 
29
  class Handler(BaseHTTPRequestHandler):
30
  def do_GET(self):
31
- files = []
32
- if os.path.exists(DATA_DIR):
33
- files = list_files(DATA_DIR)
34
-
35
- body = "<h1>/data files</h1>"
36
- if files:
37
- body += "<ul>" + "".join(f"<li>{f}</li>" for f in files) + "</ul>"
38
- else:
39
- body += "<p>No files found in /data</p>"
40
 
41
  self.send_response(200)
42
- self.send_header("Content-Type", "text/html; charset=utf-8")
 
43
  self.end_headers()
44
- self.wfile.write(body.encode("utf-8"))
 
 
 
 
 
 
 
 
 
 
45
 
46
- port = int(os.environ.get("PORT", "7860"))
47
- print(f"Serving on 0.0.0.0:{port}")
48
- HTTPServer(("0.0.0.0", port), Handler).serve_forever()
 
1
+ import json
2
  import os
3
+ from pathlib import Path
4
  from http.server import BaseHTTPRequestHandler, HTTPServer
5
 
6
+ # ĐẶT biến này đúng bằng mount path bạn chọn trong Space Settings
7
+ MOUNT_PATH = os.getenv("BUCKET_MOUNT_PATH", "/bucket")
8
+
9
+ def scan(base: str):
10
+ p = Path(base)
11
+ if not p.exists():
12
+ return {
13
+ "exists": False,
14
+ "path": base,
15
+ "count": 0,
16
+ "files": []
17
+ }
18
+
19
+ files = []
20
+ for x in sorted(p.rglob("*")):
21
+ if x.is_file():
22
+ st = x.stat()
23
+ files.append({
24
+ "path": str(x.relative_to(p)),
25
+ "size": st.st_size
26
+ })
27
+
28
+ return {
29
+ "exists": True,
30
+ "path": base,
31
+ "count": len(files),
32
+ "files": files
33
+ }
34
 
35
  class Handler(BaseHTTPRequestHandler):
36
  def do_GET(self):
37
+ data = scan(MOUNT_PATH)
38
+ body = json.dumps(data, ensure_ascii=False, indent=2).encode("utf-8")
 
 
 
 
 
 
 
39
 
40
  self.send_response(200)
41
+ self.send_header("Content-Type", "application/json; charset=utf-8")
42
+ self.send_header("Content-Length", str(len(body)))
43
  self.end_headers()
44
+ self.wfile.write(body)
45
+
46
+ def log_message(self, fmt, *args):
47
+ print(fmt % args, flush=True)
48
+
49
+ if __name__ == "__main__":
50
+ data = scan(MOUNT_PATH)
51
+
52
+ print("=== BUCKET_SCAN_START ===", flush=True)
53
+ print(json.dumps(data, ensure_ascii=False, indent=2), flush=True)
54
+ print("=== BUCKET_SCAN_END ===", flush=True)
55
 
56
+ HTTPServer(("0.0.0.0", 7860), Handler).serve_forever()