diff --git a/dsSupport/Business/Preview/PreviewController/PreviewController.go b/dsSupport/Business/Preview/PreviewController/PreviewController.go index b7b1a582..96d67dca 100644 --- a/dsSupport/Business/Preview/PreviewController/PreviewController.go +++ b/dsSupport/Business/Preview/PreviewController/PreviewController.go @@ -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) diff --git a/dsSupport/TempFiles/0200C9BE-07C6-4EFC-A945-8554C7D41790.doc b/dsSupport/TempFiles/0200C9BE-07C6-4EFC-A945-8554C7D41790.doc new file mode 100644 index 00000000..d4d89681 Binary files /dev/null and b/dsSupport/TempFiles/0200C9BE-07C6-4EFC-A945-8554C7D41790.doc differ diff --git a/dsSupport/Test/PreviewTest/PreviewTest.go b/dsSupport/Test/PreviewTest/PreviewTest.go index a5d6078b..6243842b 100644 --- a/dsSupport/Test/PreviewTest/PreviewTest.go +++ b/dsSupport/Test/PreviewTest/PreviewTest.go @@ -7,17 +7,25 @@ import ( "net/http" ) -func main() { - // 如果要用在url中,需要使用URLEncoding - input:="C:\\Users\\Administrator\\Desktop\\技术讨论会\\20A4782A-512C-40ED-8AF9-FCFC7CD29E5B.docx" +func showPreview(input string){ uEnc := base64.URLEncoding.EncodeToString([]byte(input)) fmt.Println(uEnc) //生成预览OFFICE的服务地址 - url := "http://10.10.24.100:9000/support/preview/preview" + url := "http://10.10.24.100:9000/support/preview/previewOffice" resp, _ := http.Get(url+"?p="+uEnc) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body)) +} + +func main() { + //(1)传入本地文件路径 + // 如果要用在url中,需要使用URLEncoding + 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) //Java的调用方法示例: //https://blog.csdn.net/orangleliu/article/details/38309367 diff --git a/dsSupport/Utils/CommonUtil/CommonUtil.go b/dsSupport/Utils/CommonUtil/CommonUtil.go index a796c5cf..3064de22 100644 --- a/dsSupport/Utils/CommonUtil/CommonUtil.go +++ b/dsSupport/Utils/CommonUtil/CommonUtil.go @@ -7,6 +7,8 @@ import ( "log" "os" "os/exec" + "path/filepath" + "strings" ) /** @@ -65,3 +67,29 @@ func PathExists(path string) (bool, error) { } return false, err } + + +/** +功能:获取当前路径 +作者:黄海 +时间:2020-06-02 +*/ +func GetCurrentPath() string { + dir, _ := os.Getwd() + dir = strings.ReplaceAll(dir, "\\", "/") + return dir +} +/** +功能:获取当前工作目录的父路径 +作者:黄海 +时间:2020-06-02 +*/ +func GetCurrentParentPath()string{ + return GetParentPath(GetCurrentPath()) +} +/** +功能:返回上一级目录 +*/ +func GetParentPath(directory string) string { + return strings.Replace(filepath.Dir(directory), "\\", "/", -1) +} \ No newline at end of file diff --git a/dsSupport/Utils/FileUtil/FileUtil.go b/dsSupport/Utils/FileUtil/FileUtil.go index 4fc6e5a1..49d9c658 100644 --- a/dsSupport/Utils/FileUtil/FileUtil.go +++ b/dsSupport/Utils/FileUtil/FileUtil.go @@ -56,4 +56,3 @@ func WriteLines(lines []string, path string) error { return w.Flush() } -