import os
import shutil
import tempfile
import zipfile

import aiohttp
import aiohttp_jinja2
from ramjet.utils import logger

# DEST_DIR_PATH = "/home/laisky/test/zip"
DEST_DIR_PATH = "/opt/cwpp/prototype/oogway"


class UploadFileView(aiohttp.web.View):
    @aiohttp_jinja2.template("upload/proto.html")
    async def get(self):
        return

    async def post(self):
        zip_fname = "uploaded.zip"
        data = await self.request.post()
        assert data["file"], "must post file"
        logger.info("got uploaded proto file")
        with tempfile.TemporaryDirectory() as tmpdir:
            zip_fpath = os.path.join(tmpdir, zip_fname)
            with open(zip_fpath, "wb") as fp:
                fp.write(data["file"].file.read())

            extract_path = os.path.join(tmpdir, "extracted")
            extract_dir_name = ""
            with zipfile.ZipFile(zip_fpath) as fp:
                # 解压的文件夹名是乱码，暂时不清楚怎么处理，直接保存了拿来用
                extract_dir_name = fp.namelist()[0]
                fp.extractall(extract_path)

            logger.info(f"remove dir {DEST_DIR_PATH}")
            shutil.rmtree(DEST_DIR_PATH, ignore_errors=True)
            # os.mkdir(DEST_DIR_PATH)
            logger.info(f"update dir {DEST_DIR_PATH}")
            os.rename(
                os.path.join(extract_path, extract_dir_name),
                # os.path.join(extract_path).encode("gbk"),
                DEST_DIR_PATH,
            )

        return aiohttp.web.Response()

@@ -119,18 +119,16 @@
 logger%0A%0A
-# 
 DEST_DIR
@@ -159,16 +159,18 @@
 st/zip%22%0A
+# 
 DEST_DIR
@@ -1469,10 +1469,21 @@
 esponse(
+text=%22%E4%B8%8A%E4%BC%A0%E6%88%90%E5%8A%9F%22
 )%0A

add resp