File: data_uri.py

package info (click to toggle)
zim 0.76.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,952 kB
  • sloc: python: 68,612; xml: 1,270; javascript: 512; sh: 101; makefile: 47
file content (23 lines) | stat: -rwxr-xr-x 445 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python3

import base64
import sys

from zim.gui.applications import get_mimetype
from zim.newfs import LocalFile


def data_uri(file):
	mimetype = get_mimetype(file)
	data64 = base64.b64encode(file.read_binary()).decode('utf-8')
	return f'data:{mimetype};base64,{data64}'


def main():
	file = LocalFile(sys.argv[1])
	assert file.exists(), f'File \'{file}\' does not exist'
	print(data_uri(file))


if __name__ == '__main__':
	main()