bugfix
This commit is contained in:
parent
df206d076c
commit
83c0d725fd
65
appPublic/base64_to_file.py
Normal file
65
appPublic/base64_to_file.py
Normal file
@ -0,0 +1,65 @@
|
||||
import os
|
||||
import base64
|
||||
from appPuyblic.uniqueID import getID
|
||||
|
||||
function getFileExtensionFromBase64(base64String) {
|
||||
// Extract MIME type from data URL
|
||||
const mimeType = base64String.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
|
||||
|
||||
if (mimeType && mimeType[1]) {
|
||||
const mime = mimeType[1];
|
||||
const mimeToExtension = {
|
||||
# 图片
|
||||
"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",
|
||||
};
|
||||
|
||||
return mimeToExtension.get(mime, '');
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
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
|
||||
name = getID()
|
||||
ext = getFileExtensionFromBase64(base64_string)
|
||||
fname = f'{name}{ext}'
|
||||
fp = os.path.join(output_path, fname)
|
||||
with open(fp, 'wb') as file:
|
||||
file.write(binary_data)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user