Last active
December 14, 2021 13:40
-
-
Save nejdetckenobi/66dcad8452c1a4cf0e03ec23339b6533 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from argparse import ArgumentParser | |
| from base64 import b64encode | |
| import mimetypes | |
| parser = ArgumentParser() | |
| parser.add_argument('file', type=str) | |
| parser.add_argument('-t', '--type', type=str) | |
| args = parser.parse_args() | |
| with open(args.file, 'rb') as f: | |
| data = f.read() | |
| encoded_data = b64encode(data).decode() | |
| if args.type is not None: | |
| mimetype = args.type | |
| else: | |
| mimetype = mimetypes.types_map.get( | |
| '.{}'.format(args.file.rsplit('.', 1)[-1]), | |
| 'application/octet-stream' | |
| ) | |
| print('data:{};base64,{}'.format(mimetype, encoded_data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment