Stable-X commited on
Commit
1638dc2
·
verified ·
1 Parent(s): 3b36287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -8
app.py CHANGED
@@ -169,14 +169,37 @@ TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
169
  # os.environ['GRADIO_TEMP_DIR'] = 'tmp'
170
  os.makedirs(TMP_DIR, exist_ok=True)
171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  def start_session(req: gr.Request):
173
- user_dir = os.path.join(TMP_DIR, str(req.session_hash))
174
- os.makedirs(user_dir, exist_ok=True)
175
-
176
-
177
  def end_session(req: gr.Request):
178
- user_dir = os.path.join(TMP_DIR, str(req.session_hash))
179
- shutil.rmtree(user_dir)
 
 
 
 
 
 
180
 
181
  @spaces.GPU
182
  def preprocess_image(image: Image.Image) -> Image.Image:
@@ -331,7 +354,7 @@ def generate_and_extract_glb(
331
  str: The path to the extracted GLB file.
332
  str: The path to the extracted GLB file (for download).
333
  """
334
- user_dir = os.path.join(TMP_DIR, str(req.session_hash))
335
  image_files = [image[0] for image in multiimages]
336
 
337
  # Generate 3D model
@@ -392,7 +415,7 @@ def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
392
  Returns:
393
  Tuple[str, str]: Paths to the extracted Gaussian file (for display and download)
394
  """
395
- user_dir = os.path.join(TMP_DIR, str(req.session_hash))
396
  gs, _ = unpack_state(state)
397
  gaussian_path = os.path.join(user_dir, 'sample.ply')
398
  gs.save_ply(gaussian_path)
 
169
  # os.environ['GRADIO_TEMP_DIR'] = 'tmp'
170
  os.makedirs(TMP_DIR, exist_ok=True)
171
 
172
+ def _get_user_dir(req: gr.Request, create: bool = False):
173
+ session_hash = getattr(req, "session_hash", None)
174
+ if not session_hash:
175
+ return None
176
+
177
+ base_dir = os.path.abspath(TMP_DIR)
178
+ user_dir = os.path.abspath(os.path.join(base_dir, str(session_hash)))
179
+
180
+ # 防御性检查,避免异常 session_hash 导致删到 TMP_DIR 外
181
+ if os.path.commonpath([base_dir, user_dir]) != base_dir:
182
+ raise ValueError(f"Unsafe session path: {user_dir}")
183
+
184
+ if create:
185
+ os.makedirs(user_dir, exist_ok=True)
186
+
187
+ return user_dir
188
+
189
+
190
  def start_session(req: gr.Request):
191
+ _get_user_dir(req, create=True)
192
+
193
+
 
194
  def end_session(req: gr.Request):
195
+ user_dir = _get_user_dir(req, create=False)
196
+ if not user_dir:
197
+ return
198
+
199
+ try:
200
+ shutil.rmtree(user_dir)
201
+ except FileNotFoundError:
202
+ pass
203
 
204
  @spaces.GPU
205
  def preprocess_image(image: Image.Image) -> Image.Image:
 
354
  str: The path to the extracted GLB file.
355
  str: The path to the extracted GLB file (for download).
356
  """
357
+ user_dir = _get_user_dir(req, create=True)
358
  image_files = [image[0] for image in multiimages]
359
 
360
  # Generate 3D model
 
415
  Returns:
416
  Tuple[str, str]: Paths to the extracted Gaussian file (for display and download)
417
  """
418
+ user_dir = _get_user_dir(req, create=True)
419
  gs, _ = unpack_state(state)
420
  gaussian_path = os.path.join(user_dir, 'sample.ply')
421
  gs.save_ply(gaussian_path)