|
- package repo
-
- import (
- "encoding/json"
- "errors"
- "io/ioutil"
- "net/http"
- "strings"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/base"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/setting"
- )
-
- const (
- tplDirIndex base.TplName = "repo/datasets/dirs/index"
- )
-
- type FileInfo struct {
- 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) {
- uuid := ctx.Params("uuid")
- parentDir := ctx.Query("parentDir")
- dirArray := strings.Split(parentDir, "/")
-
- attachment, err := models.GetAttachmentByUUID(uuid)
- if err != nil {
- ctx.ServerError("GetDatasetAttachments", err)
- return
- }
-
- if !strings.HasSuffix(attachment.Name, ".zip") {
- log.Error("The file is not zip file, can not query the dir")
- ctx.ServerError("The file is not zip file, can not query the dir", errors.New("The file is not zip file, can not query the dir"))
- return
- } else if attachment.DecompressState != models.DecompressStateDone {
- log.Error("The file has not been decompressed completely now")
- ctx.ServerError("The file has not been decompressed completely now", errors.New("The file has not been decompressed completely now"))
- return
- }
-
- dirArray = append([]string{attachment.Name}, dirArray...)
- if parentDir == "" {
- dirArray = []string{attachment.Name}
- }
-
- dirs, err := getDirs(uuid, parentDir)
- if err != nil {
- log.Error("getDirs failed:", err.Error())
- ctx.ServerError("getDirs failed:", err)
- return
- }
-
- var fileInfos []FileInfo
- 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
- ctx.Data["Dirs"] = fileInfos
- ctx.Data["PageIsDataset"] = true
-
- ctx.HTML(200, tplDirIndex)
- }
-
- 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 {
- log.Error("send http to decompress failed:", err.Error())
- return dirs, err
- }
-
- 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 {
- 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")
- }
-
- dirs = resp.FileInfos
-
- return dirs, nil
- }
|