|
|
|
@ -3,12 +3,15 @@ package PreviewController
|
|
|
|
|
import (
|
|
|
|
|
"dsSupport/Const"
|
|
|
|
|
"dsSupport/Utils/CommonUtil"
|
|
|
|
|
"dsSupport/Utils/FileUtil"
|
|
|
|
|
"dsSupport/Utils/ObsUtil"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"log"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//模块的路由配置
|
|
|
|
@ -30,11 +33,32 @@ func previewOffice(c *gin.Context) {
|
|
|
|
|
uEnc := c.Query("p")
|
|
|
|
|
uDec, err := base64.URLEncoding.DecodeString(uEnc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
c.String(http.StatusOK, "输入的参数p转为base64Url时失败,请确认是否未经Base64.URLEdcoding进行加码!")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fullPath := string(uDec)
|
|
|
|
|
//(1) 是否以http或https开头
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(strings.ToLower(fullPath), "https://") || strings.HasPrefix(strings.ToLower(fullPath), "http://") {
|
|
|
|
|
//下载
|
|
|
|
|
res, err := http.Get(fullPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.String(http.StatusOK, "输入的网络地址无法找到,不能进行预览!")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//文件名称
|
|
|
|
|
arr := strings.Split(fullPath, "/")
|
|
|
|
|
fi := arr[len(arr)-1]
|
|
|
|
|
fullPath = CommonUtil.GetCurrentPath() + "/TempFiles/" + fi
|
|
|
|
|
if FileUtil.Exists(fullPath) {
|
|
|
|
|
os.Remove(fullPath)
|
|
|
|
|
}
|
|
|
|
|
f, err := os.Create(fullPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.String(http.StatusOK, "创建文件时发生读写错误:"+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
io.Copy(f, res.Body)
|
|
|
|
|
}
|
|
|
|
|
//(2) 是不是本地文件
|
|
|
|
|
_, p := filepath.Split(fullPath)
|
|
|
|
|
|
|
|
|
|