-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoolBase64.py
67 lines (57 loc) · 2.4 KB
/
CoolBase64.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import string
import base64
import io
import webbrowser
import sublime_plugin
import sublime
class Base64EncodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
for s in view.sel():
if s.empty():
sublime.active_window().run_command("copy_file_as_base64")
break
encoded = base64.encodebytes(view.substr(s).encode())
view.replace(edit, s, encoded.decode().replace("\n", ""))
class Base64DecodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
for s in view.sel():
if s.empty():
break
decoded = base64.decodebytes(view.substr(s).encode())
decodedstring = decoded.decode().replace("\r", "")
view.replace(edit, s, decodedstring)
class CopyFileAsBase64Command(sublime_plugin.WindowCommand):
def run(self):
fileinfo = sublime.active_window().extract_variables()
encoded = io.BytesIO()
base64.encode(open(fileinfo["file"], "rb"), encoded)
encoded = encoded.getvalue().decode().replace("\n", "")
sublime.set_clipboard(encoded)
print(fileinfo["file_name"] + " copied as Base64")
class CopyFileAsBase64WebUrlCommand(sublime_plugin.WindowCommand):
def run(self):
fileinfo = sublime.active_window().extract_variables()
encoded = io.BytesIO()
base64.encode(open(fileinfo["file"], "rb"), encoded)
encoded = encoded.getvalue().decode().replace("\n", "")
fileext = fileinfo["file_extension"]
if (fileext == "jpg" or fileext == "png" or
fileext == "webp" or fileext == "gif" or
fileext == "jpeg"):
datatype = "image/%s" % (fileext)
elif (fileext == "mp3" or fileext == "ogg" or
fileext == "opus" or fileext == "wav" or
fileext == "flac" or fileext == "m4a" or
fileext == "aac"):
datatype = "audio/%s" % (fileext)
elif (fileext == "mp4" or fileext == "avi" or
fileext == "mkv" or fileext == "mov" or
fileext == "webm" or fileext == "3gp"):
datatype = "video/%s" % (fileext)
else:
datatype = "text"
url = "data:%s;base64,%s" % (datatype, encoded)
sublime.set_clipboard(url)
print("%s copied as %s Base64 Web URL" % (fileinfo["file_name"], datatype))