diff --git a/dsSupport/ConvertMovie/Convert.go b/dsSupport/Business/ConvertMovie/Convert.go similarity index 100% rename from dsSupport/ConvertMovie/Convert.go rename to dsSupport/Business/ConvertMovie/Convert.go diff --git a/dsSupport/Business/Preview/PreviewController/PreviewController.go b/dsSupport/Business/Preview/PreviewController/PreviewController.go index 82a0eb30..2781a55b 100644 --- a/dsSupport/Business/Preview/PreviewController/PreviewController.go +++ b/dsSupport/Business/Preview/PreviewController/PreviewController.go @@ -11,7 +11,6 @@ import ( "net/http" "os" "path" - "path/filepath" "strings" ) @@ -41,16 +40,28 @@ func previewOffice(c *gin.Context) { 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, "输入的预览文件,系统不支持!") + c.String(http.StatusOK, "输入的预览文件格式,系统不支持!") return } - //下载 + //准备下载 res, err := http.Get(fullPath) if err != nil { c.String(http.StatusOK, "输入的网络地址无法找到,不能进行预览!") @@ -68,8 +79,6 @@ func previewOffice(c *gin.Context) { io.Copy(f, res.Body) } //(2) 是不是本地文件 - _, p := filepath.Split(fullPath) - //判断是不是本地存在 isExist, err := CommonUtil.PathExists(fullPath) if err != nil { @@ -81,10 +90,7 @@ func previewOffice(c *gin.Context) { 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) } diff --git a/dsSupport/Test/PreviewTest/PreviewTest.go b/dsSupport/Test/PreviewTest/PreviewTest.go index 1aebdb1a..280cb532 100644 --- a/dsSupport/Test/PreviewTest/PreviewTest.go +++ b/dsSupport/Test/PreviewTest/PreviewTest.go @@ -14,7 +14,6 @@ import ( */ func showPreview(input string){ uEnc := base64.URLEncoding.EncodeToString([]byte(input)) - fmt.Println(uEnc) //生成预览OFFICE的服务地址 url := "http://10.10.24.100:9000/support/preview/previewOffice" resp, _ := http.Get(url+"?p="+uEnc) @@ -28,12 +27,13 @@ func main() { // 如果要用在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) - input:=`E:\Work\ShlProjectJava\src\main\java\com\shl\zuowen\model\zuowenModel.java` - 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/ObsUtil/ObsUtil.go b/dsSupport/Utils/ObsUtil/ObsUtil.go index 6ea50a04..87c05f0b 100644 --- a/dsSupport/Utils/ObsUtil/ObsUtil.go +++ b/dsSupport/Utils/ObsUtil/ObsUtil.go @@ -1,19 +1,34 @@ package ObsUtil import ( + "dsSupport/Const" "dsSupport/Utils/ConfigUtil" "dsSupport/Utils/obs" "fmt" + "net/http" "os" "time" ) +/** +功能:判断一个文件在OBS上是不是存在 +作者:黄海 +时间:2020-07-16 +*/ +func IsExist(key string) bool { + _, err := http.Head("https://"+ConfigUtil.Endpoint+"/"+ConfigUtil.Bucket+"/"+Const.PreviewPrefix + key) + if err != nil { + return false + } + return true +} + /** 功能:删除指定前缀的过期文件 作者:黄海 时间:2020-04-05 */ -func DeleteExpireFile(prefix string,remainDays int32){ +func DeleteExpireFile(prefix string, remainDays int32) { // 创建ObsClient结构体 var obsClient, _ = obs.New(ConfigUtil.Ak, ConfigUtil.Sk, ConfigUtil.Endpoint) @@ -32,14 +47,14 @@ func DeleteExpireFile(prefix string,remainDays int32){ now := time.Now() sumD := now.Sub(val.LastModified) if int32(sumD.Hours()/24) > remainDays { - fmt.Println("发现超时文件,将删除:"+val.Key) + fmt.Println("发现超时文件,将删除:" + val.Key) input := &obs.DeleteObjectInput{} input.Bucket = ConfigUtil.Bucket input.Key = val.Key obsClient.DeleteObject(input) fmt.Println("已成功删除!" + val.Key) - }else{ - fmt.Println("没有过期,无需删除:"+val.Key) + } else { + fmt.Println("没有过期,无需删除:" + val.Key) } } } else if obsError, ok := err.(obs.ObsError); ok { @@ -54,7 +69,7 @@ func DeleteExpireFile(prefix string,remainDays int32){ 作者:黄海 时间:2020-04-05 */ -func UploadFileMultiPart(key string,sourceFile string){ +func UploadFileMultiPart(key string, sourceFile string) { // 创建ObsClient结构体 var obsClient, _ = obs.New(ConfigUtil.Ak, ConfigUtil.Sk, ConfigUtil.Endpoint) // 初始化分段上传任务 diff --git a/dsSupport/main.go b/dsSupport/main.go index db1b7f97..cae0aada 100644 --- a/dsSupport/main.go +++ b/dsSupport/main.go @@ -10,7 +10,7 @@ import ( ) func main(){ - // 一、显示Logo + // 显示Logo configIniFile := "./Config/logo.txt" var logo = FileUtil.ReadFileContent(configIniFile) fmt.Print(logo)