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 5.1 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package repo
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net/http"
  7. "path"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/storage"
  15. )
  16. const (
  17. tplDirIndex base.TplName = "repo/datasets/dirs/index"
  18. )
  19. type RespGetDirs struct {
  20. ResultCode string `json:"resultCode"`
  21. FileInfos string `json:"fileInfos"`
  22. }
  23. func DeleteAllUnzipFile(attachment *models.Attachment, parentDir string) {
  24. uuid := attachment.UUID
  25. dirArray := strings.Split(parentDir, "/")
  26. //if !strings.HasSuffix(attachment.Name, ".zip") {
  27. if !isCanDecompress(attachment.Name) {
  28. log.Error("The file is not zip file, can not query the dir")
  29. return
  30. } else if attachment.DecompressState != models.DecompressStateDone {
  31. log.Error("The file has not been decompressed completely now")
  32. return
  33. }
  34. dirArray = append([]string{attachment.Name}, dirArray...)
  35. if parentDir == "" {
  36. dirArray = []string{attachment.Name}
  37. }
  38. if attachment.Type == models.TypeCloudBrainOne {
  39. dirs, err := GetDatasetDirs(uuid, parentDir)
  40. if err != nil {
  41. log.Error("getDatasetDirs failed:", err.Error())
  42. return
  43. }
  44. var fileInfos []storage.FileInfo
  45. err = json.Unmarshal([]byte(dirs), &fileInfos)
  46. if err != nil {
  47. log.Error("json.Unmarshal failed:", err.Error())
  48. return
  49. }
  50. for _, fileInfo := range fileInfos {
  51. log.Info("fileName=" + fileInfo.FileName)
  52. log.Info("parentDir=" + fileInfo.ParenDir)
  53. if fileInfo.IsDir {
  54. DeleteAllUnzipFile(attachment, fileInfo.ParenDir)
  55. } else {
  56. absolutepath := path.Join(attachment.RelativePath()+attachment.UUID, fileInfo.ParenDir)
  57. log.Info("absolutepath=" + absolutepath)
  58. storage.Attachments.Delete(absolutepath)
  59. }
  60. }
  61. }
  62. if attachment.Type == models.TypeCloudBrainTwo {
  63. err := storage.ObsRemoveObject(setting.Bucket, setting.BasePath+attachment.RelativePath()+attachment.UUID)
  64. if err != nil {
  65. log.Info("delete file error.")
  66. }
  67. }
  68. }
  69. func DirIndex(ctx *context.Context) {
  70. uuid := ctx.Params("uuid")
  71. parentDir := ctx.Query("parentDir")
  72. dirArray := strings.Split(parentDir, "/")
  73. attachment, err := models.GetAttachmentByUUID(uuid)
  74. if err != nil {
  75. ctx.ServerError("GetDatasetAttachments", err)
  76. return
  77. }
  78. //if !strings.HasSuffix(attachment.Name, ".zip") {
  79. if !isCanDecompress(attachment.Name) {
  80. log.Error("The file is not zip file, can not query the dir")
  81. 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"))
  82. return
  83. } else if attachment.DecompressState != models.DecompressStateDone {
  84. log.Error("The file has not been decompressed completely now")
  85. ctx.ServerError("The file has not been decompressed completely now", errors.New("The file has not been decompressed completely now"))
  86. return
  87. }
  88. dirArray = append([]string{attachment.Name}, dirArray...)
  89. if parentDir == "" {
  90. dirArray = []string{attachment.Name}
  91. }
  92. /*
  93. dirs, err := GetDatasetDirs(uuid, parentDir)
  94. if err != nil {
  95. log.Error("getDatasetDirs failed:", err.Error())
  96. ctx.ServerError("getDatasetDirs failed:", err)
  97. return
  98. }
  99. */
  100. //var fileInfos []FileInfo
  101. /*
  102. err = json.Unmarshal([]byte(dirs), &fileInfos)
  103. if err != nil {
  104. log.Error("json.Unmarshal failed:", err.Error())
  105. ctx.ServerError("json.Unmarshal failed:", err)
  106. return
  107. }
  108. */
  109. ctx.Data["Path"] = dirArray
  110. ctx.Data["Dirs"] = true
  111. ctx.Data["Uuid"] = uuid
  112. ctx.Data["PageIsDataset"] = true
  113. ctx.HTML(200, tplDirIndex)
  114. }
  115. func GetDatasetDirs(uuid string, parentDir string) (string, error) {
  116. var req string
  117. dataActualPath := setting.Attachment.Minio.RealPath +
  118. setting.Attachment.Minio.Bucket + "/" +
  119. setting.Attachment.Minio.BasePath +
  120. models.AttachmentRelativePath(uuid) +
  121. uuid + "/"
  122. if parentDir == "" {
  123. req = "baseDir=" + dataActualPath
  124. } else {
  125. req = "baseDir=" + dataActualPath + "&parentDir=" + parentDir
  126. }
  127. return getDirs(req)
  128. }
  129. func getDirs(req string) (string, error) {
  130. var dirs string
  131. url := setting.DecompressAddress + "/dirs?" + req
  132. reqHttp, err := http.NewRequest(http.MethodGet, url, nil)
  133. if err != nil {
  134. log.Error("http.NewRequest failed:", err.Error())
  135. return dirs, err
  136. }
  137. reqHttp.SetBasicAuth(setting.AuthUser, setting.AuthPassword)
  138. res, err := http.DefaultClient.Do(reqHttp)
  139. if err != nil {
  140. log.Error("send http to decompress failed:", err.Error())
  141. return dirs, err
  142. }
  143. if res.StatusCode != http.StatusOK {
  144. log.Error("the response from decompress is failed")
  145. return dirs, errors.New("the response from decompress is failed")
  146. }
  147. body, err := ioutil.ReadAll(res.Body)
  148. if err != nil {
  149. log.Error("read resp body failed:", err.Error())
  150. return dirs, err
  151. }
  152. var resp RespGetDirs
  153. err = json.Unmarshal(body, &resp)
  154. if err != nil {
  155. log.Error("unmarshal resp failed:", err.Error())
  156. return dirs, err
  157. }
  158. if resp.ResultCode != "0" {
  159. log.Error("GetDirs failed:", resp.ResultCode)
  160. return dirs, errors.New("GetDirs failed")
  161. }
  162. dirs = resp.FileInfos
  163. return dirs, nil
  164. }