| 106 | |
| 107 | @rx.event(background=True) |
| 108 | async def handle_upload_stream( |
| 109 | self, chunk_iter: rx.UploadChunkIterator, field: str |
| 110 | ): |
| 111 | async with self: |
| 112 | self.upload_done = False |
| 113 | self.stream_field = field |
| 114 | upload_dir = rx.get_upload_dir() / "streaming" |
| 115 | file_handles: dict[str, Any] = {} |
| 116 | |
| 117 | try: |
| 118 | async for chunk in chunk_iter: |
| 119 | path = upload_dir / chunk.filename |
| 120 | path.parent.mkdir(parents=True, exist_ok=True) |
| 121 | |
| 122 | fh = file_handles.get(chunk.filename) |
| 123 | if fh is None: |
| 124 | fh = path.open("r+b") if path.exists() else path.open("wb") |
| 125 | file_handles[chunk.filename] = fh |
| 126 | |
| 127 | fh.seek(chunk.offset) |
| 128 | fh.write(chunk.data) |
| 129 | |
| 130 | async with self: |
| 131 | self.stream_chunk_records.append( |
| 132 | f"{chunk.filename}:{chunk.offset}:{len(chunk.data)}" |
| 133 | ) |
| 134 | finally: |
| 135 | for fh in file_handles.values(): |
| 136 | fh.close() |
| 137 | |
| 138 | async with self: |
| 139 | self.stream_completed_files = sorted(file_handles) |
| 140 | self.upload_done = True |
| 141 | |
| 142 | @rx.event |
| 143 | def do_download(self): |