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