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 13 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
3 years ago
5 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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. package repo
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  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. // 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. ctx.Data["PageIsDataset"] = true
  94. repo := ctx.Repo.Repository
  95. dataset, err := models.GetDatasetByRepo(repo)
  96. ctx.Data["CanWrite"] = ctx.Repo.CanWrite(models.UnitTypeDatasets)
  97. if err != nil {
  98. log.Warn("query dataset, not found.")
  99. ctx.HTML(200, tplIndex)
  100. return
  101. }
  102. cloudbrainType := -1
  103. if ctx.Query("type") != "" {
  104. cloudbrainType = ctx.QueryInt("type")
  105. }
  106. err = models.GetDatasetAttachments(cloudbrainType, ctx.IsSigned, ctx.User, dataset)
  107. if err != nil {
  108. ctx.ServerError("GetDatasetAttachments", err)
  109. return
  110. }
  111. attachments := newFilterPrivateAttachments(ctx, dataset.Attachments, repo)
  112. sort.Slice(attachments, func(i, j int) bool {
  113. return attachments[i].CreatedUnix > attachments[j].CreatedUnix
  114. })
  115. page := ctx.QueryInt("page")
  116. if page <= 0 {
  117. page = 1
  118. }
  119. pagesize := ctx.QueryInt("pagesize")
  120. if pagesize <= 0 {
  121. pagesize = 10
  122. }
  123. pager := context.NewPagination(len(attachments), pagesize, page, 5)
  124. pageAttachments := getPageAttachments(attachments, page, pagesize)
  125. //load attachment creator
  126. for _, attachment := range pageAttachments {
  127. uploader, _ := models.GetUserByID(attachment.UploaderID)
  128. attachment.Uploader = uploader
  129. }
  130. ctx.Data["Page"] = pager
  131. ctx.Data["Title"] = ctx.Tr("dataset.show_dataset")
  132. ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets"
  133. ctx.Data["dataset"] = dataset
  134. ctx.Data["Attachments"] = pageAttachments
  135. ctx.Data["IsOwner"] = true
  136. ctx.Data["StoreType"] = setting.Attachment.StoreType
  137. ctx.Data["Type"] = cloudbrainType
  138. renderAttachmentSettings(ctx)
  139. ctx.HTML(200, tplIndex)
  140. }
  141. func getPageAttachments(attachments []*models.Attachment, page int, pagesize int) []*models.Attachment {
  142. begin := (page - 1) * pagesize
  143. end := (page) * pagesize
  144. if begin > len(attachments)-1 {
  145. return nil
  146. }
  147. if end > len(attachments)-1 {
  148. return attachments[begin:]
  149. } else {
  150. return attachments[begin:end]
  151. }
  152. }
  153. func CreateDataset(ctx *context.Context) {
  154. MustEnableDataset(ctx)
  155. ctx.Data["PageIsDataset"] = true
  156. ctx.HTML(200, tplDatasetCreate)
  157. }
  158. func EditDataset(ctx *context.Context) {
  159. MustEnableDataset(ctx)
  160. ctx.Data["PageIsDataset"] = true
  161. datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  162. dataset, _ := models.GetDatasetByID(datasetId)
  163. if dataset == nil {
  164. ctx.Error(http.StatusNotFound, "")
  165. return
  166. }
  167. ctx.Data["Dataset"] = dataset
  168. ctx.HTML(200, tplDatasetEdit)
  169. }
  170. func CreateDatasetPost(ctx *context.Context, form auth.CreateDatasetForm) {
  171. dataset := &models.Dataset{}
  172. if !NamePattern.MatchString(form.Title) {
  173. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err")))
  174. return
  175. }
  176. if utf8.RuneCountInString(form.Description) > 1024 {
  177. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err", 1024)))
  178. return
  179. }
  180. dataset.RepoID = ctx.Repo.Repository.ID
  181. dataset.UserID = ctx.User.ID
  182. dataset.Category = form.Category
  183. dataset.Task = form.Task
  184. dataset.Title = form.Title
  185. dataset.License = form.License
  186. dataset.Description = form.Description
  187. dataset.DownloadTimes = 0
  188. if ctx.Repo.Repository.IsPrivate {
  189. dataset.Status = 0
  190. } else {
  191. dataset.Status = 1
  192. }
  193. err := models.CreateDataset(dataset)
  194. if err != nil {
  195. log.Error("fail to create dataset", err)
  196. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.create_dataset_fail")))
  197. } else {
  198. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  199. }
  200. }
  201. func EditDatasetPost(ctx *context.Context, form auth.EditDatasetForm) {
  202. ctx.Data["PageIsDataset"] = true
  203. ctx.Data["Title"] = ctx.Tr("dataset.edit_dataset")
  204. if !NamePattern.MatchString(form.Title) {
  205. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.title_format_err")))
  206. return
  207. }
  208. if utf8.RuneCountInString(form.Description) > 1024 {
  209. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.description_format_err", 1024)))
  210. return
  211. }
  212. rel, err := models.GetDatasetByID(form.ID)
  213. ctx.Data["dataset"] = rel
  214. if err != nil {
  215. log.Error("failed to query dataset", err)
  216. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.query_dataset_fail")))
  217. return
  218. }
  219. rel.Title = form.Title
  220. rel.Description = form.Description
  221. rel.Category = form.Category
  222. rel.Task = form.Task
  223. rel.License = form.License
  224. if err = models.UpdateDataset(models.DefaultDBContext(), rel); err != nil {
  225. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("dataset.query_dataset_fail")))
  226. }
  227. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  228. }
  229. func DatasetAction(ctx *context.Context) {
  230. var err error
  231. datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  232. switch ctx.Params(":action") {
  233. case "star":
  234. err = models.StarDataset(ctx.User.ID, datasetId, true)
  235. case "unstar":
  236. err = models.StarDataset(ctx.User.ID, datasetId, false)
  237. }
  238. if err != nil {
  239. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("repo.star_fail", ctx.Params(":action"))))
  240. } else {
  241. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  242. }
  243. }
  244. func CurrentRepoDataset(ctx *context.Context) {
  245. page := ctx.QueryInt("page")
  246. cloudbrainType := ctx.QueryInt("type")
  247. keyword := strings.Trim(ctx.Query("q"), " ")
  248. repo := ctx.Repo.Repository
  249. var datasetIDs []int64
  250. dataset, err := models.GetDatasetByRepo(repo)
  251. if err != nil {
  252. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("GetDatasetByRepo failed", err)))
  253. return
  254. }
  255. datasetIDs = append(datasetIDs, dataset.ID)
  256. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  257. ListOptions: models.ListOptions{
  258. Page: page,
  259. PageSize: setting.UI.DatasetPagingNum,
  260. },
  261. Keyword: keyword,
  262. NeedDatasetIDs: true,
  263. DatasetIDs: datasetIDs,
  264. Type: cloudbrainType,
  265. NeedIsPrivate: false,
  266. JustNeedZipFile: true,
  267. NeedRepoInfo: true,
  268. })
  269. if err != nil {
  270. ctx.ServerError("datasets", err)
  271. return
  272. }
  273. data, err := json.Marshal(datasets)
  274. if err != nil {
  275. log.Error("json.Marshal failed:", err.Error())
  276. ctx.JSON(200, map[string]string{
  277. "result_code": "-1",
  278. "error_msg": err.Error(),
  279. "data": "",
  280. })
  281. return
  282. }
  283. ctx.JSON(200, map[string]string{
  284. "result_code": "0",
  285. "data": string(data),
  286. "count": strconv.FormatInt(count, 10),
  287. })
  288. }
  289. func MyDatasets(ctx *context.Context) {
  290. page := ctx.QueryInt("page")
  291. cloudbrainType := ctx.QueryInt("type")
  292. keyword := strings.Trim(ctx.Query("q"), " ")
  293. uploaderID := ctx.User.ID
  294. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  295. ListOptions: models.ListOptions{
  296. Page: page,
  297. PageSize: setting.UI.DatasetPagingNum,
  298. },
  299. Keyword: keyword,
  300. NeedDatasetIDs: false,
  301. UploaderID: uploaderID,
  302. Type: cloudbrainType,
  303. NeedIsPrivate: false,
  304. JustNeedZipFile: true,
  305. NeedRepoInfo: true,
  306. RecommendOnly: ctx.QueryBool("recommend"),
  307. })
  308. if err != nil {
  309. ctx.ServerError("datasets", err)
  310. return
  311. }
  312. data, err := json.Marshal(datasets)
  313. if err != nil {
  314. log.Error("json.Marshal failed:", err.Error())
  315. ctx.JSON(200, map[string]string{
  316. "result_code": "-1",
  317. "error_msg": err.Error(),
  318. "data": "",
  319. })
  320. return
  321. }
  322. ctx.JSON(200, map[string]string{
  323. "result_code": "0",
  324. "data": string(data),
  325. "count": strconv.FormatInt(count, 10),
  326. })
  327. }
  328. func PublicDataset(ctx *context.Context) {
  329. page := ctx.QueryInt("page")
  330. cloudbrainType := ctx.QueryInt("type")
  331. keyword := strings.Trim(ctx.Query("q"), " ")
  332. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  333. ListOptions: models.ListOptions{
  334. Page: page,
  335. PageSize: setting.UI.DatasetPagingNum,
  336. },
  337. Keyword: keyword,
  338. NeedDatasetIDs: false,
  339. NeedIsPrivate: true,
  340. IsPrivate: false,
  341. Type: cloudbrainType,
  342. JustNeedZipFile: true,
  343. NeedRepoInfo: true,
  344. RecommendOnly: ctx.QueryBool("recommend"),
  345. })
  346. if err != nil {
  347. ctx.ServerError("datasets", err)
  348. return
  349. }
  350. data, err := json.Marshal(datasets)
  351. if err != nil {
  352. log.Error("json.Marshal failed:", err.Error())
  353. ctx.JSON(200, map[string]string{
  354. "result_code": "-1",
  355. "error_msg": err.Error(),
  356. "data": "",
  357. })
  358. return
  359. }
  360. ctx.JSON(200, map[string]string{
  361. "result_code": "0",
  362. "data": string(data),
  363. "count": strconv.FormatInt(count, 10),
  364. })
  365. }
  366. func MyFavoriteDataset(ctx *context.Context) {
  367. page := ctx.QueryInt("page")
  368. cloudbrainType := ctx.QueryInt("type")
  369. keyword := strings.Trim(ctx.Query("q"), " ")
  370. var datasetIDs []int64
  371. datasetStars, err := models.GetDatasetStarByUser(ctx.User)
  372. if err != nil {
  373. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("GetDatasetStarByUser failed", err)))
  374. log.Error("GetDatasetStarByUser failed:", err.Error())
  375. ctx.JSON(200, map[string]string{
  376. "result_code": "-1",
  377. "error_msg": err.Error(),
  378. "data": "",
  379. })
  380. return
  381. }
  382. for i, _ := range datasetStars {
  383. datasetIDs = append(datasetIDs, datasetStars[i].DatasetID)
  384. }
  385. datasets, count, err := models.Attachments(&models.AttachmentsOptions{
  386. ListOptions: models.ListOptions{
  387. Page: page,
  388. PageSize: setting.UI.DatasetPagingNum,
  389. },
  390. Keyword: keyword,
  391. NeedDatasetIDs: true,
  392. DatasetIDs: datasetIDs,
  393. NeedIsPrivate: true,
  394. IsPrivate: false,
  395. Type: cloudbrainType,
  396. JustNeedZipFile: true,
  397. NeedRepoInfo: true,
  398. RecommendOnly: ctx.QueryBool("recommend"),
  399. })
  400. if err != nil {
  401. ctx.ServerError("datasets", err)
  402. return
  403. }
  404. data, err := json.Marshal(datasets)
  405. if err != nil {
  406. log.Error("json.Marshal failed:", err.Error())
  407. ctx.JSON(200, map[string]string{
  408. "result_code": "-1",
  409. "error_msg": err.Error(),
  410. "data": "",
  411. })
  412. return
  413. }
  414. ctx.JSON(200, map[string]string{
  415. "result_code": "0",
  416. "data": string(data),
  417. "count": strconv.FormatInt(count, 10),
  418. })
  419. }
  420. func GetDatasetStatus(ctx *context.Context) {
  421. var (
  422. err error
  423. )
  424. UUID := ctx.Params(":uuid")
  425. attachment, err := models.GetAttachmentByUUID(UUID)
  426. if err != nil {
  427. log.Error("GetDatasetStarByUser failed:", err.Error())
  428. ctx.JSON(200, map[string]string{
  429. "result_code": "-1",
  430. "error_msg": err.Error(),
  431. "data": "",
  432. })
  433. return
  434. }
  435. ctx.JSON(200, map[string]string{
  436. "result_code": "0",
  437. "UUID": UUID,
  438. "AttachmentStatus": fmt.Sprint(attachment.DecompressState),
  439. })
  440. }