Browse Source

Remove uploaded part file after merging (#1030)

Dinh Long Nguyen 1 year ago
parent
commit
5a89dea131
2 changed files with 9 additions and 6 deletions
  1. 9 2
      taipy/gui/gui.py
  2. 0 4
      tests/gui/server/http/test_file_upload.py

+ 9 - 2
taipy/gui/gui.py

@@ -745,7 +745,8 @@ class Gui:
             return f"{var_name_decode}.{suffix_var_name}" if suffix_var_name else var_name_decode, module_name
         if module_name == current_context:
             var_name = var_name_decode
-        else:
+        # only strict checking for cross-context linked variable when the context has been properly set
+        elif self._has_set_context():
             if var_name not in self.__var_dir._var_head:
                 raise NameError(f"Can't find matching variable for {var_name} on context: {current_context}")
             _found = False
@@ -953,8 +954,11 @@ class Gui:
                     try:
                         with open(file_path, "wb") as grouped_file:
                             for nb in range(part + 1):
-                                with open(upload_path / f"{file_path.name}.part.{nb}", "rb") as part_file:
+                                part_file_path = upload_path / f"{file_path.name}.part.{nb}"
+                                with open(part_file_path, "rb") as part_file:
                                     grouped_file.write(part_file.read())
+                                # remove file_path after it is merged
+                                part_file_path.unlink()
                     except EnvironmentError as ee:  # pragma: no cover
                         _warn("Cannot group file after chunk upload", ee)
                         return
@@ -1620,6 +1624,9 @@ class Gui:
     def _set_locals_context(self, context: t.Optional[str]) -> t.ContextManager[None]:
         return self.__locals_context.set_locals_context(context)
 
+    def _has_set_context(self):
+        return self.__locals_context.get_context() is not None
+
     def _get_page_context(self, page_name: str) -> str | None:
         if page_name not in self._config.routes:
             return None

+ 0 - 4
tests/gui/server/http/test_file_upload.py

@@ -87,16 +87,12 @@ def test_file_upload_multi_part(gui: Gui, helpers):
         content_type="multipart/form-data",
     )
     assert ret.status_code == 200
-    file0_path = upload_path / f"{file_name}.part.0"
-    assert file0_path.exists()
     ret = flask_client.post(
         f"/taipy-uploads?client_id={sid}",
         data={"var_name": "varname", "blob": file1, "total": "2", "part": "1"},
         content_type="multipart/form-data",
     )
     assert ret.status_code == 200
-    file1_path = upload_path / f"{file_name}.part.1"
-    assert file1_path.exists()
     file_path = upload_path / file_name
     assert file_path.exists()