From 1fb62e7a7fc0087d731ee93d8881b9be5f2f6805 Mon Sep 17 00:00:00 2001 From: yuyuanshifu <747342561@qq.com> Date: Wed, 16 Sep 2020 10:26:40 +0800 Subject: [PATCH] add auth --- custom/conf/app.ini.sample | 5 ++ modules/setting/setting.go | 10 ++++ routers/repo/dir.go | 114 ++++++++++++++++++++++--------------- 3 files changed, 82 insertions(+), 47 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 63ea95e65..f25ef8b8f 100755 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -1049,3 +1049,8 @@ RESULT_BACKEND = redis://localhost:6379 HOST = http://192.168.204.24 USERNAME = PASSWORD = + +[decompress] +HOST = http://192.168.207.34:39987 +USER = cW4cMtH24eoWPE7X +PASSWORD = 4BPmgvK2hb2Eywwyp4YZRY4B7yQf4DAC diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 2085b0873..d103fb796 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -425,6 +425,11 @@ var ( Broker string DefaultQueue string ResultBackend string + + //decompress config + DecompressAddress string + AuthUser string + AuthPassword string ) // DateLang transforms standard language locale name to corresponding value in datetime plugin. @@ -1087,6 +1092,11 @@ func NewContext() { Broker = sec.Key("BROKER").MustString("redis://localhost:6379") DefaultQueue = sec.Key("DEFAULT_QUEUE").MustString("DecompressTasksQueue") ResultBackend = sec.Key("RESULT_BACKEND").MustString("redis://localhost:6379") + + sec = Cfg.Section("decompress") + DecompressAddress = sec.Key("HOST").MustString("http://192.168.207.34:39987") + AuthUser = sec.Key("USER").MustString("cW4cMtH24eoWPE7X") + AuthPassword = sec.Key("PASSWORD").MustString("4BPmgvK2hb2Eywwyp4YZRY4B7yQf4DAC") } func loadInternalToken(sec *ini.Section) string { diff --git a/routers/repo/dir.go b/routers/repo/dir.go index 4d7cdafe8..a44227c66 100755 --- a/routers/repo/dir.go +++ b/routers/repo/dir.go @@ -1,10 +1,10 @@ package repo import ( + "encoding/json" "errors" - "os" - "path" - "sort" + "io/ioutil" + "net/http" "strings" "code.gitea.io/gitea/models" @@ -19,12 +19,17 @@ const ( ) type FileInfo struct { - FileName string - ModTime string - IsDir bool - Size int64 - ParenDir string - UUID string + FileName string `json:"FileName"` + ModTime string `json:"ModTime"` + IsDir bool `json:"IsDir"` + Size int64 `json:"Size"` + ParenDir string `json:"ParenDir"` + UUID string `json:"UUID"` +} + +type RespGetDirs struct { + ResultCode string `json:"resultCode"` + FileInfos string `json:"fileInfos"` } func DirIndex(ctx *context.Context) { @@ -53,39 +58,19 @@ func DirIndex(ctx *context.Context) { dirArray = []string{attachment.Name} } - files, err := readDir(setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + "/" + setting.Attachment.Minio.BasePath + - path.Join(uuid[0:1], uuid[1:2], uuid+uuid) + "/" + parentDir) + dirs, err := getDirs(uuid, parentDir) if err != nil { - log.Error("ReadDir failed:", err.Error()) - ctx.ServerError("ReadDir failed:", err) + log.Error("getDirs failed:", err.Error()) + ctx.ServerError("getDirs failed:", err) return } - i := 1 var fileInfos []FileInfo - for _, file := range files { - if i > 100 { - break - } - - log.Info(file.Name()) - - var tmp string - if parentDir == "" { - tmp = file.Name() - } else { - tmp = parentDir + "/" + file.Name() - } - - fileInfos = append(fileInfos, FileInfo{ - FileName: file.Name(), - ModTime: file.ModTime().Format("2006-01-02 15:04:05"), - IsDir: file.IsDir(), - Size: file.Size(), - ParenDir: tmp, - UUID: uuid, - }) - i++ + err = json.Unmarshal([]byte(dirs), &fileInfos) + if err != nil { + log.Error("json.Unmarshal failed:", err.Error()) + ctx.ServerError("json.Unmarshal failed:", err) + return } ctx.Data["Path"] = dirArray @@ -95,18 +80,53 @@ func DirIndex(ctx *context.Context) { ctx.HTML(200, tplDirIndex) } -// readDir reads the directory named by dirname and returns -// a list of directory entries sorted by filename. -func readDir(dirname string) ([]os.FileInfo, error) { - f, err := os.Open(dirname) +func getDirs(uuid string, parentDir string) (string,error) { + var dirs string + var req string + if parentDir == "" { + req = "uuid=" + uuid + } else { + req = "uuid=" + uuid + "&parentDir=" + parentDir + } + + url := setting.DecompressAddress + "/dirs?" + req + reqHttp, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + log.Error("http.NewRequest failed:", err.Error()) + return dirs, err + } + + reqHttp.SetBasicAuth(setting.AuthUser, setting.AuthPassword) + res, err := http.DefaultClient.Do(reqHttp) if err != nil { - return nil, err + log.Error("send http to decompress failed:", err.Error()) + return dirs, err } - list, err := f.Readdir(100) - f.Close() + + if res.StatusCode != http.StatusOK { + log.Error("the response from decompress is failed") + return dirs, errors.New("the response from decompress is failed") + } + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Error("read resp body failed:", err.Error()) + return dirs, err + } + + var resp RespGetDirs + err = json.Unmarshal(body, &resp) if err != nil { - return nil, err + log.Error("unmarshal resp failed:", err.Error()) + return dirs, err + } + + if resp.ResultCode != "0" { + log.Error("GetDirs failed:", resp.ResultCode) + return dirs, errors.New("GetDirs failed") } - sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() }) - return list, nil + + dirs = resp.FileInfos + + return dirs, nil }