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