Browse Source

将PDF源文件保存到本地

xingzai 3 năm trước cách đây
mục cha
commit
b3323415de
1 tập tin đã thay đổi với 7 bổ sung9 xóa
  1. 7 9
      controllers/article.go

+ 7 - 9
controllers/article.go

@@ -809,15 +809,12 @@ func (this *ArticleController) Pdfwatermark() {
 	uploadDir := "static/pdf/"
 	err = os.MkdirAll(uploadDir, 777)
 	if err != nil {
-		br.Msg = "存储目录创建失败"
+		br.Msg = "下载失败"
 		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]
 	oldFile = uploadDir + pdfName
@@ -826,7 +823,8 @@ func (this *ArticleController) Pdfwatermark() {
 	if err != nil {
 		res, err := nhttp.Get(fileLink)
 		if err != nil {
-			fmt.Println("A error occurred!")
+			br.Msg = "下载失败"
+			br.ErrMsg = "获取源文件失败,Err:" + err.Error()
 			return
 		}
 		defer res.Body.Close()
@@ -834,7 +832,9 @@ func (this *ArticleController) Pdfwatermark() {
 		reader := bufio.NewReaderSize(res.Body, 32*1024)
 		file, err := os.Create(oldFile)
 		if err != nil {
-			panic(err)
+			br.Msg = "下载失败"
+			br.ErrMsg = "保存源文件到本地失败,Err:" + err.Error()
+			return
 		}
 		//获得文件的writer对象
 		writer := bufio.NewWriter(file)
@@ -858,9 +858,7 @@ func (this *ArticleController) Pdfwatermark() {
 	resp := new(models.ArticleDetailFileLink)
 	randStr := utils.GetRandStringNoSpecialChar(28)
 	fileName := randStr + ".pdf"
-	//hzUploadDir := "static/pdf/"
 	savePath := uploadDir + time.Now().Format("200601/20060102/")
-	fmt.Println(savePath)
 	savePath += fileName
 	//上传到阿里云
 	err = services.UploadFileToAliyun(fileName, newFile, savePath)
@@ -872,7 +870,7 @@ func (this *ArticleController) Pdfwatermark() {
 	fileHost := "https://hzstatic.hzinsights.com/"
 	resourceUrl := fileHost + savePath
 	defer func() {
-		//os.Remove(newFile)
+		os.Remove(newFile)
 		//os.Remove(fileLink)
 	}()
 	resp.FileLink = resourceUrl