image.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_pub/models"
  5. "eta/eta_pub/services"
  6. "eta/eta_pub/services/ppt2img"
  7. "eta/eta_pub/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "strings"
  14. "time"
  15. )
  16. type ImageController struct {
  17. BaseCommonController
  18. }
  19. //
  20. //// @Title 图片上传
  21. //// @Description 图片上传接口
  22. //// @Param Img query string true "图片base64字符串"
  23. //// @Param Sign query string true "策略组提供的资源id"
  24. //// @Success 200 新增成功
  25. //// @router /svg_to_png [post]
  26. //func (this *ImageController) Base64Upload() {
  27. // br := new(models.BaseResponse).Init()
  28. // defer func() {
  29. // this.Data["json"] = br
  30. // this.ServeJSON()
  31. // }()
  32. // sign := this.GetString("Sign")
  33. // checkSign := "dIGi2M0VN21s3dKS"
  34. // if sign != checkSign {
  35. // br.Msg = "前面错误"
  36. // return
  37. // }
  38. // imgData := this.GetString("Img")
  39. // if imgData == "" {
  40. // br.Msg = "图片参数错误"
  41. // br.ErrMsg = "图片参数错误,Img Is Empty"
  42. // return
  43. // }
  44. // var err error
  45. // dateDir := time.Now().Format("20060102")
  46. // uploadDir := "static/imgs/" + dateDir
  47. //
  48. // if !utils.FileIsExist(uploadDir) {
  49. // err = os.MkdirAll(uploadDir, 777)
  50. // if err != nil {
  51. // br.Msg = "存储目录创建失败"
  52. // br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  53. // return
  54. // }
  55. // }
  56. // var saveToOssPath string
  57. // randStr := utils.GetRandStringNoSpecialChar(28)
  58. // var fileName, savePath, svgPath, outPath, outFileName string
  59. // imgData = strings.Replace(imgData, "data:image/svg+xml;base64,", "", -1)
  60. // fileName = randStr + ".svg"
  61. // savePath = uploadDir + "/" + fileName
  62. // svgPath = savePath
  63. // err = utils.SaveBase64ToFile(imgData, savePath)
  64. // if err != nil {
  65. // br.Msg = "图片保存失败"
  66. // br.ErrMsg = "图片保存失败,Err:" + err.Error()
  67. // return
  68. // }
  69. //
  70. // outFileName = randStr + ".png"
  71. // outPath = uploadDir + "/" + randStr + ".png"
  72. //
  73. // imagick.Initialize()
  74. // defer imagick.Terminate()
  75. //
  76. // args := []string{
  77. // "convert", "-background", "none", "-size", "1200x800", svgPath, outPath,
  78. // }
  79. // convertImageCommand, err := imagick.ConvertImageCommand(args)
  80. // fmt.Println()
  81. // if err != nil {
  82. // fmt.Println("imagick.ConvertImageCommand:" + err.Error())
  83. // br.Msg = "图片保存失败"
  84. // br.ErrMsg = "图片格式转换失败,Err:" + err.Error()
  85. // return
  86. // }
  87. // fmt.Println(convertImageCommand)
  88. //
  89. // if outPath != "" {
  90. // savePath = outPath
  91. // }
  92. // fmt.Println("upload path:" + savePath)
  93. // hzUploadDir := "static/images/"
  94. // saveToOssPath = hzUploadDir + time.Now().Format("200601/20060102/")
  95. // saveToOssPath += outFileName
  96. // //上传到阿里云
  97. // err = services.UploadFileToAliyun("", savePath, saveToOssPath)
  98. // if err != nil {
  99. // br.Msg = "文件上传失败"
  100. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  101. // return
  102. // }
  103. // fileHost := "https://hzstatic.hzinsights.com/"
  104. // resourceUrl := fileHost + saveToOssPath
  105. // resp := new(models.ResourceResp)
  106. // resp.ResourceUrl = resourceUrl
  107. // br.Msg = "上传成功"
  108. // br.Ret = 200
  109. // br.Success = true
  110. // br.Data = resp
  111. // return
  112. //}
  113. // @Title 图片上传
  114. // @Description 图片上传接口
  115. // @Param Img query string true "图片base64字符串"
  116. // @Param Sign query string true "策略组提供的资源id"
  117. // @Success 200 新增成功
  118. // @router /svg_to_png [post]
  119. func (this *ImageController) Base64Upload() {
  120. br := new(models.BaseResponse).Init()
  121. defer func() {
  122. this.Data["json"] = br
  123. this.ServeJSON()
  124. }()
  125. sign := this.GetString("Sign")
  126. checkSign := "dIGi2M0VN21s3dKS"
  127. if sign != checkSign {
  128. br.Msg = "签名错误"
  129. return
  130. }
  131. imgData := this.GetString("Img")
  132. if imgData == "" {
  133. br.Msg = "图片参数错误"
  134. br.ErrMsg = "图片参数错误,Img Is Empty"
  135. return
  136. }
  137. var err error
  138. uploadDir := "static/images/"
  139. if !utils.FileIsExist(uploadDir) {
  140. err = os.MkdirAll(uploadDir, 777)
  141. if err != nil {
  142. br.Msg = "存储目录创建失败"
  143. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  144. return
  145. }
  146. }
  147. var saveToOssPath string
  148. randStr := utils.GetRandStringNoSpecialChar(28)
  149. var fileName, saveSvgPath, outFileName string
  150. fileName = randStr + ".svg"
  151. saveSvgPath = uploadDir + "/" + fileName
  152. err = utils.SaveToFile(imgData, saveSvgPath)
  153. if err != nil {
  154. br.Msg = "图片保存失败"
  155. br.ErrMsg = "图片保存失败,Err:" + err.Error()
  156. return
  157. }
  158. outFileName = uploadDir + randStr + ".png"
  159. fmt.Println("start convert", time.Now())
  160. cd := exec.Command("highcharts-export-server", "--infile", saveSvgPath, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "5", "--outfile", outFileName)
  161. str, err := cd.Output()
  162. fmt.Println(string(str))
  163. if err != nil {
  164. fmt.Println("Command Err:" + err.Error())
  165. return
  166. }
  167. fmt.Println("end convert", time.Now())
  168. defer func() {
  169. os.Remove(saveSvgPath)
  170. os.Remove(outFileName)
  171. }()
  172. saveToOssPath = uploadDir + time.Now().Format("200601/20060102/")
  173. saveToOssPath += outFileName
  174. //上传到阿里云
  175. fmt.Println("upload:", outFileName, saveToOssPath)
  176. err = services.UploadFileToAliyun("", outFileName, saveToOssPath)
  177. if err != nil {
  178. br.Msg = "文件上传失败"
  179. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  180. return
  181. }
  182. fileHost := "https://hzstatic.hzinsights.com/"
  183. resourceUrl := fileHost + saveToOssPath
  184. resp := new(models.ResourceResp)
  185. resp.ResourceUrl = resourceUrl
  186. br.Msg = "上传成功"
  187. br.Ret = 200
  188. br.Success = true
  189. br.Data = resp
  190. return
  191. }
  192. // Ppt2Image
  193. // @Title ppt转图片
  194. // @Description ppt转图片
  195. // @Param request body models.Ppt2ImgReq true "type json string"
  196. // @Success 200 新增成功
  197. // @router /ppt2img [post]
  198. func (this *ImageController) Ppt2Image() {
  199. br := new(models.BaseResponse).Init()
  200. defer func() {
  201. this.Data["json"] = br
  202. this.ServeJSON()
  203. }()
  204. body := this.Ctx.Input.RequestBody
  205. req := new(models.Ppt2ImgReq)
  206. err := json.Unmarshal(body, &req)
  207. if err != nil {
  208. br.Msg = "参数解析失败"
  209. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  210. return
  211. }
  212. if req.PptUrl == "" {
  213. br.Msg = "参数错误,ppt下载地址不能为空"
  214. return
  215. }
  216. // 下载ppt
  217. pptPath, err := utils.DownloadImage(req.PptUrl)
  218. if err != nil {
  219. br.Msg = "下载ppt失败"
  220. br.ErrMsg = "下载ppt失败,Err:" + err.Error()
  221. return
  222. }
  223. defer func() {
  224. os.Remove(pptPath)
  225. }()
  226. // ppt转pdf
  227. converterType := "pdf"
  228. pdfPath, err := ppt2img.FuncDocs2Pdf(utils.LibreOfficePath, pptPath, "./static/pdf", converterType)
  229. if err != nil {
  230. br.Msg = "ppt转pdf失败"
  231. br.ErrMsg = "ppt转pdf失败,Err:" + err.Error()
  232. return
  233. }
  234. defer func() {
  235. // 删除指定pdf目录
  236. os.Remove(pdfPath)
  237. }()
  238. // 生成临时存放图片的目录
  239. uploadDir := "./static/pdf/png/" + utils.GetRandStringNoSpecialChar(16) + "/"
  240. {
  241. if !utils.FileIsExist(uploadDir) {
  242. err := os.MkdirAll(uploadDir, 0777)
  243. if err != nil {
  244. br.Msg = "创建存放图片的目录失败"
  245. br.ErrMsg = "创建存放图片的目录失败,Err:" + err.Error()
  246. return
  247. }
  248. }
  249. }
  250. defer func() {
  251. // 删除指定临时目录
  252. os.RemoveAll(uploadDir)
  253. }()
  254. pdfPathList := strings.Split(pdfPath, "/")
  255. pngName := strings.Split(path.Base(pdfPathList[len(pdfPathList)-1]), ".")[0] //图片名称
  256. convPngName := uploadDir + pngName + ".png"
  257. // pdf 转图片
  258. _, err = ppt2img.Pdf2Img(pdfPath, convPngName)
  259. if err != nil {
  260. br.Msg = "pdf转png失败"
  261. br.ErrMsg = "pdf转png失败,Err:" + err.Error()
  262. return
  263. }
  264. // oss的图片存放地址
  265. resourceUrlList := make([]string, 0)
  266. // 获取生成的文件列表
  267. fileInfoList, err := ioutil.ReadDir(uploadDir)
  268. if err != nil {
  269. br.Msg = "获取生成的文件列表失败"
  270. br.ErrMsg = "获取生成的文件列表失败,Err:" + err.Error()
  271. return
  272. }
  273. countFile := len(fileInfoList)
  274. for i := 0; i < countFile; i++ {
  275. tmpFilePath := fmt.Sprintf("%s%s-%d.png", uploadDir, pngName, i)
  276. if utils.FileIsExist(tmpFilePath) {
  277. resourceUrl, tmpErr := uploadToAliOss(tmpFilePath)
  278. if tmpErr != nil {
  279. br.Msg = "上传图片至oss失败"
  280. br.ErrMsg = "上传图片至oss失败,Err:" + err.Error()
  281. return
  282. }
  283. resourceUrlList = append(resourceUrlList, resourceUrl)
  284. } else {
  285. br.Msg = "上传图片至oss失败"
  286. br.ErrMsg = "上传图片至oss失败,Err:" + err.Error()
  287. return
  288. }
  289. }
  290. br.Ret = 200
  291. br.Msg = "转换成功"
  292. br.Data = resourceUrlList
  293. return
  294. }
  295. // 上传文件到阿里云
  296. func uploadToAliOss(outFileName string) (resourceUrl string, err error) {
  297. var saveToOssPath string
  298. randStr := utils.GetRandStringNoSpecialChar(28)
  299. uploadDir := "static/images/"
  300. saveToOssPath = uploadDir + time.Now().Format("200601/20060102/")
  301. saveToOssPath += randStr + ".png"
  302. //上传到阿里云
  303. //fmt.Println("upload:", outFileName, saveToOssPath)
  304. err = services.UploadFileToAliyun("", outFileName, saveToOssPath)
  305. if err != nil {
  306. return
  307. }
  308. fileHost := "https://hzstatic.hzinsights.com/"
  309. resourceUrl = fileHost + saveToOssPath
  310. return
  311. }