|
@@ -1,6 +1,11 @@
|
|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
+ "bufio"
|
|
|
+ "github.com/pdfcpu/pdfcpu/pkg/api"
|
|
|
+ "github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
|
|
|
+ "io"
|
|
|
+
|
|
|
//"bufio"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
@@ -777,7 +782,6 @@ func (this *ArticleController) Pdfwatermark() {
|
|
|
br.Ret = 408
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
articleId, err := this.GetInt("ArticleId")
|
|
|
if articleId <= 0 {
|
|
|
br.Msg = "文章不存在"
|
|
@@ -809,72 +813,73 @@ func (this *ArticleController) Pdfwatermark() {
|
|
|
br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
|
|
|
return
|
|
|
}
|
|
|
+ var oldFile string
|
|
|
+ var newFile string
|
|
|
|
|
|
fmt.Println(fileLink)
|
|
|
fmt.Println(sliceLink[len(sliceLink)-1])
|
|
|
+ //获取PDF源文件名称
|
|
|
pdfName := sliceLink[len(sliceLink)-1]
|
|
|
- _, err = os.Stat(uploadDir + pdfName)
|
|
|
- fmt.Println(err)
|
|
|
- //pdfPath := "static/"
|
|
|
- //pdfUrl := fileLink
|
|
|
- //fileName := utils.GetRandStringNoSpecialChar(28) + ".pdf"
|
|
|
- //res, err := nhttp.Get(pdfUrl)
|
|
|
- //if err != nil {
|
|
|
- // fmt.Println("A error occurred!")
|
|
|
- // return
|
|
|
- //}
|
|
|
- //defer res.Body.Close()
|
|
|
- //// 获得get请求响应的reader对象
|
|
|
- //reader := bufio.NewReaderSize(res.Body, 32*1024)
|
|
|
- //file, err := os.Create(pdfPath + fileName)
|
|
|
- //if err != nil {
|
|
|
- // panic(err)
|
|
|
- //}
|
|
|
- //获得文件的writer对象
|
|
|
- //writer := bufio.NewWriter(file)
|
|
|
- //written, _ := io.Copy(writer, reader)
|
|
|
- //fmt.Printf("Total length: %d", written)
|
|
|
- //str, _ := os.Getwd()
|
|
|
- //fileLink = str + "/" + pdfPath + fileName
|
|
|
- //oldFile := pdfPath + fileName
|
|
|
- //newFile := pdfPath + "new" + fileName
|
|
|
- //onTop := true
|
|
|
- //wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, "sc:4 abs, d:1, op:.4, pos:c ,points:16", onTop, 1)
|
|
|
- //if err != nil {
|
|
|
- // br.Msg = "下载失败"
|
|
|
- // br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
|
|
|
- // return
|
|
|
- //}
|
|
|
- //err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
|
|
|
- //if err != nil {
|
|
|
- // br.Msg = "下载失败"
|
|
|
- // br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
|
|
|
- // return
|
|
|
- //}
|
|
|
- //resp := new(models.ArticleDetailFileLink)
|
|
|
- //randStr := utils.GetRandStringNoSpecialChar(28)
|
|
|
- //fileName = randStr + ".pdf"
|
|
|
+ oldFile = uploadDir + pdfName
|
|
|
+ //判断PDF本地是否存在,不存在则保存到本地
|
|
|
+ _, err = os.Stat(oldFile)
|
|
|
+ if err != nil {
|
|
|
+ res, err := nhttp.Get(fileLink)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("A error occurred!")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer res.Body.Close()
|
|
|
+ // 获得get请求响应的reader对象
|
|
|
+ reader := bufio.NewReaderSize(res.Body, 32*1024)
|
|
|
+ file, err := os.Create(oldFile)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ //获得文件的writer对象
|
|
|
+ writer := bufio.NewWriter(file)
|
|
|
+ written, _ := io.Copy(writer, reader)
|
|
|
+ fmt.Printf("Total length: %d", written)
|
|
|
+ }
|
|
|
+ newFile = uploadDir + "new_" + pdfName
|
|
|
+ onTop := true
|
|
|
+ wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, "sc:4 abs, d:1, op:.4, pos:c ,points:16 ", onTop, 1)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "下载失败"
|
|
|
+ br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "下载失败"
|
|
|
+ br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp := new(models.ArticleDetailFileLink)
|
|
|
+ randStr := utils.GetRandStringNoSpecialChar(28)
|
|
|
+ fileName := randStr + ".pdf"
|
|
|
//hzUploadDir := "static/pdf/"
|
|
|
- //savePath := hzUploadDir + time.Now().Format("200601/20060102/")
|
|
|
- //savePath += fileName
|
|
|
- ////上传到阿里云
|
|
|
- //err = services.UploadFileToAliyun(fileName, newFile, savePath)
|
|
|
- //if err != nil {
|
|
|
- // br.Msg = "下载失败"
|
|
|
- // br.ErrMsg = "文件上传失败,Err:" + err.Error()
|
|
|
- // return
|
|
|
- //}
|
|
|
- //fileHost := "https://hzstatic.hzinsights.com/"
|
|
|
- //resourceUrl := fileHost + savePath
|
|
|
- //defer func() {
|
|
|
- // os.Remove(newFile)
|
|
|
- // os.Remove(fileLink)
|
|
|
- //}()
|
|
|
- //resp.FileLink = resourceUrl
|
|
|
+ savePath := uploadDir + time.Now().Format("200601/20060102/")
|
|
|
+ fmt.Println(savePath)
|
|
|
+ savePath += fileName
|
|
|
+ //上传到阿里云
|
|
|
+ err = services.UploadFileToAliyun(fileName, newFile, savePath)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "下载失败"
|
|
|
+ br.ErrMsg = "文件上传失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fileHost := "https://hzstatic.hzinsights.com/"
|
|
|
+ resourceUrl := fileHost + savePath
|
|
|
+ defer func() {
|
|
|
+ //os.Remove(newFile)
|
|
|
+ //os.Remove(fileLink)
|
|
|
+ }()
|
|
|
+ resp.FileLink = resourceUrl
|
|
|
br.Ret = 200
|
|
|
br.Success = true
|
|
|
br.Msg = "获取成功"
|
|
|
- //br.Data = resp
|
|
|
+ br.Data = resp
|
|
|
}
|
|
|
|
|
|
// @Title 同步策略报告内容
|