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

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
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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package repo
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "regexp"
  6. "sort"
  7. "strconv"
  8. "unicode/utf8"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. const (
  17. tplIndex base.TplName = "repo/datasets/index"
  18. tplDatasetCreate base.TplName = "repo/datasets/create"
  19. tplDatasetEdit base.TplName = "repo/datasets/edit"
  20. taskstplIndex base.TplName = "repo/datasets/tasks/index"
  21. )
  22. var titlePattern = regexp.MustCompile(`^[A-Za-z0-9-_\\.]{1,100}$`)
  23. // MustEnableDataset check if repository enable internal dataset
  24. func MustEnableDataset(ctx *context.Context) {
  25. if !ctx.Repo.CanRead(models.UnitTypeDatasets) {
  26. ctx.NotFound("MustEnableDataset", nil)
  27. return
  28. }
  29. }
  30. func newFilterPrivateAttachments(ctx *context.Context, list []*models.Attachment, repo *models.Repository) []*models.Attachment {
  31. if ctx.Repo.CanWrite(models.UnitTypeDatasets) {
  32. log.Info("can write.")
  33. return list
  34. } else {
  35. if repo.Owner == nil {
  36. repo.GetOwner()
  37. }
  38. permission := false
  39. if repo.Owner.IsOrganization() && ctx.User != nil {
  40. if repo.Owner.IsUserPartOfOrg(ctx.User.ID) {
  41. log.Info("user is member of org.")
  42. permission = true
  43. }
  44. }
  45. if !permission && ctx.User != nil {
  46. isCollaborator, _ := repo.IsCollaborator(ctx.User.ID)
  47. if isCollaborator {
  48. log.Info("Collaborator user may visit the attach.")
  49. permission = true
  50. }
  51. }
  52. var publicList []*models.Attachment
  53. for _, attach := range list {
  54. if !attach.IsPrivate {
  55. publicList = append(publicList, attach)
  56. } else {
  57. if permission {
  58. publicList = append(publicList, attach)
  59. }
  60. }
  61. }
  62. return publicList
  63. }
  64. }
  65. func QueryDataSet(ctx *context.Context) []*models.Attachment {
  66. repo := ctx.Repo.Repository
  67. dataset, err := models.GetDatasetByRepo(repo)
  68. if err != nil {
  69. log.Error("zou not found dataset 1")
  70. ctx.NotFound("GetDatasetByRepo", err)
  71. return nil
  72. }
  73. if ctx.Query("type") == "" {
  74. log.Error("zou not found type 2")
  75. ctx.NotFound("type error", nil)
  76. return nil
  77. }
  78. err = models.GetDatasetAttachments(ctx.QueryInt("type"), ctx.IsSigned, ctx.User, dataset)
  79. if err != nil {
  80. ctx.ServerError("GetDatasetAttachments", err)
  81. return nil
  82. }
  83. attachments := newFilterPrivateAttachments(ctx, dataset.Attachments, repo)
  84. ctx.Data["SortType"] = ctx.Query("sort")
  85. sort.Slice(attachments, func(i, j int) bool {
  86. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  87. })
  88. return attachments
  89. }
  90. func DatasetIndex(ctx *context.Context) {
  91. log.Info("dataset index 1")
  92. MustEnableDataset(ctx)
  93. repo := ctx.Repo.Repository
  94. dataset, err := models.GetDatasetByRepo(repo)
  95. if err != nil {
  96. log.Warn("query dataset, not found.")
  97. ctx.HTML(200, tplIndex)
  98. return
  99. }
  100. cloudbrainType := -1
  101. if ctx.Query("type") != "" {
  102. cloudbrainType = ctx.QueryInt("type")
  103. }
  104. err = models.GetDatasetAttachments(cloudbrainType, ctx.IsSigned, ctx.User, dataset)
  105. if err != nil {
  106. ctx.ServerError("GetDatasetAttachments", err)
  107. return
  108. }
  109. attachments := newFilterPrivateAttachments(ctx, dataset.Attachments, repo)
  110. sort.Slice(attachments, func(i, j int) bool {
  111. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  112. })
  113. page := ctx.QueryInt("page")
  114. if page <= 0 {
  115. page = 1
  116. }
  117. pagesize := ctx.QueryInt("pagesize")
  118. if pagesize <= 0 {
  119. pagesize = setting.UI.ExplorePagingNum
  120. }
  121. pager := context.NewPagination(len(attachments), pagesize, page, 5)
  122. pageAttachments := getPageAttachments(attachments, page, pagesize)
  123. ctx.Data["Page"] = pager
  124. ctx.Data["PageIsDataset"] = true
  125. ctx.Data["Title"] = ctx.Tr("dataset.show_dataset")
  126. ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets"
  127. ctx.Data["dataset"] = dataset
  128. ctx.Data["Attachments"] = pageAttachments
  129. ctx.Data["IsOwner"] = true
  130. ctx.Data["StoreType"] = setting.Attachment.StoreType
  131. ctx.Data["Type"] = cloudbrainType
  132. renderAttachmentSettings(ctx)
  133. ctx.HTML(200, tplIndex)
  134. }
  135. func getPageAttachments(attachments []*models.Attachment, page int, pagesize int) []*models.Attachment {
  136. begin := (page - 1) * pagesize
  137. end := (page) * pagesize
  138. if begin > len(attachments)-1 {
  139. return nil
  140. }
  141. if end > len(attachments)-1 {
  142. return attachments[begin:]
  143. } else {
  144. return attachments[begin:end]
  145. }
  146. }
  147. func CreateDataset(ctx *context.Context) {
  148. MustEnableDataset(ctx)
  149. ctx.HTML(200, tplDatasetCreate)
  150. }
  151. func EditDataset(ctx *context.Context) {
  152. MustEnableDataset(ctx)
  153. datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  154. dataset, _ := models.GetDatasetByID(datasetId)
  155. if dataset == nil {
  156. ctx.Error(http.StatusNotFound, "")
  157. return
  158. }
  159. ctx.Data["Dataset"] = dataset
  160. ctx.HTML(200, tplDatasetEdit)
  161. }
  162. func CreateDatasetPost(ctx *context.Context, form auth.CreateDatasetForm) {
  163. dataset := &models.Dataset{}
  164. if !titlePattern.MatchString(form.Title) {
  165. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err")))
  166. return
  167. }
  168. if utf8.RuneCountInString(form.Description) > 1024 {
  169. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err")))
  170. return
  171. }
  172. dataset.RepoID = ctx.Repo.Repository.ID
  173. dataset.UserID = ctx.User.ID
  174. dataset.Category = form.Category
  175. dataset.Task = form.Task
  176. dataset.Title = form.Title
  177. dataset.License = form.License
  178. dataset.Description = form.Description
  179. dataset.DownloadTimes = 0
  180. if ctx.Repo.Repository.IsPrivate {
  181. dataset.Status = 0
  182. } else {
  183. dataset.Status = 1
  184. }
  185. err := models.CreateDataset(dataset)
  186. if err != nil {
  187. log.Error("fail to create dataset", err)
  188. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.create_dataset_fail")))
  189. } else {
  190. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  191. }
  192. }
  193. func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) {
  194. ctx.Data["PageIsDataset"] = true
  195. ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset")
  196. if !titlePattern.MatchString(form.Title) {
  197. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err")))
  198. return
  199. }
  200. if utf8.RuneCountInString(form.Description) > 1024 {
  201. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err")))
  202. return
  203. }
  204. rel, err := models.GetDatasetByID(form.ID)
  205. ctx.Data["dataset"] = rel
  206. if err != nil {
  207. log.Error("failed to query dataset", err)
  208. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.query_dataset_fail")))
  209. return
  210. }
  211. rel.Title = form.Title
  212. rel.Description = form.Description
  213. rel.Category = form.Category
  214. rel.Task = form.Task
  215. rel.License = form.License
  216. if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil {
  217. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.query_dataset_fail")))
  218. }
  219. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  220. }
  221. func DatasetAction(ctx *context.Context) {
  222. var err error
  223. datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  224. switch ctx.Params(":action") {
  225. case "star":
  226. err = models.StarDataset(ctx.User.ID, datasetId, true)
  227. case "unstar":
  228. err = models.StarDataset(ctx.User.ID, datasetId, false)
  229. }
  230. if err != nil {
  231. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("repo.star_fail", ctx.Params(":action"))))
  232. } else {
  233. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  234. }
  235. }
  236. func CurrentRepoDataset(ctx *context.Context) {
  237. page := ctx.QueryInt("page")
  238. cloudbrainType := ctx.QueryInt("type")
  239. repo := ctx.Repo.Repository
  240. var datasetIDs []int64
  241. dataset, err := models.GetDatasetByRepo(repo)
  242. if err != nil {
  243. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("GetDatasetByRepo failed", err)))
  244. }
  245. datasetIDs = append(datasetIDs, dataset.ID)
  246. uploaderID := ctx.User.ID
  247. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  248. ListOptions: models.ListOptions{
  249. Page: page,
  250. PageSize: setting.UI.IssuePagingNum,
  251. },
  252. DatasetIDs: datasetIDs,
  253. UploaderID: uploaderID,
  254. Type: cloudbrainType,
  255. NeedIsPrivate: false,
  256. NeedRepoInfo: true,
  257. })
  258. if err != nil {
  259. ctx.ServerError("datasets", err)
  260. return
  261. }
  262. data, err := json.Marshal(datasets)
  263. if err != nil {
  264. log.Error("json.Marshal failed:", err.Error())
  265. ctx.JSON(200, map[string]string{
  266. "result_code": "-1",
  267. "error_msg": err.Error(),
  268. "data": "",
  269. })
  270. return
  271. }
  272. ctx.JSON(200, map[string]string{
  273. "data": string(data),
  274. "count": strconv.FormatInt(count, 10),
  275. })
  276. }
  277. func MyDataset(ctx *context.Context) {
  278. page := ctx.QueryInt("page")
  279. cloudbrainType := ctx.QueryInt("type")
  280. uploaderID := ctx.User.ID
  281. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  282. ListOptions: models.ListOptions{
  283. Page: page,
  284. PageSize: setting.UI.IssuePagingNum,
  285. },
  286. UploaderID: uploaderID,
  287. Type: cloudbrainType,
  288. NeedIsPrivate: false,
  289. NeedRepoInfo: true,
  290. })
  291. if err != nil {
  292. ctx.ServerError("datasets", err)
  293. return
  294. }
  295. data, err := json.Marshal(datasets)
  296. if err != nil {
  297. log.Error("json.Marshal failed:", err.Error())
  298. ctx.JSON(200, map[string]string{
  299. "result_code": "-1",
  300. "error_msg": err.Error(),
  301. "data": "",
  302. })
  303. return
  304. }
  305. ctx.JSON(200, map[string]string{
  306. "data": string(data),
  307. "count": strconv.FormatInt(count, 10),
  308. })
  309. }
  310. func PublicDataset(ctx *context.Context) {
  311. page := ctx.QueryInt("page")
  312. cloudbrainType := ctx.QueryInt("type")
  313. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  314. ListOptions: models.ListOptions{
  315. Page: page,
  316. PageSize: setting.UI.IssuePagingNum,
  317. },
  318. NeedIsPrivate: true,
  319. IsPrivate: false,
  320. Type: cloudbrainType,
  321. NeedRepoInfo: true,
  322. })
  323. if err != nil {
  324. ctx.ServerError("datasets", err)
  325. return
  326. }
  327. data, err := json.Marshal(datasets)
  328. if err != nil {
  329. log.Error("json.Marshal failed:", err.Error())
  330. ctx.JSON(200, map[string]string{
  331. "result_code": "-1",
  332. "error_msg": err.Error(),
  333. "data": "",
  334. })
  335. return
  336. }
  337. ctx.JSON(200, map[string]string{
  338. "data": string(data),
  339. "count": strconv.FormatInt(count, 10),
  340. })
  341. }
  342. func MyFavoriteDataset(ctx *context.Context) {
  343. page := ctx.QueryInt("page")
  344. cloudbrainType := ctx.QueryInt("type")
  345. var datasetIDs []int64
  346. datasetStars, err := models.GetDatasetStarByUser(ctx.User)
  347. if err != nil {
  348. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("GetDatasetByRepo failed", err)))
  349. }
  350. for i, _ := range datasetStars {
  351. datasetIDs = append(datasetIDs, datasetStars[i].DatasetID)
  352. }
  353. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  354. ListOptions: models.ListOptions{
  355. Page: page,
  356. PageSize: setting.UI.IssuePagingNum,
  357. },
  358. DatasetIDs: datasetIDs,
  359. NeedIsPrivate: true,
  360. IsPrivate: false,
  361. Type: cloudbrainType,
  362. NeedRepoInfo: true,
  363. })
  364. if err != nil {
  365. ctx.ServerError("datasets", err)
  366. return
  367. }
  368. data, err := json.Marshal(datasets)
  369. if err != nil {
  370. log.Error("json.Marshal failed:", err.Error())
  371. ctx.JSON(200, map[string]string{
  372. "result_code": "-1",
  373. "error_msg": err.Error(),
  374. "data": "",
  375. })
  376. return
  377. }
  378. ctx.JSON(200, map[string]string{
  379. "data": string(data),
  380. "count": strconv.FormatInt(count, 10),
  381. })
  382. }