(self, text:str)
| 172 | |
| 173 | |
| 174 | def get_from_base64(self, text:str) -> Base64File: |
| 175 | file = Base64File() |
| 176 | try: |
| 177 | if text: |
| 178 | if text.startswith("data:image/"): |
| 179 | text = text.split(",")[1] |
| 180 | |
| 181 | content = None |
| 182 | content_bytes = None |
| 183 | |
| 184 | valid, _ = self._is_base64_valid(text) |
| 185 | |
| 186 | if valid: |
| 187 | decoded = self._decode(text) |
| 188 | content_bytes = decoded.content |
| 189 | if content_bytes: |
| 190 | try: |
| 191 | |
| 192 | text_type = magic.from_buffer(content_bytes, mime=True) |
| 193 | ext, format = self._get_extension(text_type) |
| 194 | if ext == 'plain': |
| 195 | ext = 'txt' |
| 196 | if text[-2:] == '==': |
| 197 | content = Util.get_readable_content(content_bytes) |
| 198 | elif Util.is_readable(content_bytes): |
| 199 | content = content_bytes.decode('latin') |
| 200 | else: |
| 201 | return file |
| 202 | elif ext == 'csv': |
| 203 | content = Util.get_readable_content(content_bytes) |
| 204 | elif ext == 'xml': |
| 205 | content = Util.get_readable_content(content_bytes) |
| 206 | elif ext == 'pdf': |
| 207 | content = self._read_pdf(content_bytes) |
| 208 | elif format == 'image': # We cannot convert to readable format |
| 209 | content = text |
| 210 | elif ext == 'zip': |
| 211 | content = self.get_zip_files(content_bytes) |
| 212 | elif format == 'application' and 'office' not in ext: |
| 213 | return file |
| 214 | |
| 215 | except UnicodeDecodeError as ue: |
| 216 | return file |
| 217 | |
| 218 | if content is None: |
| 219 | ext, content = self.get_file_type(content_bytes) |
| 220 | |
| 221 | file.bytearray = content_bytes |
| 222 | file.extension = ext |
| 223 | file.content = content |
| 224 | |
| 225 | except Exception as e: |
| 226 | AnsiPrint.print_debug(e) |
| 227 | |
| 228 | return file |
no test coverage detected