18
from composio.exceptions import (
19
ErrorDownloadingFile,
20
ErrorUploadingFile,
21
+ FileUploadAbortedError,
22
ResponseTooLargeError,
23
SDKFileNotFoundError,
24
)
25
from composio.utils import mimetypes
26
+from composio.utils.sensitive_file_upload_paths import (
27
+ assert_safe_local_file_upload_path,
28
+)
29
from composio.utils.logging import WithLogger
30
31
if t.TYPE_CHECKING:
420
file: t.Union[str, Path],
421
tool: str,
422
toolkit: str,
423
+ *,
424
+ sensitive_file_upload_protection: bool = True,
425
+ file_upload_path_deny_segments: t.Optional[t.Sequence[str]] = None,
426
+ before_file_upload: t.Optional[
427
+ t.Callable[[str, str, str], t.Union[str, bool]]
428
+ ] = None,
429
) -> te.Self:
430
"""Create a FileUploadable from a local file path or public URL.
431
437
:param file: Local file path or public URL
438
:param tool: The tool slug
439
:param toolkit: The toolkit slug
440
+ :param sensitive_file_upload_protection: When True, block paths on the built-in denylist.
441
+ :param file_upload_path_deny_segments: Extra path segment names to merge with the built-in list.
442
+ :param before_file_upload: Optional ``(path, tool, toolkit) -> str | bool``; return a new path string, or False to abort.
443
:return: FileUploadable instance with S3 key
444
"""
445
+ file_str = str(file) if isinstance(file, Path) else file
446
+
447
# Check if it's a URL
433
- if isinstance(file, str) and _is_url(file):
434
- return cls.from_url(client=client, url=file, tool=tool, toolkit=toolkit)
448
+ if isinstance(file_str, str) and _is_url(file_str):
449
+ path_in = file_str
450
+ if before_file_upload is not None:
451
+ out = before_file_upload(path_in, tool, toolkit)
452
+ if out is False:
453
+ raise FileUploadAbortedError(
454
+ "File upload was aborted because before_file_upload returned False."
455
+ )
456
+ if isinstance(out, str):
457
+ path_in = out
458
+ return cls.from_url(client=client, url=path_in, tool=tool, toolkit=toolkit)
459
+
460
+ path_in = file_str
461
+ if before_file_upload is not None:
462
+ out = before_file_upload(path_in, tool, toolkit)
463
+ if out is False:
464
+ raise FileUploadAbortedError(
465
+ "File upload was aborted because before_file_upload returned False."
466
+ )
467
+ if isinstance(out, str):
468
+ path_in = out
469
+
470
+ assert_safe_local_file_upload_path(
471
+ path_in,
472
+ enabled=sensitive_file_upload_protection,
473
+ additional_deny_segments=file_upload_path_deny_segments,
474
+ )
475
476
# Handle as local file path
437
- file = Path(file)
477
+ file = Path(path_in)
478
if not file.exists():
479
raise SDKFileNotFoundError(
480
f"File not found: {file}. Please provide a valid file path."
527
return outfile
528
529
530
+BeforeFileUpload = t.Callable[[str, str, str], t.Union[str, bool]]
531
+
532
+
533
class FileHelper(WithLogger):
491
- def __init__(self, client: HttpClient, outdir: t.Optional[str] = None):
534
+ def __init__(
535
+ self,
536
+ client: HttpClient,
537
+ outdir: t.Optional[str] = None,
538
+ *,
539
+ sensitive_file_upload_protection: bool = True,
540
+ file_upload_path_deny_segments: t.Optional[t.Sequence[str]] = None,
541
+ ) -> None:
542
super().__init__()
543
self._client = client
544
self._outdir = Path(outdir or LOCAL_OUTPUT_FILE_DIRECTORY)
545
+ self._sensitive_file_upload_protection = sensitive_file_upload_protection
546
+ self._file_upload_path_deny_segments = file_upload_path_deny_segments
547
548
def _has_file_property(
549
self, schema: t.Dict, property_name: str = "file_uploadable"
743
tool: Tool,
744
schema: t.Dict,
745
request: t.Dict,
746
+ *,
747
+ before_file_upload: t.Optional[BeforeFileUpload] = None,
748
) -> t.Dict:
749
if "properties" not in schema:
750
return request
768
file=request[_param],
769
tool=tool.slug,
770
toolkit=tool.toolkit.slug,
771
+ sensitive_file_upload_protection=self._sensitive_file_upload_protection,
772
+ file_upload_path_deny_segments=self._file_upload_path_deny_segments,
773
+ before_file_upload=before_file_upload,
774
).model_dump()
775
continue
776
788
file=request[_param],
789
tool=tool.slug,
790
toolkit=tool.toolkit.slug,
791
+ sensitive_file_upload_protection=self._sensitive_file_upload_protection,
792
+ file_upload_path_deny_segments=self._file_upload_path_deny_segments,
793
+ before_file_upload=before_file_upload,
794
).model_dump()
795
continue
796
803
schema=uploadable_variant,
804
request=request[_param],
805
tool=tool,
806
+ before_file_upload=before_file_upload,
807
)
808
continue
809
816
schema=param_schema,
817
request=request[_param],
818
tool=tool,
819
+ before_file_upload=before_file_upload,
820
)
821
continue
822
839
file=item,
840
tool=tool.slug,
841
toolkit=tool.toolkit.slug,
842
+ sensitive_file_upload_protection=self._sensitive_file_upload_protection,
843
+ file_upload_path_deny_segments=self._file_upload_path_deny_segments,
844
+ before_file_upload=before_file_upload,
845
).model_dump()
846
)
847
elif isinstance(item, dict):
850
schema=items_schema,
851
request=item,
852
tool=tool,
853
+ before_file_upload=before_file_upload,
854
)
855
)
856
else:
861
862
return request
863
798
- def substitute_file_uploads(self, tool: Tool, request: t.Dict) -> t.Dict:
864
+ def substitute_file_uploads(
865
+ self,
866
+ tool: Tool,
867
+ request: t.Dict,
868
+ *,
869
+ before_file_upload: t.Optional[BeforeFileUpload] = None,
870
+ ) -> t.Dict:
871
return self._substitute_file_uploads_recursively(
872
tool=tool,
873
schema=tool.input_parameters,
874
request=request,
875
+ before_file_upload=before_file_upload,
876
)
877
878
def _is_file_downloadable(self, schema: t.Dict) -> bool: