aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-16 16:49:43 +0100
committerChocobozzz <me@florianbigard.com>2021-12-16 16:49:43 +0100
commit2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481 (patch)
tree9003b67f56f13267aa373e0d0314c9893ad38fb6 /server
parent2b6af10e9f37ccd104a9cc179e32c530a6655964 (diff)
downloadPeerTube-2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481.tar.gz
PeerTube-2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481.tar.zst
PeerTube-2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481.zip
Add videos.getFiles plugin helper
Diffstat (limited to 'server')
-rw-r--r--server/lib/plugins/plugin-helpers-builder.ts55
-rw-r--r--server/tests/fixtures/peertube-plugin-test-four/main.js7
-rw-r--r--server/tests/plugins/plugin-helpers.ts52
-rw-r--r--server/types/plugins/register-server-option.model.ts28
4 files changed, 139 insertions, 3 deletions
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts
index e26776f45..bea0f8959 100644
--- a/server/lib/plugins/plugin-helpers-builder.ts
+++ b/server/lib/plugins/plugin-helpers-builder.ts
@@ -9,15 +9,16 @@ import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
9import { getServerActor } from '@server/models/application/application' 9import { getServerActor } from '@server/models/application/application'
10import { ServerModel } from '@server/models/server/server' 10import { ServerModel } from '@server/models/server/server'
11import { ServerBlocklistModel } from '@server/models/server/server-blocklist' 11import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
12import { UserModel } from '@server/models/user/user'
12import { VideoModel } from '@server/models/video/video' 13import { VideoModel } from '@server/models/video/video'
13import { VideoBlacklistModel } from '@server/models/video/video-blacklist' 14import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
14import { MPlugin } from '@server/types/models' 15import { MPlugin } from '@server/types/models'
15import { PeerTubeHelpers } from '@server/types/plugins' 16import { PeerTubeHelpers } from '@server/types/plugins'
16import { VideoBlacklistCreate } from '@shared/models' 17import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
17import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' 18import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
18import { ServerConfigManager } from '../server-config-manager' 19import { ServerConfigManager } from '../server-config-manager'
19import { blacklistVideo, unblacklistVideo } from '../video-blacklist' 20import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
20import { UserModel } from '@server/models/user/user' 21import { VideoPathManager } from '../video-path-manager'
21 22
22function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { 23function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
23 const logger = buildPluginLogger(npmName) 24 const logger = buildPluginLogger(npmName)
@@ -85,6 +86,56 @@ function buildVideosHelpers () {
85 86
86 await video.destroy({ transaction: t }) 87 await video.destroy({ transaction: t })
87 }) 88 })
89 },
90
91 getFiles: async (id: number | string) => {
92 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
93 if (!video) return undefined
94
95 const webtorrentVideoFiles = (video.VideoFiles || []).map(f => ({
96 path: f.storage === VideoStorage.FILE_SYSTEM
97 ? VideoPathManager.Instance.getFSVideoFileOutputPath(video, f)
98 : null,
99 url: f.getFileUrl(video),
100
101 resolution: f.resolution,
102 size: f.size,
103 fps: f.fps
104 }))
105
106 const hls = video.getHLSPlaylist()
107
108 const hlsVideoFiles = hls
109 ? (video.getHLSPlaylist().VideoFiles || []).map(f => {
110 return {
111 path: f.storage === VideoStorage.FILE_SYSTEM
112 ? VideoPathManager.Instance.getFSVideoFileOutputPath(hls, f)
113 : null,
114 url: f.getFileUrl(video),
115 resolution: f.resolution,
116 size: f.size,
117 fps: f.fps
118 }
119 })
120 : []
121
122 const thumbnails = video.Thumbnails.map(t => ({
123 type: t.type,
124 url: t.getFileUrl(video),
125 path: t.getPath()
126 }))
127
128 return {
129 webtorrent: {
130 videoFiles: webtorrentVideoFiles
131 },
132
133 hls: {
134 videoFiles: hlsVideoFiles
135 },
136
137 thumbnails
138 }
88 } 139 }
89 } 140 }
90} 141}
diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js
index b9b207b81..edbb883e2 100644
--- a/server/tests/fixtures/peertube-plugin-test-four/main.js
+++ b/server/tests/fixtures/peertube-plugin-test-four/main.js
@@ -104,6 +104,13 @@ async function register ({
104 isUser 104 isUser
105 }) 105 })
106 }) 106 })
107
108 router.get('/video-files/:id', async (req, res) => {
109 const details = await peertubeHelpers.videos.getFiles(req.params.id)
110 if (!details) return res.sendStatus(404)
111
112 return res.json(details)
113 })
107 } 114 }
108 115
109} 116}
diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts
index 5d16b28a4..26f66b0b1 100644
--- a/server/tests/plugins/plugin-helpers.ts
+++ b/server/tests/plugins/plugin-helpers.ts
@@ -2,6 +2,7 @@
2 2
3import 'mocha' 3import 'mocha'
4import { expect } from 'chai' 4import { expect } from 'chai'
5import { pathExists } from 'fs-extra'
5import { 6import {
6 checkVideoFilesWereRemoved, 7 checkVideoFilesWereRemoved,
7 cleanupTests, 8 cleanupTests,
@@ -9,12 +10,13 @@ import {
9 doubleFollow, 10 doubleFollow,
10 makeGetRequest, 11 makeGetRequest,
11 makePostBodyRequest, 12 makePostBodyRequest,
13 makeRawRequest,
12 PeerTubeServer, 14 PeerTubeServer,
13 PluginsCommand, 15 PluginsCommand,
14 setAccessTokensToServers, 16 setAccessTokensToServers,
15 waitJobs 17 waitJobs
16} from '@shared/extra-utils' 18} from '@shared/extra-utils'
17import { HttpStatusCode } from '@shared/models' 19import { HttpStatusCode, ThumbnailType } from '@shared/models'
18 20
19function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) { 21function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) {
20 const body = { command } 22 const body = { command }
@@ -224,8 +226,56 @@ describe('Test plugin helpers', function () {
224 let videoUUID: string 226 let videoUUID: string
225 227
226 before(async () => { 228 before(async () => {
229 this.timeout(240000)
230
231 await servers[0].config.enableTranscoding()
232
227 const res = await servers[0].videos.quickUpload({ name: 'video1' }) 233 const res = await servers[0].videos.quickUpload({ name: 'video1' })
228 videoUUID = res.uuid 234 videoUUID = res.uuid
235
236 await waitJobs(servers)
237 })
238
239 it('Should get video files', async function () {
240 const { body } = await makeGetRequest({
241 url: servers[0].url,
242 path: '/plugins/test-four/router/video-files/' + videoUUID,
243 expectedStatus: HttpStatusCode.OK_200
244 })
245
246 // Video files check
247 {
248 expect(body.webtorrent.videoFiles).to.be.an('array')
249 expect(body.hls.videoFiles).to.be.an('array')
250
251 for (const resolution of [ 144, 240, 360, 480, 720 ]) {
252 for (const files of [ body.webtorrent.videoFiles, body.hls.videoFiles ]) {
253 const file = files.find(f => f.resolution === resolution)
254 expect(file).to.exist
255
256 expect(file.size).to.be.a('number')
257 expect(file.fps).to.equal(25)
258
259 expect(await pathExists(file.path)).to.be.true
260 await makeRawRequest(file.url, HttpStatusCode.OK_200)
261 }
262 }
263 }
264
265 // Thumbnails check
266 {
267 expect(body.thumbnails).to.be.an('array')
268
269 const miniature = body.thumbnails.find(t => t.type === ThumbnailType.MINIATURE)
270 expect(miniature).to.exist
271 expect(await pathExists(miniature.path)).to.be.true
272 await makeRawRequest(miniature.url, HttpStatusCode.OK_200)
273
274 const preview = body.thumbnails.find(t => t.type === ThumbnailType.PREVIEW)
275 expect(preview).to.exist
276 expect(await pathExists(preview.path)).to.be.true
277 await makeRawRequest(preview.url, HttpStatusCode.OK_200)
278 }
229 }) 279 })
230 280
231 it('Should remove a video after a view', async function () { 281 it('Should remove a video after a view', async function () {
diff --git a/server/types/plugins/register-server-option.model.ts b/server/types/plugins/register-server-option.model.ts
index 8774bcd8c..473990eb6 100644
--- a/server/types/plugins/register-server-option.model.ts
+++ b/server/types/plugins/register-server-option.model.ts
@@ -13,6 +13,7 @@ import {
13 RegisterServerHookOptions, 13 RegisterServerHookOptions,
14 RegisterServerSettingOptions, 14 RegisterServerSettingOptions,
15 ServerConfig, 15 ServerConfig,
16 ThumbnailType,
16 UserRole, 17 UserRole,
17 VideoBlacklistCreate 18 VideoBlacklistCreate
18} from '@shared/models' 19} from '@shared/models'
@@ -35,6 +36,33 @@ export type PeerTubeHelpers = {
35 loadByIdOrUUID: (id: number | string) => Promise<MVideoThumbnail> 36 loadByIdOrUUID: (id: number | string) => Promise<MVideoThumbnail>
36 37
37 removeVideo: (videoId: number) => Promise<void> 38 removeVideo: (videoId: number) => Promise<void>
39
40 getFiles: (id: number | string) => Promise<{
41 webtorrent: {
42 videoFiles: {
43 path: string // Could be null if using remote storage
44 url: string
45 resolution: number
46 size: number
47 fps: number
48 }[]
49 }
50
51 hls: {
52 videoFiles: {
53 path: string // Could be null if using remote storage
54 url: string
55 resolution: number
56 size: number
57 fps: number
58 }[]
59 }
60
61 thumbnails: {
62 type: ThumbnailType
63 path: string
64 }[]
65 }>
38 } 66 }
39 67
40 config: { 68 config: {