Merge branch 'master' of 10.10.14.250:huanghai/dsMin

master
wubin 5 years ago
commit e440f5ad57

@ -1,42 +1,75 @@
package PreviewController
import (
"dsSupport/Const"
"dsSupport/Utils/CommonUtil"
"dsSupport/Utils/FileUtil"
"dsSupport/Utils/ObsUtil"
"encoding/base64"
"fmt"
"github.com/gin-gonic/gin"
"log"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
//模块的路由配置
func Routers(r *gin.RouterGroup) {
rr := r.Group("/preview")
//配置接口
rr.GET("/preview", preview)
rr.GET("/previewOffice", previewOffice)
return
}
// http://127.0.0.1:9000/support/preview/preview
func preview(c *gin.Context) {
// 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")
uEnc := c.Query("p")
uDec, err := base64.URLEncoding.DecodeString(uEnc)
if err != nil {
log.Fatalln(err)
}
fullPath:=string(uDec)
fmt.Println(fullPath)
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)
//TODO
//(1)保允许上传office系列的文件
//(2)在上传前判断文件是否已存在,存在了直接返回,不做重复工作,也可以支持强制刷新,需要添加参数
//(3)需要判断文件名是不是是标准的GUID形式不是的不让上传
//(4)对于异常的明显提示信息
//判断是不是本地存在
isExist, err := CommonUtil.PathExists(fullPath)
if err != nil {
@ -48,7 +81,7 @@ func preview(c *gin.Context) {
return
}
//将文件上传到华为云
key := "dsMinPreview/" + p
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

@ -0,0 +1,11 @@
_ _____ _
| | / ____| | |
__| |___| (___ _ _ _ __ _ __ ___ _ __| |_
/ _` / __|\___ \| | | | '_ \| '_ \ / _ \| '__| __|
| (_| \__ \____) | |_| | |_) | |_) | (_) | | | |_
\__,_|___/_____/ \__,_| .__/| .__/ \___/|_| \__|
| | | |
|_| |_|
Created By HuangHai 2020-01-20
http://patorjk.com/software/taag/#p=display&f=Big&t=dsSupport

@ -0,0 +1,3 @@
package Const
const PreviewPrefix = "dsMinPreview/"

@ -0,0 +1,11 @@
package CronTask
import (
"dsSupport/Const"
"dsSupport/Utils/ObsUtil"
)
func Task() {
//删除7天前上传的文件
ObsUtil.DeleteExpireFile(Const.PreviewPrefix, 7)
}

@ -7,18 +7,33 @@ import (
"net/http"
)
func main() {
// 如果要用在url中需要使用URLEncoding
input:="C:\\Users\\Administrator\\Desktop\\技术讨论会\\20A4782A-512C-40ED-8AF9-FCFC7CD29E5B.docx"
/**
:2020-07-15
*/
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)
input:=`E:\Work\ShlProjectJava\src\main\java\com\shl\zuowen\model\zuowenModel.java`
showPreview(input)
//Java的调用方法示例
//https://blog.csdn.net/orangleliu/article/details/38309367
}

@ -7,6 +7,8 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
/**
@ -65,3 +67,51 @@ 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)
}
/**
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
}

@ -3,6 +3,8 @@ package FileUtil
import (
"bufio"
"fmt"
"io"
"log"
"os"
)
@ -57,3 +59,38 @@ func WriteLines(lines []string, path string) error {
}
/**
2019-12-31
*/
func ReadFileContent(filepath string) string {
file, err := os.Open(filepath)
if err != nil {
log.Print("文件打开失败:", err)
}
defer file.Close()
buf := make([]byte, 12) // 存放文件内容的缓存,相当于中转站
data := make([]byte, 0) // 用来存放文件内容buf读取的内容都会写到data里面去
for {
//无限循环,不断读取
n, err := file.Read(buf)
// 什么时候文件读完呢如果文件读完的话那么err不为nil而是io.EOF
// 所以我们可以进行判断
if err != nil {
//如果err != nil说明出错了但如果还等于io.EOF的话说明读完了因为文件读完err也不为nil。直接break
if err == io.EOF {
break
} else {
//如果错误不是io.EOF的话说明就真的在读取中出现了错误直接panic出来
//panic(err)
fmt.Println(err.Error())
}
}
//此时文件内容写到buf里面去了写了多少个呢写了n个那么我们再写到data里面去
data = append(data, buf[:n]...)
}
return string(data)
}

@ -122,7 +122,6 @@ func UploadFileMultiPart(key string,sourceFile string){
close(partChan)
}
}
completeMultipartUploadInput := &obs.CompleteMultipartUploadInput{}
completeMultipartUploadInput.Bucket = ConfigUtil.Bucket
completeMultipartUploadInput.Key = key

@ -9,6 +9,7 @@ require (
github.com/json-iterator/go v1.1.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/robfig/cron/v3 v3.0.0
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/ini.v1 v1.57.0

@ -57,6 +57,8 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

@ -1,11 +1,20 @@
package main
import (
"dsSupport/CronTask"
"dsSupport/Router"
"dsSupport/Utils/FileUtil"
"fmt"
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
)
func main(){
// 一、显示Logo
configIniFile := "./Config/logo.txt"
var logo = FileUtil.ReadFileContent(configIniFile)
fmt.Print(logo)
r := gin.Default()
//主路由
Router.GinRouter(r)
@ -14,6 +23,20 @@ func main(){
// http://127.0.0.1:9000/M3u8/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.m3u8
//r.Static("/M3u8", "./Target")
//定时任务
crontab:=cron.New(cron.WithSeconds()) //精确到秒
//定义定时器调用的任务函数
task := func() {
CronTask.Task()
}
//定时任务
spec := "0 0 */1 * * ?" //cron表达式每1小时一次
// 添加定时任务
crontab.AddFunc(spec, task)
// 启动定时器
crontab.Start()
// 指定地址和端口号
r.Run(":9000")
}

Loading…
Cancel
Save