|
|
package Preview
|
|
|
|
|
|
import (
|
|
|
"dsSupport/Const"
|
|
|
"dsSupport/Utils/CommonUtil"
|
|
|
"dsSupport/Utils/FileUtil"
|
|
|
"dsSupport/Utils/ObsUtil"
|
|
|
"encoding/base64"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"io"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
//模块的路由配置
|
|
|
func Routers(r *gin.RouterGroup) {
|
|
|
rr := r.Group("/preview")
|
|
|
//配置接口
|
|
|
rr.GET("/previewOffice", previewOffice)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// http://127.0.0.1:9000/dsSupport/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)
|
|
|
//判断是不是华为云上存在,原则上存在就不需要再次上传了~
|
|
|
tempArray:=strings.Split(strings.Replace(strings.ToLower(fullPath),"\\","/",-1),"/")
|
|
|
fiName:=tempArray[len(tempArray)-1]
|
|
|
key := Const.PreviewPrefix + fiName
|
|
|
url := "http://ow365.cn/?i=14531&fname=" + fiName + "&furl=http%3A%2F%2Fvideo.edusoa.com%2F" + key
|
|
|
//如果存在,则直接返回
|
|
|
if ObsUtil.IsExist(fiName){
|
|
|
//输出预览地址
|
|
|
c.String(http.StatusOK, url)
|
|
|
//fmt.Println("文件已在云存储中存在,无需下载和上传!")
|
|
|
return
|
|
|
}
|
|
|
//(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) 是不是本地文件
|
|
|
//判断是不是本地存在
|
|
|
isExist, err := CommonUtil.PathExists(fullPath)
|
|
|
if err != nil {
|
|
|
c.String(http.StatusOK, "发生严重错误:"+err.Error())
|
|
|
return
|
|
|
}
|
|
|
if !isExist {
|
|
|
c.String(http.StatusOK, "输入的文件无法找到!")
|
|
|
return
|
|
|
}
|
|
|
//将文件上传到华为云
|
|
|
ObsUtil.UploadFileMultiPart(key, fullPath)
|
|
|
//输出预览地址
|
|
|
c.String(http.StatusOK, url)
|
|
|
}
|