@@ -255,11 +255,9 @@ def _normalize_upload_options(workspace, base_path, base_paths):
255255
256256 @staticmethod
257257 def _calculate_normalized_upload_key (file_path , workspace , base_path , base_paths ):
258- # Calculate the key name for the form data
259258 key = file_path
260259 path_stripped = False
261260
262- # If base_paths is provided, try to strip one of the paths from the file path
263261 if base_paths :
264262 for bp in base_paths :
265263 normalized_base_path = bp .rstrip ("/" ) + "/" if not bp .endswith ("/" ) else bp
@@ -286,10 +284,9 @@ def _calculate_normalized_upload_key(file_path, workspace, base_path, base_paths
286284 key = key .lstrip ("/" )
287285 path_stripped = True
288286
289- # If workspace is provided and no base paths matched, fall back to workspace logic
287+ # Use the workspace when no base path matched.
290288 if not path_stripped and workspace and file_path .startswith (workspace ):
291289 key = file_path [len (workspace ):]
292- # Remove all leading slashes (for absolute paths)
293290 while key .startswith ("/" ):
294291 key = key [1 :]
295292 path_stripped = True
@@ -299,7 +296,6 @@ def _calculate_normalized_upload_key(file_path, workspace, base_path, base_paths
299296 key = key [2 :]
300297 while key .startswith ("../" ):
301298 key = key [3 :]
302- # Remove any remaining leading slashes (for absolute paths)
303299 while key .startswith ("/" ):
304300 key = key [1 :]
305301
@@ -319,7 +315,7 @@ def validate_upload_keys(key_sources: List[Tuple[str, str]]) -> None:
319315 for key , source in key_sources :
320316 if key is None :
321317 continue
322- # Use the installed encoder's escaping rules; the result is name="<key>".
318+ # Extract the escaped value from the encoder's name="<key>" parameter .
323319 wire_key = formatter ("name" , key )[6 :- 1 ]
324320 if wire_key in seen :
325321 raise ValueError (
@@ -328,18 +324,24 @@ def validate_upload_keys(key_sources: List[Tuple[str, str]]) -> None:
328324 seen [wire_key ] = source
329325
330326 @staticmethod
331- def prepare_upload_keys (files : List [str ], workspace : Optional [str ] = None , base_path : Optional [str ] = None , base_paths : Optional [List [str ]] = None ) -> List [Tuple [str , str ]]:
332- """Compute and validate all keys before reading any file contents ."""
327+ def calculate_upload_keys (files : List [str ], workspace : Optional [str ] = None , base_path : Optional [str ] = None , base_paths : Optional [List [str ]] = None ) -> List [Tuple [str , str ]]:
328+ """Pair each multipart field name with its original source path ."""
333329 workspace , base_path , base_paths = Utils ._normalize_upload_options (workspace , base_path , base_paths )
334- key_sources = [
330+ return [
335331 (Utils ._calculate_normalized_upload_key (path .replace ("\\ " , "/" ), workspace , base_path , base_paths ), path )
336332 for path in files
337333 ]
338- Utils .validate_upload_keys (key_sources )
339- return key_sources
340334
341335 @staticmethod
342- def load_files_for_sending_lazy (files : List [str ], workspace : Optional [str ] = None , max_open_files : int = 100 , base_path : Optional [str ] = None , base_paths : Optional [List [str ]] = None ) -> List [Tuple [str , Tuple [str , LazyFileLoader ]]]:
336+ def load_files_for_sending_lazy (
337+ files : List [str ],
338+ workspace : Optional [str ] = None ,
339+ max_open_files : int = 100 ,
340+ base_path : Optional [str ] = None ,
341+ base_paths : Optional [List [str ]] = None ,
342+ * ,
343+ reject_duplicate_keys : bool = True ,
344+ ) -> List [Tuple [str , Tuple [str , LazyFileLoader ]]]:
343345 """
344346 Prepares files for sending to the Socket API using lazy loading.
345347
@@ -354,21 +356,24 @@ def load_files_for_sending_lazy(files: List[str], workspace: Optional[str] = Non
354356 max_open_files: Maximum number of files to keep open simultaneously (default: 100)
355357 base_path: Optional base path to strip from key names for cleaner file organization
356358 base_paths: Optional list of base paths to strip from key names (takes precedence over base_path)
359+ reject_duplicate_keys: Whether to reject repeated multipart field names
357360
358361 Returns:
359362 List of tuples formatted for requests multipart upload:
360363 [(field_name, (filename, lazy_file_object)), ...]
361364
362365 Raises:
363- ValueError: If two source paths produce the same upload key.
366+ ValueError: If duplicate-key rejection is enabled and two paths produce the same key.
364367 """
365- # Configure the file descriptor manager with the specified limit
368+ upload_files = [path for path in files if not os .path .isdir (path .replace ("\\ " , "/" ))]
369+ key_sources = Utils .calculate_upload_keys (upload_files , workspace , base_path , base_paths )
370+ if reject_duplicate_keys :
371+ Utils .validate_upload_keys (key_sources )
372+
373+ # Changing the shared limit can close files belonging to other uploads.
366374 _fd_manager .set_max_open_files (max_open_files )
367375
368376 send_files = []
369- # Directories do not produce multipart parts, so exclude them before validating keys.
370- upload_files = [path for path in files if not os .path .isdir (path .replace ("\\ " , "/" ))]
371- key_sources = Utils .prepare_upload_keys (upload_files , workspace , base_path , base_paths )
372377 for key , source_path in key_sources :
373378 file_path = source_path .replace ("\\ " , "/" )
374379
0 commit comments