Adds datauri tool
This commit is contained in:
1 parent
ee15f271c6
commit
be3980d18e
1 file changed
+27
@@ -0,0 +1,27 @@
|
|||||||
|
#!/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))
|
||||||
Reference in new issue
Block a user