package controllers import ( "encoding/json" "eta/eta_pub/models" "eta/eta_pub/services" "eta/eta_pub/services/ppt2img" "eta/eta_pub/utils" "fmt" "io/ioutil" "os" "os/exec" "path" "strings" "time" ) type ImageController struct { BaseCommonController } // //// @Title 图片上传 //// @Description 图片上传接口 //// @Param Img query string true "图片base64字符串" //// @Param Sign query string true "策略组提供的资源id" //// @Success 200 新增成功 //// @router /svg_to_png [post] //func (this *ImageController) Base64Upload() { // br := new(models.BaseResponse).Init() // defer func() { // this.Data["json"] = br // this.ServeJSON() // }() // sign := this.GetString("Sign") // checkSign := "dIGi2M0VN21s3dKS" // if sign != checkSign { // br.Msg = "前面错误" // return // } // imgData := this.GetString("Img") // if imgData == "" { // br.Msg = "图片参数错误" // br.ErrMsg = "图片参数错误,Img Is Empty" // return // } // var err error // dateDir := time.Now().Format("20060102") // uploadDir := "static/imgs/" + dateDir // // if !utils.FileIsExist(uploadDir) { // err = os.MkdirAll(uploadDir, 777) // if err != nil { // br.Msg = "存储目录创建失败" // br.ErrMsg = "存储目录创建失败,Err:" + err.Error() // return // } // } // var saveToOssPath string // randStr := utils.GetRandStringNoSpecialChar(28) // var fileName, savePath, svgPath, outPath, outFileName string // imgData = strings.Replace(imgData, "data:image/svg+xml;base64,", "", -1) // fileName = randStr + ".svg" // savePath = uploadDir + "/" + fileName // svgPath = savePath // err = utils.SaveBase64ToFile(imgData, savePath) // if err != nil { // br.Msg = "图片保存失败" // br.ErrMsg = "图片保存失败,Err:" + err.Error() // return // } // // outFileName = randStr + ".png" // outPath = uploadDir + "/" + randStr + ".png" // // imagick.Initialize() // defer imagick.Terminate() // // args := []string{ // "convert", "-background", "none", "-size", "1200x800", svgPath, outPath, // } // convertImageCommand, err := imagick.ConvertImageCommand(args) // fmt.Println() // if err != nil { // fmt.Println("imagick.ConvertImageCommand:" + err.Error()) // br.Msg = "图片保存失败" // br.ErrMsg = "图片格式转换失败,Err:" + err.Error() // return // } // fmt.Println(convertImageCommand) // // if outPath != "" { // savePath = outPath // } // fmt.Println("upload path:" + savePath) // hzUploadDir := "static/images/" // saveToOssPath = hzUploadDir + time.Now().Format("200601/20060102/") // saveToOssPath += outFileName // //上传到阿里云 // err = services.UploadFileToAliyun("", savePath, saveToOssPath) // if err != nil { // br.Msg = "文件上传失败" // br.ErrMsg = "文件上传失败,Err:" + err.Error() // return // } // fileHost := "https://hzstatic.hzinsights.com/" // resourceUrl := fileHost + saveToOssPath // resp := new(models.ResourceResp) // resp.ResourceUrl = resourceUrl // br.Msg = "上传成功" // br.Ret = 200 // br.Success = true // br.Data = resp // return //} // @Title 图片上传 // @Description 图片上传接口 // @Param Img query string true "图片base64字符串" // @Param Sign query string true "策略组提供的资源id" // @Success 200 新增成功 // @router /svg_to_png [post] func (this *ImageController) Base64Upload() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() sign := this.GetString("Sign") checkSign := "dIGi2M0VN21s3dKS" if sign != checkSign { br.Msg = "签名错误" return } imgData := this.GetString("Img") if imgData == "" { br.Msg = "图片参数错误" br.ErrMsg = "图片参数错误,Img Is Empty" return } var err error uploadDir := "static/images/" if !utils.FileIsExist(uploadDir) { err = os.MkdirAll(uploadDir, 777) if err != nil { br.Msg = "存储目录创建失败" br.ErrMsg = "存储目录创建失败,Err:" + err.Error() return } } var saveToOssPath string randStr := utils.GetRandStringNoSpecialChar(28) var fileName, saveSvgPath, outFileName string fileName = randStr + ".svg" saveSvgPath = uploadDir + "/" + fileName err = utils.SaveToFile(imgData, saveSvgPath) if err != nil { br.Msg = "图片保存失败" br.ErrMsg = "图片保存失败,Err:" + err.Error() return } outFileName = uploadDir + randStr + ".png" fmt.Println("start convert", time.Now()) cd := exec.Command("highcharts-export-server", "--infile", saveSvgPath, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "5", "--outfile", outFileName) str, err := cd.Output() fmt.Println(string(str)) if err != nil { fmt.Println("Command Err:" + err.Error()) return } fmt.Println("end convert", time.Now()) defer func() { os.Remove(saveSvgPath) os.Remove(outFileName) }() saveToOssPath = uploadDir + time.Now().Format("200601/20060102/") saveToOssPath += outFileName //上传到阿里云 fmt.Println("upload:", outFileName, saveToOssPath) err = services.UploadFileToAliyun("", outFileName, saveToOssPath) if err != nil { br.Msg = "文件上传失败" br.ErrMsg = "文件上传失败,Err:" + err.Error() return } fileHost := "https://hzstatic.hzinsights.com/" resourceUrl := fileHost + saveToOssPath resp := new(models.ResourceResp) resp.ResourceUrl = resourceUrl br.Msg = "上传成功" br.Ret = 200 br.Success = true br.Data = resp return } // Ppt2Image // @Title ppt转图片 // @Description ppt转图片 // @Param request body models.Ppt2ImgReq true "type json string" // @Success 200 新增成功 // @router /ppt2img [post] func (this *ImageController) Ppt2Image() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() body := this.Ctx.Input.RequestBody req := new(models.Ppt2ImgReq) err := json.Unmarshal(body, &req) if err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.PptUrl == "" { br.Msg = "参数错误,ppt下载地址不能为空" return } // 下载ppt pptPath, err := utils.DownloadImage(req.PptUrl) if err != nil { br.Msg = "下载ppt失败" br.ErrMsg = "下载ppt失败,Err:" + err.Error() return } defer func() { os.Remove(pptPath) }() // ppt转pdf converterType := "pdf" pdfPath, err := ppt2img.FuncDocs2Pdf(utils.LibreOfficePath, pptPath, "./static/pdf", converterType) if err != nil { br.Msg = "ppt转pdf失败" br.ErrMsg = "ppt转pdf失败,Err:" + err.Error() return } defer func() { // 删除指定pdf目录 os.Remove(pdfPath) }() // 生成临时存放图片的目录 uploadDir := "./static/pdf/png/" + utils.GetRandStringNoSpecialChar(16) + "/" { if !utils.FileIsExist(uploadDir) { err := os.MkdirAll(uploadDir, 0777) if err != nil { br.Msg = "创建存放图片的目录失败" br.ErrMsg = "创建存放图片的目录失败,Err:" + err.Error() return } } } defer func() { // 删除指定临时目录 os.RemoveAll(uploadDir) }() pdfPathList := strings.Split(pdfPath, "/") pngName := strings.Split(path.Base(pdfPathList[len(pdfPathList)-1]), ".")[0] //图片名称 convPngName := uploadDir + pngName + ".png" // pdf 转图片 _, err = ppt2img.Pdf2Img(pdfPath, convPngName) if err != nil { br.Msg = "pdf转png失败" br.ErrMsg = "pdf转png失败,Err:" + err.Error() return } // oss的图片存放地址 resourceUrlList := make([]string, 0) // 获取生成的文件列表 fileInfoList, err := ioutil.ReadDir(uploadDir) if err != nil { br.Msg = "获取生成的文件列表失败" br.ErrMsg = "获取生成的文件列表失败,Err:" + err.Error() return } countFile := len(fileInfoList) for i := 0; i < countFile; i++ { tmpFilePath := fmt.Sprintf("%s%s-%d.png", uploadDir, pngName, i) if utils.FileIsExist(tmpFilePath) { resourceUrl, tmpErr := uploadToAliOss(tmpFilePath) if tmpErr != nil { br.Msg = "上传图片至oss失败" br.ErrMsg = "上传图片至oss失败,Err:" + err.Error() return } resourceUrlList = append(resourceUrlList, resourceUrl) } else { br.Msg = "上传图片至oss失败" br.ErrMsg = "上传图片至oss失败,Err:" + err.Error() return } } br.Ret = 200 br.Msg = "转换成功" br.Data = resourceUrlList return } // 上传文件到阿里云 func uploadToAliOss(outFileName string) (resourceUrl string, err error) { var saveToOssPath string randStr := utils.GetRandStringNoSpecialChar(28) uploadDir := "static/images/" saveToOssPath = uploadDir + time.Now().Format("200601/20060102/") saveToOssPath += randStr + ".png" //上传到阿里云 //fmt.Println("upload:", outFileName, saveToOssPath) err = services.UploadFileToAliyun("", outFileName, saveToOssPath) if err != nil { return } fileHost := "https://hzstatic.hzinsights.com/" resourceUrl = fileHost + saveToOssPath return }