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.9 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/obs"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/storage"
  16. )
  17. const (
  18. tplDirIndex base.TplName = "repo/datasets/dirs/index"
  19. )
  20. type RespGetDirs struct {
  21. ResultCode string `json:"resultCode"`
  22. FileInfos string `json:"fileInfos"`
  23. }
  24. func DeleteAllUnzipFile(attachment *models.Attachment, parentDir string) {
  25. uuid := attachment.UUID
  26. dirArray := strings.Split(parentDir, "/")
  27. //if !strings.HasSuffix(attachment.Name, ".zip") {
  28. if !isCanDecompress(attachment.Name) {
  29. log.Error("The file is not zip file, can not query the dir")
  30. return
  31. } else if attachment.DecompressState != models.DecompressStateDone {
  32. log.Error("The file has not been decompressed completely now")
  33. return
  34. }
  35. dirArray = append([]string{attachment.Name}, dirArray...)
  36. if parentDir == "" {
  37. dirArray = []string{attachment.Name}
  38. }
  39. if attachment.Type == models.TypeCloudBrainOne {
  40. dirs, err := GetDatasetDirs(uuid, parentDir)
  41. if err != nil {
  42. log.Error("getDatasetDirs failed:", err.Error())
  43. return
  44. }
  45. var fileInfos []storage.FileInfo
  46. err = json.Unmarshal([]byte(dirs), &fileInfos)
  47. if err != nil {
  48. log.Error("json.Unmarshal failed:", err.Error())
  49. return
  50. }
  51. for _, fileInfo := range fileInfos {
  52. log.Info("fileName=" + fileInfo.FileName)
  53. log.Info("parentDir=" + fileInfo.ParenDir)
  54. if fileInfo.IsDir {
  55. DeleteAllUnzipFile(attachment, fileInfo.ParenDir)
  56. } else {
  57. absolutepath := path.Join(attachment.RelativePath()+attachment.UUID, fileInfo.ParenDir)
  58. log.Info("absolutepath=" + absolutepath)
  59. storage.Attachments.Delete(absolutepath)
  60. }
  61. }
  62. }
  63. if attachment.Type == models.TypeCloudBrainTwo {
  64. input := &obs.ListObjectsInput{}
  65. input.Bucket = setting.Bucket
  66. // 设置每页100个对象
  67. input.MaxKeys = 100
  68. input.Prefix = setting.BasePath + attachment.RelativePath() + attachment.UUID
  69. index := 1
  70. log.Info("prefix=" + input.Prefix)
  71. for {
  72. output, err := storage.ObsCli.ListObjects(input)
  73. if err == nil {
  74. log.Info("Page:%d\n", index)
  75. index++
  76. for _, val := range output.Contents {
  77. log.Info("delete obs file:" + val.Key)
  78. delObj := &obs.DeleteObjectInput{}
  79. delObj.Bucket = setting.Bucket
  80. delObj.Key = val.Key
  81. storage.ObsCli.DeleteObject(delObj)
  82. }
  83. if output.IsTruncated {
  84. input.Marker = output.NextMarker
  85. } else {
  86. break
  87. }
  88. } else {
  89. if obsError, ok := err.(obs.ObsError); ok {
  90. log.Info("Code:%s\n", obsError.Code)
  91. log.Info("Message:%s\n", obsError.Message)
  92. }
  93. break
  94. }
  95. }
  96. }
  97. }
  98. func DirIndex(ctx *context.Context) {
  99. uuid := ctx.Params("uuid")
  100. parentDir := ctx.Query("parentDir")
  101. dirArray := strings.Split(parentDir, "/")
  102. attachment, err := models.GetAttachmentByUUID(uuid)
  103. if err != nil {
  104. ctx.ServerError("GetDatasetAttachments", err)
  105. return
  106. }
  107. //if !strings.HasSuffix(attachment.Name, ".zip") {
  108. if !isCanDecompress(attachment.Name) {
  109. log.Error("The file is not zip file, can not query the dir")
  110. 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"))
  111. return
  112. } else if attachment.DecompressState != models.DecompressStateDone {
  113. log.Error("The file has not been decompressed completely now")
  114. ctx.ServerError("The file has not been decompressed completely now", errors.New("The file has not been decompressed completely now"))
  115. return
  116. }
  117. dirArray = append([]string{attachment.Name}, dirArray...)
  118. if parentDir == "" {
  119. dirArray = []string{attachment.Name}
  120. }
  121. /*
  122. dirs, err := GetDatasetDirs(uuid, parentDir)
  123. if err != nil {
  124. log.Error("getDatasetDirs failed:", err.Error())
  125. ctx.ServerError("getDatasetDirs failed:", err)
  126. return
  127. }
  128. */
  129. //var fileInfos []FileInfo
  130. /*
  131. err = json.Unmarshal([]byte(dirs), &fileInfos)
  132. if err != nil {
  133. log.Error("json.Unmarshal failed:", err.Error())
  134. ctx.ServerError("json.Unmarshal failed:", err)
  135. return
  136. }
  137. */
  138. ctx.Data["Path"] = dirArray
  139. ctx.Data["Dirs"] = true
  140. ctx.Data["Uuid"] = uuid
  141. ctx.Data["PageIsDataset"] = true
  142. ctx.HTML(200, tplDirIndex)
  143. }
  144. func GetDatasetDirs(uuid string, parentDir string) (string, error) {
  145. var req string
  146. dataActualPath := setting.Attachment.Minio.RealPath +
  147. setting.Attachment.Minio.Bucket + "/" +
  148. setting.Attachment.Minio.BasePath +
  149. models.AttachmentRelativePath(uuid) +
  150. uuid + "/"
  151. if parentDir == "" {
  152. req = "baseDir=" + dataActualPath
  153. } else {
  154. req = "baseDir=" + dataActualPath + "&parentDir=" + parentDir
  155. }
  156. return getDirs(req)
  157. }
  158. func getDirs(req string) (string, error) {
  159. var dirs string
  160. url := setting.DecompressAddress + "/dirs?" + req
  161. reqHttp, err := http.NewRequest(http.MethodGet, url, nil)
  162. if err != nil {
  163. log.Error("http.NewRequest failed:", err.Error())
  164. return dirs, err
  165. }
  166. reqHttp.SetBasicAuth(setting.AuthUser, setting.AuthPassword)
  167. res, err := http.DefaultClient.Do(reqHttp)
  168. if err != nil {
  169. log.Error("send http to decompress failed:", err.Error())
  170. return dirs, err
  171. }
  172. if res.StatusCode != http.StatusOK {
  173. log.Error("the response from decompress is failed")
  174. return dirs, errors.New("the response from decompress is failed")
  175. }
  176. body, err := ioutil.ReadAll(res.Body)
  177. if err != nil {
  178. log.Error("read resp body failed:", err.Error())
  179. return dirs, err
  180. }
  181. var resp RespGetDirs
  182. err = json.Unmarshal(body, &resp)
  183. if err != nil {
  184. log.Error("unmarshal resp failed:", err.Error())
  185. return dirs, err
  186. }
  187. if resp.ResultCode != "0" {
  188. log.Error("GetDirs failed:", resp.ResultCode)
  189. return dirs, errors.New("GetDirs failed")
  190. }
  191. dirs = resp.FileInfos
  192. return dirs, nil
  193. }