file.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package services
  2. import (
  3. "bytes"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "github.com/pdfcpu/pdfcpu/pkg/api"
  9. "github.com/pdfcpu/pdfcpu/pkg/font"
  10. "github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
  11. "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
  12. "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
  13. "io"
  14. "mime/multipart"
  15. "os"
  16. "os/exec"
  17. "strings"
  18. "time"
  19. )
  20. // UploadToOssAndFileName 上传到oss并使用这个名称
  21. func UploadToOssAndFileName(fileMulti multipart.File, newFileName string) (resourceUrl string, err error) {
  22. dateDir := time.Now().Format("20060102")
  23. uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
  24. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  25. if err != nil {
  26. err = errors.New("存储目录创建失败,Err:" + err.Error())
  27. return
  28. }
  29. //本地地址
  30. fpath := uploadDir + "/" + newFileName
  31. err = saveToFile(fileMulti, fpath)
  32. if err != nil {
  33. err = errors.New("文件上传失败,Err:" + err.Error())
  34. return
  35. }
  36. //上传到阿里云 和 minio
  37. //if utils.ObjectStorageClient == "minio" {
  38. // resourceUrl, err = UploadImgToMinIo(newFileName, fpath)
  39. // if err != nil {
  40. // err = errors.New("文件上传失败,Err:" + err.Error())
  41. // return
  42. // }
  43. //} else {
  44. // resourceUrl, err = UploadAliyunV2(newFileName, fpath)
  45. // if err != nil {
  46. // err = errors.New("文件上传失败,Err:" + err.Error())
  47. // return
  48. // }
  49. //}
  50. ossClient := NewOssClient()
  51. if ossClient == nil {
  52. err = fmt.Errorf("初始化OSS服务失败")
  53. return
  54. }
  55. resourceUrl, err = ossClient.UploadFile(newFileName, fpath, "")
  56. if err != nil {
  57. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  58. return
  59. }
  60. defer func() {
  61. os.Remove(fpath)
  62. }()
  63. item := new(models.Resource)
  64. item.ResourceUrl = resourceUrl
  65. item.ResourceType = 1
  66. item.CreateTime = time.Now()
  67. _, err = models.AddResource(item)
  68. if err != nil {
  69. err = errors.New("资源上传失败,Err:" + err.Error())
  70. return
  71. }
  72. return
  73. }
  74. // saveToFile 保存到本地文件
  75. func saveToFile(fileMulti multipart.File, tofile string) error {
  76. f, err := os.OpenFile(tofile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  77. if err != nil {
  78. return err
  79. }
  80. defer f.Close()
  81. io.Copy(f, fileMulti)
  82. return nil
  83. }
  84. // GetResourceUrlBySvgImg
  85. // @Description: 通过svg图片生成图片资源地址(传到OSS后的地址)
  86. // @author: Roc
  87. // @datetime 2024-07-16 10:18:09
  88. // @param imgData string
  89. // @return resourceUrl string
  90. // @return err error
  91. // @return errMsg string
  92. func GetResourceUrlBySvgImg(imgData string) (resourceUrl string, err error, errMsg string) {
  93. errMsg = "图表保存失败"
  94. uploadDir := "static/images/"
  95. if !utils.FileIsExist(uploadDir) {
  96. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  97. if err != nil {
  98. err = errors.New("存储目录创建失败,Err:" + err.Error())
  99. return
  100. }
  101. }
  102. //var saveToOssPath string
  103. randStr := utils.GetRandStringNoSpecialChar(28)
  104. var fileName, outFileName string
  105. fileName = randStr + ".txt"
  106. fileName = uploadDir + fileName
  107. err = utils.SaveToFile(imgData, fileName)
  108. if err != nil {
  109. err = errors.New("图片保存失败,Err:" + err.Error())
  110. return
  111. }
  112. // 删除临时存储的svg文件
  113. defer func() {
  114. err = os.Remove(fileName)
  115. if err != nil {
  116. utils.FileLog.Info("删除临时存储的svg文件失败, err: " + err.Error())
  117. }
  118. }()
  119. outFileName = randStr + ".png"
  120. doneChannel := make(chan bool, 1)
  121. errorChannel := make(chan error, 1)
  122. cmd := exec.Command("highcharts-export-server", "--infile", fileName, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "3", "--outfile", outFileName)
  123. go func() {
  124. output, err := cmd.CombinedOutput()
  125. if err != nil {
  126. utils.FileLog.Info("execute command failed, output: , error: \n" + string(output) + err.Error())
  127. errorChannel <- err
  128. return
  129. }
  130. doneChannel <- true
  131. }()
  132. defer func() {
  133. _ = os.Remove(outFileName)
  134. if err != nil {
  135. utils.FileLog.Info("删除生产的图片文件失败, err: " + err.Error())
  136. }
  137. }()
  138. select {
  139. case <-time.After(30 * time.Second):
  140. utils.FileLog.Info("执行超过30秒 杀死超时进程")
  141. e := cmd.Process.Kill()
  142. if e != nil {
  143. fmt.Println("cmd kill err: ", e.Error())
  144. utils.FileLog.Info(fmt.Sprintf("cmd kill err: %s", e.Error()))
  145. errMsg = "图片生成失败"
  146. err = errors.New("图片生成失败, 执行超时" + e.Error())
  147. return
  148. }
  149. fmt.Println("timeout kill process")
  150. case <-doneChannel:
  151. fmt.Println("done")
  152. case e := <-errorChannel:
  153. errMsg = "文件上传失败"
  154. err = errors.New(fmt.Sprintf("execute command failure err: %s", e.Error()))
  155. fmt.Println("execute command failure err:" + e.Error())
  156. return
  157. }
  158. //上传到阿里云 和 minio
  159. ossClient := NewOssClient()
  160. if ossClient == nil {
  161. errMsg = "上传失败"
  162. err = errors.New("初始化OSS服务失败")
  163. return
  164. }
  165. resourceUrl, err = ossClient.UploadFile(outFileName, outFileName, "")
  166. if err != nil {
  167. errMsg = "文件上传失败"
  168. err = errors.New("文件上传失败,Err:" + err.Error())
  169. return
  170. }
  171. return
  172. }
  173. func GeneralWaterMarkPdf(pdfPath, waterMarkStr string) (f2 *bytes.Buffer, err error) {
  174. onTop := true
  175. update := false
  176. //fontDir := `C:\Users\123\go\src\eta\eta_api\static`
  177. fontDir := `static`
  178. font.UserFontDir = fontDir
  179. ttfList := fontDir + `/SimHei.ttf`
  180. err = api.InstallFonts([]string{ttfList})
  181. if err != nil {
  182. fmt.Println("InstallFonts err:", err)
  183. }
  184. api.DisableConfigDir()
  185. f1, err := os.Open(pdfPath)
  186. if err != nil {
  187. fmt.Println("文件不存在")
  188. return
  189. }
  190. defer func() {
  191. _ = f1.Close()
  192. }()
  193. f2 = &bytes.Buffer{}
  194. //strList := []string{waterMarkStr, waterMarkStr, waterMarkStr, waterMarkStr, waterMarkStr, waterMarkStr}
  195. strList := []string{waterMarkStr, waterMarkStr, waterMarkStr, waterMarkStr}
  196. //strList := []string{waterMarkStr}
  197. newWaterMarkStr := strings.Join(strList, " ")
  198. wmList := make([]*model.Watermark, 0)
  199. for i := types.TopLeft; i <= 9; i++ {
  200. wm, tmpErr := api.TextWatermark(newWaterMarkStr, "fo:SimHei, points:48, col: 0.75 0.75 0.75, rot:45, scale:1 abs, opacity:0.3", onTop, update, types.POINTS)
  201. if tmpErr != nil {
  202. fmt.Println("TextWatermark err:", tmpErr)
  203. return
  204. }
  205. //wm.Pos = types.Center
  206. wm.Pos = i
  207. wmList = append(wmList, wm)
  208. }
  209. err = AddWatermarks(f1, f2, nil, wmList, nil)
  210. return
  211. }
  212. func AddWatermarks(rs io.ReadSeeker, w io.Writer, selectedPages []string, wmList []*model.Watermark, conf *model.Configuration) error {
  213. if rs == nil {
  214. return errors.New("pdfcpu: AddWatermarks: missing rs")
  215. }
  216. if conf == nil {
  217. conf = model.NewDefaultConfiguration()
  218. }
  219. conf.Cmd = model.ADDWATERMARKS
  220. conf.OptimizeDuplicateContentStreams = false
  221. if len(wmList) <= 0 {
  222. return errors.New("pdfcpu: missing watermark configuration")
  223. }
  224. ctx, err := api.ReadValidateAndOptimize(rs, conf)
  225. if err != nil {
  226. return err
  227. }
  228. var pages types.IntSet
  229. pages, err = api.PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
  230. if err != nil {
  231. return err
  232. }
  233. for _, wm := range wmList {
  234. if err = pdfcpu.AddWatermarks(ctx, pages, wm); err != nil {
  235. return err
  236. }
  237. }
  238. return api.Write(ctx, w, conf)
  239. }