Merge branch 'main' of git.opencomputing.cn:yumoqing/apppublic
This commit is contained in:
commit
079d78a83d
60
appPublic/base64_to_file.py
Normal file
60
appPublic/base64_to_file.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import os
|
||||||
|
import re
|
||||||
|
import base64
|
||||||
|
from appPublic.uniqueID import getID
|
||||||
|
|
||||||
|
MIME_EXT = {
|
||||||
|
# 图片
|
||||||
|
"image/jpeg": "jpg",
|
||||||
|
"image/png": "png",
|
||||||
|
"image/gif": "gif",
|
||||||
|
"image/webp": "webp",
|
||||||
|
"image/bmp": "bmp",
|
||||||
|
"image/svg+xml": "svg",
|
||||||
|
"image/x-icon": "ico",
|
||||||
|
"image/tiff": "tiff",
|
||||||
|
|
||||||
|
# 音频
|
||||||
|
"audio/mpeg": "mp3",
|
||||||
|
"audio/wav": "wav",
|
||||||
|
"audio/ogg": "ogg",
|
||||||
|
"audio/webm": "weba",
|
||||||
|
"audio/aac": "aac",
|
||||||
|
"audio/flac": "flac",
|
||||||
|
"audio/mp4": "m4a",
|
||||||
|
"audio/3gpp": "3gp",
|
||||||
|
|
||||||
|
# 视频
|
||||||
|
"video/mp4": "mp4",
|
||||||
|
"video/webm": "webm",
|
||||||
|
"video/ogg": "ogv",
|
||||||
|
"video/x-msvideo": "avi",
|
||||||
|
"video/quicktime": "mov",
|
||||||
|
"video/x-matroska": "mkv",
|
||||||
|
"video/3gpp": "3gp",
|
||||||
|
"video/x-flv": "flv",
|
||||||
|
}
|
||||||
|
|
||||||
|
def getFilenameFromBase64(base64String):
|
||||||
|
match = re.match(r"data:(.*?);base64,(.*)", base64String)
|
||||||
|
if not match:
|
||||||
|
raise ValueError("不是合法的 base64 Data URL")
|
||||||
|
mime_type, b64_data = match.groups()
|
||||||
|
ext = MIME_EXT.get(mime_type, mime_type.split("/")[-1])
|
||||||
|
name = getID()
|
||||||
|
fname = f'{name}.{ext}'
|
||||||
|
return fname
|
||||||
|
|
||||||
|
def base64_to_file(base64_string, output_path):
|
||||||
|
# Remove data URL prefix if present (e.g., "data:image/png;base64,")
|
||||||
|
if ',' in base64_string:
|
||||||
|
header, base64_data = base64_string.split(',', 1)
|
||||||
|
else:
|
||||||
|
base64_data = base64_string
|
||||||
|
|
||||||
|
# Decode Base64 string
|
||||||
|
binary_data = base64.b64decode(base64_data)
|
||||||
|
|
||||||
|
# Write binary data to file
|
||||||
|
with open(output_path, 'wb') as file:
|
||||||
|
file.write(binary_data)
|
||||||
@ -48,22 +48,22 @@ def timestamp2datatiemStr(ts):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def findAllDrives():
|
def findAllDrives():
|
||||||
Drives=[]
|
Drives=[]
|
||||||
# print "Searching for drives..."
|
# print "Searching for drives..."
|
||||||
drives=win32api.GetLogicalDriveStrings().split(":")
|
drives=win32api.GetLogicalDriveStrings().split(":")
|
||||||
for i in drives:
|
for i in drives:
|
||||||
# print "i=",i,":"
|
# print "i=",i,":"
|
||||||
dr=i[-1].lower()
|
dr=i[-1].lower()
|
||||||
if dr.isalpha():
|
if dr.isalpha():
|
||||||
dr+=":\\"
|
dr+=":\\"
|
||||||
inf=None
|
inf=None
|
||||||
try:
|
try:
|
||||||
inf=win32api.GetVolumeInformation(dr)
|
inf=win32api.GetVolumeInformation(dr)
|
||||||
except:
|
except:
|
||||||
pass # Removable drive, not ready
|
pass # Removable drive, not ready
|
||||||
# You'll still get the drive letter, but inf will be None
|
# You'll still get the drive letter, but inf will be None
|
||||||
Drives.append([dr,inf])
|
Drives.append([dr,inf])
|
||||||
return Drives
|
return Drives
|
||||||
"""
|
"""
|
||||||
|
|
||||||
## list all folder name under folder named by path
|
## list all folder name under folder named by path
|
||||||
@ -77,19 +77,19 @@ def listFolder(path, rescursive=False) :
|
|||||||
yield full_name
|
yield full_name
|
||||||
|
|
||||||
def listFile(folder,suffixs=[],rescursive=False):
|
def listFile(folder,suffixs=[],rescursive=False):
|
||||||
subffixs = [ i.lower() for i in suffixs ]
|
subffixs = [ i.lower() for i in suffixs ]
|
||||||
for f in os.listdir(folder):
|
for f in os.listdir(folder):
|
||||||
p = os.path.join(folder,f)
|
p = os.path.join(folder,f)
|
||||||
if rescursive and os.path.isdir(p):
|
if rescursive and os.path.isdir(p):
|
||||||
for p1 in listFile(p,suffixs=suffixs,rescursive=True):
|
for p1 in listFile(p,suffixs=suffixs,rescursive=True):
|
||||||
yield p1
|
yield p1
|
||||||
if os.path.isfile(p):
|
if os.path.isfile(p):
|
||||||
e = p.lower()
|
e = p.lower()
|
||||||
if suffixs == [] :
|
if suffixs == [] :
|
||||||
yield p
|
yield p
|
||||||
for s in subffixs:
|
for s in subffixs:
|
||||||
if e.endswith(s):
|
if e.endswith(s):
|
||||||
yield p
|
yield p
|
||||||
|
|
||||||
def folderInfo(root,uri=''):
|
def folderInfo(root,uri=''):
|
||||||
relpath = uri
|
relpath = uri
|
||||||
@ -137,23 +137,12 @@ def rmdir_recursive(dir):
|
|||||||
os.rmdir(dir)
|
os.rmdir(dir)
|
||||||
|
|
||||||
def _mkdir(newdir) :
|
def _mkdir(newdir) :
|
||||||
"""works the way a good mkdir should :)
|
"""works the way a good mkdir should :)
|
||||||
- already exists, silently complete
|
- already exists, silently complete
|
||||||
- regular file in the way, raise an exception
|
- regular file in the way, raise an exception
|
||||||
- parent directory(ies) does not exist, make them as well
|
- parent directory(ies) does not exist, make them as well
|
||||||
"""
|
"""
|
||||||
if os.path.isdir(newdir):
|
return os.makedirs(newdir, exist_ok=True)
|
||||||
pass
|
|
||||||
elif os.path.isfile(newdir):
|
|
||||||
raise OSError("a file with the same name as the desired " \
|
|
||||||
"dir, '%s', already exists." % newdir)
|
|
||||||
else:
|
|
||||||
head, tail = os.path.split(newdir)
|
|
||||||
if head and not os.path.isdir(head):
|
|
||||||
_mkdir(head)
|
|
||||||
#print "_mkdir %s" % repr(newdir)
|
|
||||||
if tail:
|
|
||||||
os.mkdir(newdir)
|
|
||||||
|
|
||||||
def _copyfile(fp,dir) :
|
def _copyfile(fp,dir) :
|
||||||
fs = open(fp,'rb')
|
fs = open(fp,'rb')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user