Browse Source

add auth

tags/v1.21.12.1
yuyuanshifu 5 years ago
parent
commit
1fb62e7a7f
3 changed files with 82 additions and 47 deletions
  1. +5
    -0
      custom/conf/app.ini.sample
  2. +10
    -0
      modules/setting/setting.go
  3. +67
    -47
      routers/repo/dir.go

+ 5
- 0
custom/conf/app.ini.sample View File

@@ -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

+ 10
- 0
modules/setting/setting.go View File

@@ -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 {


+ 67
- 47
routers/repo/dir.go View File

@@ -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
}

Loading…
Cancel
Save