將压缩包放在zippath="D:/xx/xx/xx"中,解压到pathto="D:/xx/xx1/xx"中
type UploaddeployLogic struct {logx.Loggerctx context.ContextsvcCtx *svc.ServiceContextr *http.Request
}func NewUploaddeployLogic(r *http.Request, svcCtx *svc.ServiceContext) *UploaddeployLogic {return &UploaddeployLogic{Logger: logx.WithContext(r.Context()),ctx: r.Context(),svcCtx: svcCtx,r: r,}
}
func (l *UploaddeployLogic) Uploaddeploy() (resp *types.UploadResp, err error) {fmt.Println("Uploaddeploy")// 解析上传的multipart表单数据// 从表单中获取名为"file"的文件。file, handler, err := l.r.FormFile("file")if err != nil {logx.Error("the value of file cannot be found")return nil, errorx.NewCodeError(errorcode.InvalidArgument,l.svcCtx.Trans.Trans(l.ctx, "file.parseFormFailed"))}// 延迟关闭文件,在函数返回时自动执行。这确保了无论后续代码路径如何,文件都会被正确关闭。defer file.Close()// 校验后缀是否合法,校验有没有后缀dotIndex := strings.LastIndex(handler.Filename, ".")// 拒绝无后缀文件if dotIndex == -1 {logx.Errorw("reject the file which does not have suffix")return nil, errorx.NewCodeError(errorcode.InvalidArgument,l.svcCtx.Trans.Trans(l.ctx, "file.wrongTypeError"))}_, fileSuffix := handler.Filename[:dotIndex], handler.Filename[dotIndex+1:]fileUUID := uuidx.NewUUID()storeFileName := fileUUID.String() + "." + fileSuffix// 判断文件大小是否超过设定值fileType := strings.Split(handler.Header.Get("Content-Type"), "/")[0]if fileType != "image" && fileType != "video" && fileType != "audio" {fileType = "other"}zipPath := l.r.Form["zippath"][0]pathto := l.r.Form["pathto"][0]// 这段代码用于检查公共存储目录是否存在,如果不存在则创建该目录。if !fileutil.IsExist(zipPath) {err = fileutil.CreateDir(zipPath + "/")if err != nil {logx.Errorw("failed to create directory for storing zip files", logx.Field("path", zipPath))return nil, errorx.NewCodeError(errorcode.Internal,l.svcCtx.Trans.Trans(l.ctx, i18n.Failed))}}// 将上传的文件保存到zipPath中zipFilePath := path.Join(zipPath, handler.Filename)targetFile, err := os.Create(zipFilePath)_, err = io.Copy(targetFile, file)if err != nil {logx.Errorw("fail to create file", logx.Field("path", path.Join(zipPath, storeFileName)))return nil, errorx.NewCodeError(errorcode.Internal,l.svcCtx.Trans.Trans(l.ctx, i18n.Failed))}targetFile.Close()// 创建目标路径if err := os.MkdirAll(pathto, os.ModePerm); err != nil {logx.Errorw("failed to create target directory", logx.Field("path", pathto), logx.Field("error", err))return nil, errorx.NewCodeError(errorcode.Internal, l.svcCtx.Trans.Trans(l.ctx, i18n.Failed))}// 解压缩文件到pathto中err = unzipFile(zipFilePath, pathto)if err != nil {logx.Errorw("fail to create file", logx.Field("path", path.Join(pathto, storeFileName)))return nil, errorx.NewCodeError(errorcode.Internal,l.svcCtx.Trans.Trans(l.ctx, i18n.Failed))}return &types.UploadResp{BaseDataInfo: types.BaseDataInfo{Msg: l.svcCtx.Trans.Trans(l.ctx, i18n.Success)},Data: types.UploadInfo{Name: handler.Filename, Url: l.svcCtx.Config.UploadConf.ServerURL},}, nil
}func unzipFile(zipFilePath, destDir string) error {r, err := zip.OpenReader(zipFilePath)if err != nil {return err}defer r.Close()for _, f := range r.File {rc, err := f.Open()if err != nil {return err}defer rc.Close()targetFilePath := filepath.Join(destDir, f.Name)if f.FileInfo().IsDir() {os.MkdirAll(targetFilePath, os.ModePerm)} else {targetFile, err := os.Create(targetFilePath)if err != nil {return err}defer targetFile.Close()_, err = io.Copy(targetFile, rc)if err != nil {return err}}}return nil
}