Prechádzať zdrojové kódy

PDf 打水印上传到oss

xingzai 3 rokov pred
rodič
commit
440e1694c4
3 zmenil súbory, kde vykonal 60 pridanie a 16 odobranie
  1. 27 3
      controllers/article.go
  2. 32 13
      services/oss.go
  3. 1 0
      utils/constants.go

+ 27 - 3
controllers/article.go

@@ -7,6 +7,11 @@ import (
 	"hongze/hongze_cygx/services"
 	"hongze/hongze_cygx/utils"
 	"html"
+	"os"
+
+	//"os"
+
+	//"os"
 	"rdluck_tools/http"
 	"regexp"
 	"strconv"
@@ -764,8 +769,26 @@ func (this *ArticleController) Pdfwatermark() {
 		br.ErrMsg = "Unmarshal,Err:" + err.Error()
 		return
 	}
+	filePathName := strings.TrimLeft(pdfResult.FilePath, ".")
+	filePathOldName := strings.TrimLeft(pdfResult.FileOldPath, ".")
+	filePath := "/home/code/python/hz_cygx_pdf/pdf" + filePathName
+	filePathOld := "/home/code/python/hz_cygx_pdf/pdf" + filePathOldName
+	randStr := utils.GetRandStringNoSpecialChar(28)
+	fileName := randStr + ".pdf"
+	//上传到阿里云
+	resourceUrl, err := services.UploadPdfAliyun(fileName, filePath)
+	if err != nil {
+		br.Msg = "文件上传失败"
+		br.ErrMsg = "文件上传失败,Err:" + err.Error()
+		return
+	}
+	defer func() {
+		os.Remove(filePath)
+		os.Remove(filePathOld)
+	}()
 	resp := new(models.ArticleDetailFileLink)
-	resp.FileLink = "/Library/WebServer/python/hongze_pptx" + strings.TrimLeft(pdfResult.FilePath, ".")
+	resp.FileLink = resourceUrl
+	//resp.FileLink = pdfResult.FilePath
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"
@@ -773,6 +796,7 @@ func (this *ArticleController) Pdfwatermark() {
 }
 
 type PublishPdfResult struct {
-	Result   string `json:"result"`
-	FilePath string `json:"file_path"`
+	Result      string `json:"result"`
+	FilePath    string `json:"file_path"`
+	FileOldPath string `json:"file_old_path"`
 }

+ 32 - 13
services/oss.go

@@ -27,48 +27,47 @@ func init() {
 */
 
 //图片上传到阿里云
-func UploadAliyun(filename, filepath string) (string,error) {
+func UploadAliyun(filename, filepath string) (string, error) {
 	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
 	if err != nil {
-		return "1",err
+		return "1", err
 	}
 	bucket, err := client.Bucket(utils.Bucketname)
 	if err != nil {
-		return "2",err
+		return "2", err
 	}
 	path := utils.Upload_dir + time.Now().Format("200601/20060102/")
 	path += filename
 	err = bucket.PutObjectFromFile(path, filepath)
 	if err != nil {
-		return "3",err
+		return "3", err
 	}
 	path = utils.Imghost + path
-	return path,err
+	return path, err
 }
 
-
 //音频上传到阿里云
-func UploadAudioAliyun(filename, filepath string) (string,error) {
+func UploadAudioAliyun(filename, filepath string) (string, error) {
 	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
 	if err != nil {
-		return "1",err
+		return "1", err
 	}
 	bucket, err := client.Bucket(utils.Bucketname)
 	if err != nil {
-		return "2",err
+		return "2", err
 	}
 	path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
 	path += filename
 	err = bucket.PutObjectFromFile(path, filepath)
 	if err != nil {
-		return "3",err
+		return "3", err
 	}
 	path = utils.Imghost + path
-	return path,err
+	return path, err
 }
 
 //视频上传到阿里云
-func UploadVideoAliyun(filename, filepath,savePath string) (error) {
+func UploadVideoAliyun(filename, filepath, savePath string) error {
 	defer func() {
 		os.Remove(filepath)
 	}()
@@ -89,4 +88,24 @@ func UploadVideoAliyun(filename, filepath,savePath string) (error) {
 	//path = utils.Imghost + path
 	//return path,err
 	return err
-}
+}
+
+//PDF上传到阿里云
+func UploadPdfAliyun(filename, filepath string) (string, error) {
+	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
+	if err != nil {
+		return "1", err
+	}
+	bucket, err := client.Bucket(utils.Bucketname)
+	if err != nil {
+		return "2", err
+	}
+	path := utils.Upload_Pdf_Dir + time.Now().Format("200601/20060102/")
+	path += filename
+	err = bucket.PutObjectFromFile(path, filepath)
+	if err != nil {
+		return "3", err
+	}
+	path = utils.Imghost + path
+	return path, err
+}

+ 1 - 0
utils/constants.go

@@ -49,6 +49,7 @@ var (
 	Imghost          string = "https://hongze.oss-cn-shanghai.aliyuncs.com/"
 	Upload_dir       string = "static/images/"
 	Upload_Audio_Dir string = "static/audio/"
+	Upload_Pdf_Dir   string = "static/pdf/"
 
 	AccessKeyId     string = "LTAIFMZYQhS2BTvW"
 	AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"