From e75f2af9d0e1f3713453ec2ca438bb1bf95f59fb Mon Sep 17 00:00:00 2001 From: huanghai <10402852@qq.com> Date: Wed, 15 Jul 2020 15:31:59 +0800 Subject: [PATCH] 'commit' --- .../PreviewController/PreviewController.go | 15 ++++++++---- dsSupport/Test/PreviewTest/PreviewTest.go | 15 ++++++++---- dsSupport/Utils/CommonUtil/CommonUtil.go | 24 ++++++++++++++++++- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/dsSupport/Business/Preview/PreviewController/PreviewController.go b/dsSupport/Business/Preview/PreviewController/PreviewController.go index 96d67dca..3fd508b8 100644 --- a/dsSupport/Business/Preview/PreviewController/PreviewController.go +++ b/dsSupport/Business/Preview/PreviewController/PreviewController.go @@ -10,6 +10,7 @@ import ( "io" "net/http" "os" + "path" "path/filepath" "strings" ) @@ -29,6 +30,9 @@ func Routers(r *gin.RouterGroup) { 时间: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) @@ -39,15 +43,19 @@ func previewOffice(c *gin.Context) { 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 } - //文件名称 - arr := strings.Split(fullPath, "/") - fi := arr[len(arr)-1] fullPath = CommonUtil.GetCurrentPath() + "/TempFiles/" + fi if FileUtil.Exists(fullPath) { os.Remove(fullPath) @@ -66,7 +74,6 @@ func previewOffice(c *gin.Context) { //(1)只允许上传office系列的文件 //(2)在上传前判断文件是否已存在,存在了直接返回,不做重复工作,也可以支持强制刷新,需要添加参数 //(3)需要判断文件名是不是是标准的GUID形式,不是的不让上传 - //(4)对于异常的明显提示信息 //判断是不是本地存在 isExist, err := CommonUtil.PathExists(fullPath) diff --git a/dsSupport/Test/PreviewTest/PreviewTest.go b/dsSupport/Test/PreviewTest/PreviewTest.go index 6243842b..1aebdb1a 100644 --- a/dsSupport/Test/PreviewTest/PreviewTest.go +++ b/dsSupport/Test/PreviewTest/PreviewTest.go @@ -7,6 +7,11 @@ import ( "net/http" ) +/** +功能:显示预览 +作者:黄海 +时间:2020-07-15 + */ func showPreview(input string){ uEnc := base64.URLEncoding.EncodeToString([]byte(input)) fmt.Println(uEnc) @@ -21,12 +26,14 @@ func showPreview(input string){ func main() { //(1)传入本地文件路径 // 如果要用在url中,需要使用URLEncoding - input:="C:\\Users\\Administrator\\Desktop\\技术讨论会\\20A4782A-512C-40ED-8AF9-FCFC7CD29E5B.docx" - showPreview(input) + //input:="C:\\Users\\Administrator\\Desktop\\技术讨论会\\20A4782A-512C-40ED-8AF9-FCFC7CD29E5B.docx" + //showPreview(input) //(2)传入http参数 - input="https://dsideal.obs.cn-north-1.myhuaweicloud.com/Material/02/0200C9BE-07C6-4EFC-A945-8554C7D41790.doc" - showPreview(input) + //input="https://dsideal.obs.cn-north-1.myhuaweicloud.com/Material/02/0200C9BE-07C6-4EFC-A945-8554C7D41790.doc" + //showPreview(input) + input:=`E:\Work\ShlProjectJava\src\main\java\com\shl\zuowen\model\zuowenModel.java` + showPreview(input) //Java的调用方法示例: //https://blog.csdn.net/orangleliu/article/details/38309367 } diff --git a/dsSupport/Utils/CommonUtil/CommonUtil.go b/dsSupport/Utils/CommonUtil/CommonUtil.go index 3064de22..9df2eae3 100644 --- a/dsSupport/Utils/CommonUtil/CommonUtil.go +++ b/dsSupport/Utils/CommonUtil/CommonUtil.go @@ -92,4 +92,26 @@ func GetCurrentParentPath()string{ */ func GetParentPath(directory string) string { return strings.Replace(filepath.Dir(directory), "\\", "/", -1) -} \ No newline at end of file +} + +/** +功能:数组中是否存在某个字符串 +作者:黄海 +时间:2020-05-30 +*/ +func IsContainString(items []string, item string) bool { + for _, eachItem := range items { + if eachItem == item { + return true + } + } + return false +} +func IsContainInt32(items []int32, item int32) bool { + for _, eachItem := range items { + if eachItem == item { + return true + } + } + return false +}