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.

dataset.go 7.2 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package repo
  2. import (
  3. "net/http"
  4. "regexp"
  5. "sort"
  6. "strconv"
  7. "unicode/utf8"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  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. )
  15. const (
  16. tplIndex base.TplName = "repo/datasets/index"
  17. tplDatasetCreate base.TplName = "repo/datasets/create"
  18. tplDatasetEdit base.TplName = "repo/datasets/edit"
  19. )
  20. var titlePattern = regexp.MustCompile(`^[A-Za-z0-9-_\\.]{1,100}$`)
  21. // MustEnableDataset check if repository enable internal dataset
  22. func MustEnableDataset(ctx *context.Context) {
  23. if !ctx.Repo.CanRead(models.UnitTypeDatasets) {
  24. ctx.NotFound("MustEnableDataset", nil)
  25. return
  26. }
  27. }
  28. func newFilterPrivateAttachments(ctx *context.Context, list []*models.Attachment, repo *models.Repository) []*models.Attachment {
  29. if ctx.Repo.CanWrite(models.UnitTypeDatasets) {
  30. log.Info("can write.")
  31. return list
  32. } else {
  33. if repo.Owner == nil {
  34. repo.GetOwner()
  35. }
  36. permission := false
  37. if repo.Owner.IsOrganization() && ctx.User != nil {
  38. if repo.Owner.IsUserPartOfOrg(ctx.User.ID) {
  39. log.Info("user is member of org.")
  40. permission = true
  41. }
  42. }
  43. if !permission && ctx.User != nil {
  44. isCollaborator, _ := repo.IsCollaborator(ctx.User.ID)
  45. if isCollaborator {
  46. log.Info("Collaborator user may visit the attach.")
  47. permission = true
  48. }
  49. }
  50. var publicList []*models.Attachment
  51. for _, attach := range list {
  52. if !attach.IsPrivate {
  53. publicList = append(publicList, attach)
  54. } else {
  55. if permission {
  56. publicList = append(publicList, attach)
  57. }
  58. }
  59. }
  60. return publicList
  61. }
  62. }
  63. func QueryDataSet(ctx *context.Context) []*models.Attachment {
  64. repo := ctx.Repo.Repository
  65. dataset, err := models.GetDatasetByRepo(repo)
  66. if err != nil {
  67. log.Error("zou not found dataset 1")
  68. ctx.NotFound("GetDatasetByRepo", err)
  69. return nil
  70. }
  71. if ctx.Query("type") == "" {
  72. log.Error("zou not found type 2")
  73. ctx.NotFound("type error", nil)
  74. return nil
  75. }
  76. err = models.GetDatasetAttachments(ctx.QueryInt("type"), ctx.IsSigned, ctx.User, dataset)
  77. if err != nil {
  78. ctx.ServerError("GetDatasetAttachments", err)
  79. return nil
  80. }
  81. attachments := newFilterPrivateAttachments(ctx, dataset.Attachments, repo)
  82. ctx.Data["SortType"] = ctx.Query("sort")
  83. sort.Slice(attachments, func(i, j int) bool {
  84. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  85. })
  86. return attachments
  87. }
  88. func DatasetIndex(ctx *context.Context) {
  89. log.Info("dataset index 1")
  90. MustEnableDataset(ctx)
  91. repo := ctx.Repo.Repository
  92. dataset, err := models.GetDatasetByRepo(repo)
  93. if err != nil {
  94. log.Warn("query dataset, not found.")
  95. ctx.HTML(200, tplIndex)
  96. return
  97. }
  98. cloudbrainType := -1
  99. if ctx.Query("type") != "" {
  100. cloudbrainType = ctx.QueryInt("type")
  101. }
  102. err = models.GetDatasetAttachments(cloudbrainType, ctx.IsSigned, ctx.User, dataset)
  103. if err != nil {
  104. ctx.ServerError("GetDatasetAttachments", err)
  105. return
  106. }
  107. attachments := newFilterPrivateAttachments(ctx, dataset.Attachments, repo)
  108. sort.Slice(attachments, func(i, j int) bool {
  109. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  110. })
  111. page := ctx.QueryInt("page")
  112. if page <= 0 {
  113. page = 1
  114. }
  115. pagesize := ctx.QueryInt("pagesize")
  116. if pagesize <= 0 {
  117. pagesize = setting.UI.ExplorePagingNum
  118. }
  119. pager := context.NewPagination(len(attachments), pagesize, page, 5)
  120. pageAttachments := getPageAttachments(attachments, page, pagesize)
  121. ctx.Data["Page"] = pager
  122. ctx.Data["PageIsDataset"] = true
  123. ctx.Data["Title"] = ctx.Tr("dataset.show_dataset")
  124. ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets"
  125. ctx.Data["dataset"] = dataset
  126. ctx.Data["Attachments"] = pageAttachments
  127. ctx.Data["IsOwner"] = true
  128. ctx.Data["StoreType"] = setting.Attachment.StoreType
  129. ctx.Data["Type"] = cloudbrainType
  130. renderAttachmentSettings(ctx)
  131. ctx.HTML(200, tplIndex)
  132. }
  133. func getPageAttachments(attachments []*models.Attachment, page int, pagesize int) []*models.Attachment {
  134. begin := (page - 1) * pagesize
  135. end := (page) * pagesize
  136. if begin > len(attachments)-1 {
  137. return nil
  138. }
  139. if end > len(attachments)-1 {
  140. return attachments[begin:]
  141. } else {
  142. return attachments[begin:end]
  143. }
  144. }
  145. func CreateDataset(ctx *context.Context) {
  146. MustEnableDataset(ctx)
  147. ctx.HTML(200, tplDatasetCreate)
  148. }
  149. func EditDataset(ctx *context.Context) {
  150. MustEnableDataset(ctx)
  151. datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  152. dataset, _ := models.GetDatasetByID(datasetId)
  153. if dataset == nil {
  154. ctx.Error(http.StatusNotFound, "")
  155. return
  156. }
  157. ctx.Data["Dataset"] = dataset
  158. ctx.HTML(200, tplDatasetEdit)
  159. }
  160. func CreateDatasetPost(ctx *context.Context, form auth.CreateDatasetForm) {
  161. dataset := &models.Dataset{}
  162. if !titlePattern.MatchString(form.Title) {
  163. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err")))
  164. return
  165. }
  166. if utf8.RuneCountInString(form.Description) > 1024 {
  167. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err")))
  168. return
  169. }
  170. dataset.RepoID = ctx.Repo.Repository.ID
  171. dataset.UserID = ctx.User.ID
  172. dataset.Category = form.Category
  173. dataset.Task = form.Task
  174. dataset.Title = form.Title
  175. dataset.License = form.License
  176. dataset.Description = form.Description
  177. dataset.DownloadTimes = 0
  178. if ctx.Repo.Repository.IsPrivate {
  179. dataset.Status = 0
  180. } else {
  181. dataset.Status = 1
  182. }
  183. err := models.CreateDataset(dataset)
  184. if err != nil {
  185. log.Error("fail to create dataset", err)
  186. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.create_dataset_fail")))
  187. } else {
  188. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  189. }
  190. }
  191. func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) {
  192. ctx.Data["PageIsDataset"] = true
  193. ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset")
  194. if !titlePattern.MatchString(form.Title) {
  195. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err")))
  196. return
  197. }
  198. if utf8.RuneCountInString(form.Description) > 1024 {
  199. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err")))
  200. return
  201. }
  202. rel, err := models.GetDatasetByID(form.ID)
  203. ctx.Data["dataset"] = rel
  204. if err != nil {
  205. log.Error("failed to query dataset", err)
  206. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.query_dataset_fail")))
  207. return
  208. }
  209. rel.Title = form.Title
  210. rel.Description = form.Description
  211. rel.Category = form.Category
  212. rel.Task = form.Task
  213. rel.License = form.License
  214. if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil {
  215. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.query_dataset_fail")))
  216. }
  217. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  218. }
  219. func DatasetAction(ctx *context.Context) {
  220. var err error
  221. datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  222. switch ctx.Params(":action") {
  223. case "star":
  224. err = models.StarDataset(ctx.User.ID, datasetId, true)
  225. case "unstar":
  226. err = models.StarDataset(ctx.User.ID, datasetId, false)
  227. }
  228. if err != nil {
  229. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("repo.star_fail", ctx.Params(":action"))))
  230. } else {
  231. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  232. }
  233. }