Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,56 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from http.server import BaseHTTPRequestHandler, HTTPServer
|
| 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 |
class Handler(BaseHTTPRequestHandler):
|
| 30 |
def do_GET(self):
|
| 31 |
-
|
| 32 |
-
|
| 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", "
|
|
|
|
| 43 |
self.end_headers()
|
| 44 |
-
self.wfile.write(body
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 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()
|
|
|
|
|
|