You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package PreviewController
import (
"dsSupport/Const"
"dsSupport/Utils/CommonUtil"
"dsSupport/Utils/FileUtil"
"dsSupport/Utils/ObsUtil"
"encoding/base64"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
//模块的路由配置
func Routers(r *gin.RouterGroup) {
rr := r.Group("/preview")
//配置接口
rr.GET("/previewOffice", previewOffice)
return
}
// http://127.0.0.1:9000/support/preview/previewOffice
/**
功能提供Office系列文件的预览功能
作者:黄海
时间2020-07-15
*/
func previewOffice(c *gin.Context) {
// 允许预览的扩展名
allowExtension := []string{`doc`, `docx`, `rtf`, `xls`, `xlsx`, `ppt`, `pptx`,
`pdf`, `zip`, `rar`, `7z`, `png`, `jpg`, `jpeg`, `wps`, `txt`, `js`, `json`, `css`, `sql`, `xml`, `java`, `cs`}
//本地物理路径
uEnc := c.Query("p")
uDec, err := base64.URLEncoding.DecodeString(uEnc)
if err != nil {
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://") {
//文件名称
arr := strings.Split(fullPath, "/")
fi := arr[len(arr)-1]
if !CommonUtil.IsContainString(allowExtension,"."+path.Ext(fi)){
c.String(http.StatusOK, "输入的预览文件,系统不支持!")
return
}
//下载
res, err := http.Get(fullPath)
if err != nil {
c.String(http.StatusOK, "输入的网络地址无法找到,不能进行预览!")
return
}
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)
//判断是不是本地存在
isExist, err := CommonUtil.PathExists(fullPath)
if err != nil {
c.String(http.StatusOK, "发生严重错误:"+err.Error())
return
}
if !isExist {
c.String(http.StatusOK, "输入的文件无法找到!")
return
}
//将文件上传到华为云
key := Const.PreviewPrefix + p
ObsUtil.UploadFileMultiPart(key, fullPath)
//返回地址
url := "http://ow365.cn/?i=14531&fname=" + p + "&furl=http%3A%2F%2Fvideo.edusoa.com%2F" + key
//输出预览地址
c.String(http.StatusOK, url)
}