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