@@ -532,6 +532,25 @@ def from_data_uri(cls, data_uri: str) -> BinaryContent:
532532 media_type , data = data_uri [len (prefix ) :].split (';base64,' , 1 )
533533 return cls .narrow_type (cls (data = base64 .b64decode (data ), media_type = media_type ))
534534
535+ @classmethod
536+ def from_path (cls , path : PathLike [str ]) -> BinaryContent :
537+ """Create a `BinaryContent` from a path.
538+
539+ Defaults to 'application/octet-stream' if the media type cannot be inferred.
540+
541+ Raises:
542+ FileNotFoundError: if the file does not exist.
543+ PermissionError: if the file cannot be read.
544+ """
545+ path = Path (path )
546+ if not path .exists ():
547+ raise FileNotFoundError (f'File not found: { path } ' )
548+ media_type , _ = guess_type (path .as_posix ())
549+ if media_type is None :
550+ media_type = 'application/octet-stream'
551+
552+ return cls (data = path .read_bytes (), media_type = media_type )
553+
535554 @pydantic .computed_field
536555 @property
537556 def identifier (self ) -> str :
@@ -1074,26 +1093,6 @@ def has_content(self) -> bool:
10741093 """Return `True` if the file content is non-empty."""
10751094 return bool (self .content .data )
10761095
1077- @classmethod
1078- def from_path (cls , path : PathLike [str ]) -> FilePart :
1079- """Create a `BinaryContent` from a path.
1080-
1081- Defaults to 'application/octet-stream' if the media type cannot be inferred.
1082-
1083- Raises:
1084- FileNotFoundError: if the file does not exist.
1085- PermissionError: if the file cannot be read.
1086- """
1087- path = Path (path )
1088- if not path .exists ():
1089- raise FileNotFoundError (f'File not found: { path } ' )
1090- media_type , _ = guess_type (path .as_posix ())
1091- if media_type is None :
1092- media_type = 'application/octet-stream'
1093-
1094- binary_content = BinaryContent (data = path .read_bytes (), media_type = media_type )
1095- return cls (content = binary_content )
1096-
10971096 __repr__ = _utils .dataclasses_no_defaults_repr
10981097
10991098
0 commit comments