Delete api/files.py
Browse files- api/files.py +0 -144
api/files.py
DELETED
|
@@ -1,144 +0,0 @@
|
|
| 1 |
-
import io
|
| 2 |
-
|
| 3 |
-
import pybase64
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
from utils.Client import Client
|
| 7 |
-
from utils.config import export_proxy_url, cf_file_url
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
async def get_file_content(url):
|
| 11 |
-
if url.startswith("data:"):
|
| 12 |
-
mime_type, base64_data = url.split(';')[0].split(':')[1], url.split(',')[1]
|
| 13 |
-
file_content = pybase64.b64decode(base64_data)
|
| 14 |
-
return file_content, mime_type
|
| 15 |
-
else:
|
| 16 |
-
client = Client()
|
| 17 |
-
try:
|
| 18 |
-
if cf_file_url:
|
| 19 |
-
body = {"file_url": url}
|
| 20 |
-
r = await client.post(cf_file_url, timeout=60, json=body)
|
| 21 |
-
else:
|
| 22 |
-
r = await client.get(url, proxy=export_proxy_url, timeout=60)
|
| 23 |
-
if r.status_code != 200:
|
| 24 |
-
return None, None
|
| 25 |
-
file_content = r.content
|
| 26 |
-
mime_type = r.headers.get('Content-Type', '').split(';')[0].strip()
|
| 27 |
-
return file_content, mime_type
|
| 28 |
-
finally:
|
| 29 |
-
await client.close()
|
| 30 |
-
del client
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
async def determine_file_use_case(mime_type):
|
| 34 |
-
multimodal_types = ["image/jpeg", "image/webp", "image/png", "image/gif"]
|
| 35 |
-
my_files_types = ["text/x-php", "application/msword", "text/x-c", "text/html",
|
| 36 |
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 37 |
-
"application/json", "text/javascript", "application/pdf",
|
| 38 |
-
"text/x-java", "text/x-tex", "text/x-typescript", "text/x-sh",
|
| 39 |
-
"text/x-csharp", "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
| 40 |
-
"text/x-c++", "application/x-latext", "text/markdown", "text/plain",
|
| 41 |
-
"text/x-ruby", "text/x-script.python"]
|
| 42 |
-
|
| 43 |
-
if mime_type in multimodal_types:
|
| 44 |
-
return "multimodal"
|
| 45 |
-
elif mime_type in my_files_types:
|
| 46 |
-
return "my_files"
|
| 47 |
-
else:
|
| 48 |
-
return "ace_upload"
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
async def get_image_size(file_content):
|
| 52 |
-
with Image.open(io.BytesIO(file_content)) as img:
|
| 53 |
-
return img.width, img.height
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
async def get_file_extension(mime_type):
|
| 57 |
-
extension_mapping = {
|
| 58 |
-
"image/jpeg": ".jpg",
|
| 59 |
-
"image/png": ".png",
|
| 60 |
-
"image/gif": ".gif",
|
| 61 |
-
"image/webp": ".webp",
|
| 62 |
-
"text/x-php": ".php",
|
| 63 |
-
"application/msword": ".doc",
|
| 64 |
-
"text/x-c": ".c",
|
| 65 |
-
"text/html": ".html",
|
| 66 |
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
|
| 67 |
-
"application/json": ".json",
|
| 68 |
-
"text/javascript": ".js",
|
| 69 |
-
"application/pdf": ".pdf",
|
| 70 |
-
"text/x-java": ".java",
|
| 71 |
-
"text/x-tex": ".tex",
|
| 72 |
-
"text/x-typescript": ".ts",
|
| 73 |
-
"text/x-sh": ".sh",
|
| 74 |
-
"text/x-csharp": ".cs",
|
| 75 |
-
"application/vnd.openxmlformats-officedocument.presentationml.presentation": ".pptx",
|
| 76 |
-
"text/x-c++": ".cpp",
|
| 77 |
-
"application/x-latex": ".latex",
|
| 78 |
-
"text/markdown": ".md",
|
| 79 |
-
"text/plain": ".txt",
|
| 80 |
-
"text/x-ruby": ".rb",
|
| 81 |
-
"text/x-script.python": ".py",
|
| 82 |
-
"application/zip": ".zip",
|
| 83 |
-
"application/x-zip-compressed": ".zip",
|
| 84 |
-
"application/x-tar": ".tar",
|
| 85 |
-
"application/x-compressed-tar": ".tar.gz",
|
| 86 |
-
"application/vnd.rar": ".rar",
|
| 87 |
-
"application/x-rar-compressed": ".rar",
|
| 88 |
-
"application/x-7z-compressed": ".7z",
|
| 89 |
-
"application/octet-stream": ".bin",
|
| 90 |
-
"audio/mpeg": ".mp3",
|
| 91 |
-
"audio/wav": ".wav",
|
| 92 |
-
"audio/ogg": ".ogg",
|
| 93 |
-
"audio/aac": ".aac",
|
| 94 |
-
"video/mp4": ".mp4",
|
| 95 |
-
"video/x-msvideo": ".avi",
|
| 96 |
-
"video/x-matroska": ".mkv",
|
| 97 |
-
"video/webm": ".webm",
|
| 98 |
-
"application/rtf": ".rtf",
|
| 99 |
-
"application/vnd.ms-excel": ".xls",
|
| 100 |
-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
| 101 |
-
"text/css": ".css",
|
| 102 |
-
"text/xml": ".xml",
|
| 103 |
-
"application/xml": ".xml",
|
| 104 |
-
"application/vnd.android.package-archive": ".apk",
|
| 105 |
-
"application/vnd.apple.installer+xml": ".mpkg",
|
| 106 |
-
"application/x-bzip": ".bz",
|
| 107 |
-
"application/x-bzip2": ".bz2",
|
| 108 |
-
"application/x-csh": ".csh",
|
| 109 |
-
"application/x-debian-package": ".deb",
|
| 110 |
-
"application/x-dvi": ".dvi",
|
| 111 |
-
"application/java-archive": ".jar",
|
| 112 |
-
"application/x-java-jnlp-file": ".jnlp",
|
| 113 |
-
"application/vnd.mozilla.xul+xml": ".xul",
|
| 114 |
-
"application/vnd.ms-fontobject": ".eot",
|
| 115 |
-
"application/ogg": ".ogx",
|
| 116 |
-
"application/x-font-ttf": ".ttf",
|
| 117 |
-
"application/font-woff": ".woff",
|
| 118 |
-
"application/x-shockwave-flash": ".swf",
|
| 119 |
-
"application/vnd.visio": ".vsd",
|
| 120 |
-
"application/xhtml+xml": ".xhtml",
|
| 121 |
-
"application/vnd.ms-powerpoint": ".ppt",
|
| 122 |
-
"application/vnd.oasis.opendocument.text": ".odt",
|
| 123 |
-
"application/vnd.oasis.opendocument.spreadsheet": ".ods",
|
| 124 |
-
"application/x-xpinstall": ".xpi",
|
| 125 |
-
"application/vnd.google-earth.kml+xml": ".kml",
|
| 126 |
-
"application/vnd.google-earth.kmz": ".kmz",
|
| 127 |
-
"application/x-font-otf": ".otf",
|
| 128 |
-
"application/vnd.ms-excel.addin.macroEnabled.12": ".xlam",
|
| 129 |
-
"application/vnd.ms-excel.sheet.binary.macroEnabled.12": ".xlsb",
|
| 130 |
-
"application/vnd.ms-excel.template.macroEnabled.12": ".xltm",
|
| 131 |
-
"application/vnd.ms-powerpoint.addin.macroEnabled.12": ".ppam",
|
| 132 |
-
"application/vnd.ms-powerpoint.presentation.macroEnabled.12": ".pptm",
|
| 133 |
-
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12": ".ppsm",
|
| 134 |
-
"application/vnd.ms-powerpoint.template.macroEnabled.12": ".potm",
|
| 135 |
-
"application/vnd.ms-word.document.macroEnabled.12": ".docm",
|
| 136 |
-
"application/vnd.ms-word.template.macroEnabled.12": ".dotm",
|
| 137 |
-
"application/x-ms-application": ".application",
|
| 138 |
-
"application/x-ms-wmd": ".wmd",
|
| 139 |
-
"application/x-ms-wmz": ".wmz",
|
| 140 |
-
"application/x-ms-xbap": ".xbap",
|
| 141 |
-
"application/vnd.ms-xpsdocument": ".xps",
|
| 142 |
-
"application/x-silverlight-app": ".xap"
|
| 143 |
-
}
|
| 144 |
-
return extension_mapping.get(mime_type, "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|