EpisodesController.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import Episode from 'App/Models/Episode'
  3. import PaginationService from 'App/Services/PaginationService'
  4. import { schema } from '@ioc:Adonis/Core/Validator'
  5. import Decimal from 'decimal.js'
  6. import Order, { OrderType } from 'App/Models/Order'
  7. import Drive from '@ioc:Adonis/Core/Drive'
  8. import Membership from 'App/Models/Membership'
  9. export default class EpisodesController {
  10. private paginationService = new PaginationService(Episode)
  11. public async index({ request }: HttpContextContract) {
  12. return await this.paginationService.paginate(request.all())
  13. }
  14. public async store({ request, bouncer }: HttpContextContract) {
  15. await bouncer.authorize('admin')
  16. const data: any = await request.validate({
  17. schema: schema.create({
  18. seriesId: schema.number(),
  19. title: schema.string.optional(),
  20. episodeNum: schema.number(),
  21. description: schema.string.optional(),
  22. cover: schema.string.optional(),
  23. video: schema.string.optional(),
  24. playCount: schema.number.optional(),
  25. price: schema.string.optional()
  26. })
  27. })
  28. if (data.price) {
  29. data.price = new Decimal(data.price)
  30. }
  31. return await Episode.create(data)
  32. }
  33. public async show({ params, auth }: HttpContextContract) {
  34. const episode = await Episode.findOrFail(params.id)
  35. if (episode.price.eq(0)) {
  36. episode.purchased = true
  37. } else if (auth.user) {
  38. const member = await Membership.query()
  39. .where('userId', auth.user.id)
  40. .where('expireAt', '>', new Date())
  41. .first()
  42. if (member) {
  43. episode.purchased = true
  44. } else {
  45. let order = await Order.query()
  46. .where('userId', auth.user.id)
  47. .where('type', OrderType.Series)
  48. .where('seriesId', episode.seriesId)
  49. .first()
  50. if (!order) {
  51. order = await Order.query()
  52. .where('userId', auth.user.id)
  53. .where('type', OrderType.Episode)
  54. .where('episodeId', episode.id)
  55. .first()
  56. }
  57. if (order) {
  58. episode.purchased = true
  59. }
  60. }
  61. }
  62. if (episode.purchased) {
  63. let url = episode.video!
  64. if (/\.m3u8$/.test(url)) {
  65. episode.signedUrl = url
  66. } else {
  67. if (/^http/.test(url)) {
  68. url = new URL(url).pathname
  69. }
  70. url = url.replace(/^\//, '')
  71. episode.signedUrl = await Drive.getSignedUrl(url.replace(/^\//, ''), {
  72. expiresIn: '1h'
  73. })
  74. }
  75. }
  76. return episode
  77. }
  78. public async update({ request, params, bouncer }: HttpContextContract) {
  79. await bouncer.authorize('admin')
  80. const data: any = await request.validate({
  81. schema: schema.create({
  82. seriesId: schema.number.optional(),
  83. title: schema.string.optional(),
  84. episodeNum: schema.number.optional(),
  85. description: schema.string.optional(),
  86. cover: schema.string.optional(),
  87. video: schema.string.optional(),
  88. playCount: schema.number.optional(),
  89. price: schema.number.optional()
  90. })
  91. })
  92. if (data.price) {
  93. data.price = new Decimal(data.price)
  94. }
  95. const episode = await Episode.findOrFail(params.id)
  96. episode.merge(data)
  97. return await episode.save()
  98. }
  99. public async destroy({ params, bouncer }: HttpContextContract) {
  100. await bouncer.authorize('admin')
  101. const episode = await Episode.findOrFail(params.id)
  102. await episode.delete()
  103. }
  104. }