dmart 1.4.41.post37__py3-none-any.whl → 1.4.41.post39__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dmart/main.py CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env -S BACKEND_ENV=config.env python3
2
2
  """ Main module """
3
3
  import socket
4
+ import mimetypes
4
5
  from starlette.datastructures import UploadFile
5
6
  from contextlib import asynccontextmanager
6
7
  import asyncio
@@ -46,16 +47,52 @@ from pathlib import Path
46
47
 
47
48
  class SPAStaticFiles(StaticFiles):
48
49
  async def get_response(self, path: str, scope) -> Response:
50
+ if path == "" or path == "index.html":
51
+ return await self.serve_file("index.html", "text/html")
52
+
53
+ ext = os.path.splitext(path)[1]
54
+ if ext in [".js", ".css", ".html"]:
55
+ try:
56
+ return await self.serve_file(path)
57
+ except StarletteHTTPException:
58
+ pass
59
+
49
60
  try:
50
61
  return await super().get_response(path, scope)
51
62
  except StarletteHTTPException as ex:
52
63
  if ex.status_code == 404 and path != "index.html" and not os.path.splitext(path)[1]:
53
64
  try:
54
- return await super().get_response("index.html", scope)
55
- except StarletteHTTPException:
65
+ return await self.serve_file("index.html", "text/html")
66
+ except Exception:
56
67
  pass
57
68
  raise ex
58
69
 
70
+ async def serve_file(self, path, media_type=None):
71
+ full_path = os.path.join(self.directory, path)
72
+ if not os.path.exists(full_path):
73
+ raise StarletteHTTPException(status_code=404)
74
+
75
+ if media_type is None:
76
+ media_type, _ = mimetypes.guess_type(full_path)
77
+
78
+ try:
79
+ with open(full_path, "r", encoding="utf-8") as f:
80
+ content = f.read()
81
+ except UnicodeDecodeError:
82
+ return FileResponse(full_path, media_type=media_type)
83
+
84
+ target_url = settings.cxb_url
85
+ if not target_url.endswith("/"):
86
+ target_url += "/"
87
+
88
+ content = content.replace("/cxb/", target_url)
89
+
90
+ cxb_name = settings.cxb_url.strip("/")
91
+ if cxb_name != "cxb":
92
+ content = content.replace('"cxb"', f'"{cxb_name}"')
93
+
94
+ return Response(content, media_type=media_type)
95
+
59
96
 
60
97
  @asynccontextmanager
61
98
  async def lifespan(app: FastAPI):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dmart
3
- Version: 1.4.41.post37
3
+ Version: 1.4.41.post39
4
4
  Requires-Python: >=3.11
5
5
  Requires-Dist: fastapi
6
6
  Requires-Dist: pydantic
@@ -14,7 +14,7 @@ dmart/hypercorn_config.toml,sha256=-eryppEG8HBOM_KbFc4dTQePnpyfoW9ZG5aUATU_6yM,5
14
14
  dmart/info.json,sha256=1VYb3TIoWeUp8-9IPkNkfqHIb3znxlrd8qa7oBl3_No,123
15
15
  dmart/login_creds.sh,sha256=Aht1LwL11uzR13sa8p3BdeUCprIa9tq0vzOoplJjH5U,235
16
16
  dmart/login_creds.sh.sample,sha256=Sb43HNNn1g11rrJrtDsPgAxcXu3_wJvdNn--8S62dTE,227
17
- dmart/main.py,sha256=FzxxjWwTF_DPUB7WvBpvqPnlqqZ2jvSPg-MNPJIfsYk,20126
17
+ dmart/main.py,sha256=nvpxQ8Ht0FCl5opAxDVGpYD7atmfSD-NopeefQKrtrs,21407
18
18
  dmart/manifest.sh,sha256=K3mY5MsUlrTyHa5cARslkShegvXh-UeqJcE2UZobdrE,544
19
19
  dmart/migrate.py,sha256=hn1MZoVby_Jjqhc7y3CrLcGD619QmVZv3PONNvO7VKQ,665
20
20
  dmart/password_gen.py,sha256=xjx8wi105ZYvhLBBQj7_rugACpxifGXHse6f7YlGXWQ,196
@@ -483,8 +483,8 @@ dmart/utils/ticket_sys_utils.py,sha256=9QAlW2iiy8KyxQRBDj_WmzS5kKb0aYJmGwd4qzmGV
483
483
  dmart/utils/web_notifier.py,sha256=QM87VVid2grC5lK3NdS1yzz0z1wXljr4GChJOeK86W4,843
484
484
  dmart/utils/templates/activation.html.j2,sha256=XAMKCdoqONoc4ZQucD0yV-Pg5DlHHASZrTVItNS-iBE,640
485
485
  dmart/utils/templates/reminder.html.j2,sha256=aoS8bTs56q4hjAZKsb0jV9c-PIURBELuBOpT_qPZNVU,639
486
- dmart-1.4.41.post37.dist-info/METADATA,sha256=D6RpAeaCE4zAg0zQPI2wEIWbxN-psKLpk89wLYa60wA,839
487
- dmart-1.4.41.post37.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
488
- dmart-1.4.41.post37.dist-info/entry_points.txt,sha256=N832M4wG8d2GDw1xztKRVM3TnxpY2QDzvdFE8XaWaG8,43
489
- dmart-1.4.41.post37.dist-info/top_level.txt,sha256=zJo4qk9fUW0BGIR9f4DCfpxaXbvQXH9izIOom6JsyAo,6
490
- dmart-1.4.41.post37.dist-info/RECORD,,
486
+ dmart-1.4.41.post39.dist-info/METADATA,sha256=AIuQR0p6jubbZBtSkC-SMphgUZ55uipsvrkzsivFqyM,839
487
+ dmart-1.4.41.post39.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
488
+ dmart-1.4.41.post39.dist-info/entry_points.txt,sha256=N832M4wG8d2GDw1xztKRVM3TnxpY2QDzvdFE8XaWaG8,43
489
+ dmart-1.4.41.post39.dist-info/top_level.txt,sha256=zJo4qk9fUW0BGIR9f4DCfpxaXbvQXH9izIOom6JsyAo,6
490
+ dmart-1.4.41.post39.dist-info/RECORD,,