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.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 := getDirs(uuid, parentDir)
  52. if err != nil {
  53. log.Error("getDirs failed:", err.Error())
  54. ctx.ServerError("getDirs 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["PageIsDataset"] = true
  67. ctx.HTML(200, tplDirIndex)
  68. }
  69. func getDirs(uuid string, parentDir string) (string, error) {
  70. var dirs string
  71. var req string
  72. if parentDir == "" {
  73. req = "uuid=" + uuid
  74. } else {
  75. req = "uuid=" + uuid + "&parentDir=" + parentDir
  76. }
  77. url := setting.DecompressAddress + "/dirs?" + req
  78. reqHttp, err := http.NewRequest(http.MethodGet, url, nil)
  79. if err != nil {
  80. log.Error("http.NewRequest failed:", err.Error())
  81. return dirs, err
  82. }
  83. reqHttp.SetBasicAuth(setting.AuthUser, setting.AuthPassword)
  84. res, err := http.DefaultClient.Do(reqHttp)
  85. if err != nil {
  86. log.Error("send http to decompress failed:", err.Error())
  87. return dirs, err
  88. }
  89. if res.StatusCode != http.StatusOK {
  90. log.Error("the response from decompress is failed")
  91. return dirs, errors.New("the response from decompress is failed")
  92. }
  93. body, err := ioutil.ReadAll(res.Body)
  94. if err != nil {
  95. log.Error("read resp body failed:", err.Error())
  96. return dirs, err
  97. }
  98. var resp RespGetDirs
  99. err = json.Unmarshal(body, &resp)
  100. if err != nil {
  101. log.Error("unmarshal resp failed:", err.Error())
  102. return dirs, err
  103. }
  104. if resp.ResultCode != "0" {
  105. log.Error("GetDirs failed:", resp.ResultCode)
  106. return dirs, errors.New("GetDirs failed")
  107. }
  108. dirs = resp.FileInfos
  109. return dirs, nil
  110. }