Преглед изворни кода

feat(controllers): 在 CollectionsController 和 PlayHistoriesController 中添加 Drive 签名 URL 处理逻辑

wuyi пре 5 месеци
родитељ
комит
098c13b11d

+ 19 - 0
app/Controllers/Http/CollectionsController.ts

@@ -4,6 +4,7 @@ import PaginationService from 'App/Services/PaginationService'
 import { schema } from '@ioc:Adonis/Core/Validator'
 import Episode from 'App/Models/Episode'
 import Series from 'App/Models/Series'
+import Drive from '@ioc:Adonis/Core/Drive'
 
 export default class CollectionsController {
     private paginationService = new PaginationService(Collection)
@@ -19,6 +20,24 @@ export default class CollectionsController {
                 .filter((id) => !!id)
         )
         const series = await Series.findMany(Array.from(seriesIds))
+        await Promise.all(
+            series.map(async (s) => {
+                try {
+                    if (s.cover) {
+                        const url = new URL(s.cover)
+                        const filePath = url.pathname.replace(/^\//, '')
+                        s.cover = await Drive.getSignedUrl(filePath)
+                    }
+                } catch (e) {}
+                try {
+                    if (s.landscapeCover) {
+                        const url = new URL(s.landscapeCover)
+                        const filePath = url.pathname.replace(/^\//, '')
+                        s.landscapeCover = await Drive.getSignedUrl(filePath)
+                    }
+                } catch (e) {}
+            })
+        )
         const episodeIds = new Set(
             page
                 .all()

+ 19 - 0
app/Controllers/Http/PlayHistoriesController.ts

@@ -4,6 +4,7 @@ import PaginationService from 'App/Services/PaginationService'
 import { schema } from '@ioc:Adonis/Core/Validator'
 import Series from 'App/Models/Series'
 import Episode from 'App/Models/Episode'
+import Drive from '@ioc:Adonis/Core/Drive'
 
 export default class PlayHistoriesController {
     private paginationService = new PaginationService(PlayHistory)
@@ -21,6 +22,24 @@ export default class PlayHistoriesController {
                 .filter((id) => !!id)
         )
         const series = await Series.findMany(Array.from(seriesIds))
+        await Promise.all(
+            series.map(async (s) => {
+                try {
+                    if (s.cover) {
+                        const url = new URL(s.cover)
+                        const filePath = url.pathname.replace(/^\//, '')
+                        s.cover = await Drive.getSignedUrl(filePath)
+                    }
+                } catch (e) {}
+                try {
+                    if (s.landscapeCover) {
+                        const url = new URL(s.landscapeCover)
+                        const filePath = url.pathname.replace(/^\//, '')
+                        s.landscapeCover = await Drive.getSignedUrl(filePath)
+                    }
+                } catch (e) {}
+            })
+        )
         const episodeIds = new Set(
             page
                 .all()