Explorar el Código

support upload

Xiaopeng Zhang hace 6 meses
padre
commit
4f45523cba
Se han modificado 1 ficheros con 40 adiciones y 6 borrados
  1. 40 6
      main.py

+ 40 - 6
main.py

@@ -4,10 +4,11 @@ from fastapi import FastAPI
 import os
 import uuid
 import re
+import zipfile
 import json
 from fastapi.middleware.cors import CORSMiddleware
 from fastapi.middleware.gzip import GZipMiddleware
-
+import base64
 from pydantic import BaseModel
 from subdir import service
 from subdir import db
@@ -630,11 +631,44 @@ async def updatezjcs(r: UpdateZjcs):
 
 @app.post("/upload/")
 async def upload(file: UploadFile):
-    
-    content = await file.read()
-    string = content.decode('utf-8')
-    await resolve(string)
-    return [file.filename]
+    if file.filename.endswith('13jz') or file.filename.endswith("13jt"):
+        content = await file.read()
+        string = content.decode('utf-8')
+        await resolve(string)
+        return [file.filename]
+    elif file.filename.endswith("jszf"):
+        content = await file.read()
+        string = content.decode('utf-8')
+        root = ET.fromstring(string)
+        for child in root:
+            print(child)
+            if child.tag == "ZBFileContent":
+                base64_bytes = child.text.encode("ascii")
+
+                sample_string_bytes = base64.b64decode(base64_bytes)
+        tmp = str(uuid.uuid4())
+        os.mkdir(os.path.join("tmp", tmp))
+        with open(tmp, "wb") as file_:
+            file_.write(sample_string_bytes)
+        with zipfile.ZipFile(tmp, 'r') as zip_ref:
+            zip_ref.extractall(os.path.join("tmp", tmp))
+        dir_list = os.listdir(os.path.join("tmp", tmp))
+        print(dir_list)
+        for entry in dir_list:
+            if entry.endswith("13jz"):
+                with open(os.path.join("tmp", tmp, entry), 'r') as f:
+                    await resolve(f.read()) 
+
+        return [file.filename]
+    else:
+        return [file.filename]
+        
+        
+        
+        
+
+
+