You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dir.go 3.6 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package repo
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. const (
  15. tplDirIndex base.TplName = "repo/datasets/dirs/index"
  16. )
  17. type FileInfo struct {
  18. FileName string `json:"FileName"`
  19. ModTime string `json:"ModTime"`
  20. IsDir bool `json:"IsDir"`
  21. Size int64 `json:"Size"`
  22. ParenDir string `json:"ParenDir"`
  23. UUID string `json:"UUID"`
  24. }
  25. type RespGetDirs struct {
  26. ResultCode string `json:"resultCode"`
  27. FileInfos string `json:"fileInfos"`
  28. }
  29. func DirIndex(ctx *context.Context) {
  30. uuid := ctx.Params("uuid")
  31. parentDir := ctx.Query("parentDir")
  32. dirArray := strings.Split(parentDir, "/")
  33. attachment, err := models.GetAttachmentByUUID(uuid)
  34. if err != nil {
  35. ctx.ServerError("GetDatasetAttachments", err)
  36. return
  37. }
  38. if !strings.HasSuffix(attachment.Name, ".zip") {
  39. log.Error("The file is not zip file, can not query the dir")
  40. 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"))
  41. return
  42. } else if attachment.DecompressState != models.DecompressStateDone {
  43. log.Error("The file has not been decompressed completely now")
  44. ctx.ServerError("The file has not been decompressed completely now", errors.New("The file has not been decompressed completely now"))
  45. return
  46. }
  47. dirArray = append([]string{attachment.Name}, dirArray...)
  48. if parentDir == "" {
  49. dirArray = []string{attachment.Name}
  50. }
  51. dirs, err := getDatasetDirs(uuid, parentDir)
  52. if err != nil {
  53. log.Error("getDatasetDirs failed:", err.Error())
  54. ctx.ServerError("getDatasetDirs failed:", err)
  55. return
  56. }
  57. var fileInfos []FileInfo
  58. err = json.Unmarshal([]byte(dirs), &fileInfos)
  59. if err != nil {
  60. log.Error("json.Unmarshal failed:", err.Error())
  61. ctx.ServerError("json.Unmarshal failed:", err)
  62. return
  63. }
  64. ctx.Data["Path"] = dirArray
  65. ctx.Data["Dirs"] = fileInfos
  66. ctx.Data["Uuid"] = uuid
  67. ctx.Data["PageIsDataset"] = true
  68. ctx.HTML(200, tplDirIndex)
  69. }
  70. func getDatasetDirs(uuid string, parentDir string) (string, error) {
  71. var req string
  72. dataActualPath := setting.Attachment.Minio.RealPath +
  73. setting.Attachment.Minio.Bucket + "/" +
  74. setting.Attachment.Minio.BasePath +
  75. models.AttachmentRelativePath(uuid) +
  76. uuid + "/"
  77. if parentDir == "" {
  78. req = "baseDir=" + dataActualPath
  79. } else {
  80. req = "baseDir=" + dataActualPath + "&parentDir=" + parentDir
  81. }
  82. return getDirs(req)
  83. }
  84. func getDirs(req string) (string, error) {
  85. var dirs string
  86. url := setting.DecompressAddress + "/dirs?" + req
  87. reqHttp, err := http.NewRequest(http.MethodGet, url, nil)
  88. if err != nil {
  89. log.Error("http.NewRequest failed:", err.Error())
  90. return dirs, err
  91. }
  92. reqHttp.SetBasicAuth(setting.AuthUser, setting.AuthPassword)
  93. res, err := http.DefaultClient.Do(reqHttp)
  94. if err != nil {
  95. log.Error("send http to decompress failed:", err.Error())
  96. return dirs, err
  97. }
  98. if res.StatusCode != http.StatusOK {
  99. log.Error("the response from decompress is failed")
  100. return dirs, errors.New("the response from decompress is failed")
  101. }
  102. body, err := ioutil.ReadAll(res.Body)
  103. if err != nil {
  104. log.Error("read resp body failed:", err.Error())
  105. return dirs, err
  106. }
  107. var resp RespGetDirs
  108. err = json.Unmarshal(body, &resp)
  109. if err != nil {
  110. log.Error("unmarshal resp failed:", err.Error())
  111. return dirs, err
  112. }
  113. if resp.ResultCode != "0" {
  114. log.Error("GetDirs failed:", resp.ResultCode)
  115. return dirs, errors.New("GetDirs failed")
  116. }
  117. dirs = resp.FileInfos
  118. return dirs, nil
  119. }