1919class Blob :
2020 """Represents a JavaScript Blob object."""
2121
22+ data : str | bytes = ""
23+ mime_type : str = ""
24+
2225
2326BLOB_T = TypeVar ("BLOB_T" , bound = bytes | str , covariant = True )
2427
2528
2629class BlobVar (Var [BLOB_T ], python_types = Blob ):
2730 """A variable representing a JavaScript Blob object."""
2831
32+ @classmethod
33+ def _determine_mime_type (cls , value : str | bytes | Blob | Var ) -> str :
34+ mime_type = ""
35+ if isinstance (value , str | bytes | Blob ):
36+ match value :
37+ case str ():
38+ mime_type = "text/plain"
39+ case bytes ():
40+ mime_type = "application/octet-stream"
41+ case Blob ():
42+ mime_type = value .mime_type
43+
44+ elif isinstance (value , Var ):
45+ if isinstance (value ._var_type , str ):
46+ mime_type = "text/plain"
47+ if isinstance (value ._var_type , bytes ):
48+ mime_type = "application/octet-stream"
49+
50+ if not mime_type :
51+ msg = "Unable to determine mime type for blob creation."
52+ raise ValueError (msg )
53+
54+ return mime_type
55+
2956 @classmethod
3057 def create (
3158 cls ,
32- value : str | bytes | Var ,
33- mime_type : str | Var ,
59+ value : str | bytes | Blob | Var ,
60+ mime_type : str | Var | None = None ,
3461 _var_data : VarData | None = None ,
3562 ):
3663 """Create a BlobVar from the given value and MIME type.
@@ -43,12 +70,21 @@ def create(
4370 Returns:
4471 A BlobVar instance representing the JavaScript Blob object.
4572 """
46- if not isinstance (value , Var ):
47- value = LiteralVar .create (value )
73+ if mime_type is None :
74+ mime_type = cls ._determine_mime_type (value )
75+
4876 if not isinstance (mime_type , Var ):
4977 mime_type = LiteralVar .create (mime_type )
50- elif type (value ).__qualname__ .endswith ("BytesCastedVar" ):
78+
79+ if isinstance (value , str | bytes ):
80+ value = LiteralVar .create (value )
81+
82+ elif isinstance (value , Blob ):
83+ value = LiteralVar .create (value .data )
84+
85+ if isinstance (value ._var_type , bytes ):
5186 value = f"new Uint8Array({ value } )"
87+
5288 return cls (
5389 _js_expr = f"new Blob([{ value } ], {{ type: { mime_type } }})" ,
5490 _var_type = Blob ,
@@ -93,8 +129,8 @@ class LiteralBlobVar(LiteralVar, BlobVar):
93129 @classmethod
94130 def create (
95131 cls ,
96- value : bytes | str ,
97- mime_type : str ,
132+ value : bytes | str | Blob ,
133+ mime_type : str | None = None ,
98134 _var_data : VarData | None = None ,
99135 ) -> BlobVar :
100136 """Create a literal Blob variable from bytes or string data.
@@ -107,12 +143,21 @@ def create(
107143 Returns:
108144 A BlobVar instance representing the Blob.
109145 """
146+ if not mime_type :
147+ mime_type = cls ._determine_mime_type (value )
148+
149+ if isinstance (value , Blob ):
150+ value = value .data
151+
152+ var_type = type (value )
153+
154+ if isinstance (value , bytes ):
155+ value = f"new Uint8Array({ list (value )} )"
156+ else :
157+ value = f"'{ value } '"
158+
110159 return cls (
111- _js_expr = (
112- f"new Blob([new Uint8Array({ list (value )} )], {{ type: '{ mime_type } ' }})"
113- if isinstance (value , bytes )
114- else f"new Blob([{ value } ], {{ type: '{ mime_type } ' }})"
115- ),
116- _var_type = bytes ,
160+ _js_expr = f"new Blob([{ value } ], {{ type: '{ mime_type } ' }})" ,
161+ _var_type = var_type ,
117162 _var_data = _var_data ,
118163 )
0 commit comments