28 lines
590 B
Python
Executable File
28 lines
590 B
Python
Executable File
#!/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))
|