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