@@ -242,6 +242,30 @@ def __init__(self, **kwargs):
242
242
self ._catalog = None
243
243
self ._caching_enabled : bool = False
244
244
245
+ def as_text_file (self ) -> "TextFile" :
246
+ """Convert the file to a `TextFile` object."""
247
+ if isinstance (self , TextFile ):
248
+ return self
249
+ file = TextFile (** self .model_dump ())
250
+ file ._set_stream (self ._catalog , caching_enabled = self ._caching_enabled )
251
+ return file
252
+
253
+ def as_image_file (self ) -> "ImageFile" :
254
+ """Convert the file to a `ImageFile` object."""
255
+ if isinstance (self , ImageFile ):
256
+ return self
257
+ file = ImageFile (** self .model_dump ())
258
+ file ._set_stream (self ._catalog , caching_enabled = self ._caching_enabled )
259
+ return file
260
+
261
+ def as_video_file (self ) -> "VideoFile" :
262
+ """Convert the file to a `VideoFile` object."""
263
+ if isinstance (self , VideoFile ):
264
+ return self
265
+ file = VideoFile (** self .model_dump ())
266
+ file ._set_stream (self ._catalog , caching_enabled = self ._caching_enabled )
267
+ return file
268
+
245
269
@classmethod
246
270
def upload (
247
271
cls , data : bytes , path : str , catalog : Optional ["Catalog" ] = None
@@ -291,20 +315,20 @@ def open(self, mode: Literal["rb", "r"] = "rb") -> Iterator[Any]:
291
315
) as f :
292
316
yield io .TextIOWrapper (f ) if mode == "r" else f
293
317
294
- def read (self , length : int = - 1 ):
295
- """Returns file contents."""
318
+ def read_bytes (self , length : int = - 1 ):
319
+ """Returns file contents as bytes ."""
296
320
with self .open () as stream :
297
321
return stream .read (length )
298
322
299
- def read_bytes (self ):
300
- """Returns file contents as bytes."""
301
- return self .read ()
302
-
303
323
def read_text (self ):
304
324
"""Returns file contents as text."""
305
325
with self .open (mode = "r" ) as stream :
306
326
return stream .read ()
307
327
328
+ def read (self , length : int = - 1 ):
329
+ """Returns file contents."""
330
+ return self .read_bytes (length )
331
+
308
332
def save (self , destination : str , client_config : Optional [dict ] = None ):
309
333
"""Writes it's content to destination"""
310
334
destination = stringify_path (destination )
@@ -547,20 +571,36 @@ def save(self, destination: str, client_config: Optional[dict] = None):
547
571
class ImageFile (File ):
548
572
"""`DataModel` for reading image files."""
549
573
574
+ def get_info (self ) -> "Image" :
575
+ """
576
+ Retrieves metadata and information about the image file.
577
+
578
+ Returns:
579
+ Image: A Model containing image metadata such as width, height and format.
580
+ """
581
+ from .image import image_info
582
+
583
+ return image_info (self )
584
+
550
585
def read (self ):
551
586
"""Returns `PIL.Image.Image` object."""
552
587
from PIL import Image as PilImage
553
588
554
589
fobj = super ().read ()
555
590
return PilImage .open (BytesIO (fobj ))
556
591
557
- def save (self , destination : str , client_config : Optional [dict ] = None ):
592
+ def save ( # type: ignore[override]
593
+ self ,
594
+ destination : str ,
595
+ format : Optional [str ] = None ,
596
+ client_config : Optional [dict ] = None ,
597
+ ):
558
598
"""Writes it's content to destination"""
559
599
destination = stringify_path (destination )
560
600
561
601
client : Client = self ._catalog .get_client (destination , ** (client_config or {}))
562
602
with client .fs .open (destination , mode = "wb" ) as f :
563
- self .read ().save (f )
603
+ self .read ().save (f , format = format )
564
604
565
605
566
606
class Image (DataModel ):
0 commit comments