diff options
author | Chocobozzz <me@florianbigard.com> | 2021-12-16 16:49:43 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-12-16 16:49:43 +0100 |
commit | 2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481 (patch) | |
tree | 9003b67f56f13267aa373e0d0314c9893ad38fb6 | |
parent | 2b6af10e9f37ccd104a9cc179e32c530a6655964 (diff) | |
download | PeerTube-2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481.tar.gz PeerTube-2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481.tar.zst PeerTube-2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481.zip |
Add videos.getFiles plugin helper
-rw-r--r-- | server/lib/plugins/plugin-helpers-builder.ts | 55 | ||||
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test-four/main.js | 7 | ||||
-rw-r--r-- | server/tests/plugins/plugin-helpers.ts | 52 | ||||
-rw-r--r-- | server/types/plugins/register-server-option.model.ts | 28 |
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' | |||
9 | import { getServerActor } from '@server/models/application/application' | 9 | import { getServerActor } from '@server/models/application/application' |
10 | import { ServerModel } from '@server/models/server/server' | 10 | import { ServerModel } from '@server/models/server/server' |
11 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' | 11 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' |
12 | import { UserModel } from '@server/models/user/user' | ||
12 | import { VideoModel } from '@server/models/video/video' | 13 | import { VideoModel } from '@server/models/video/video' |
13 | import { VideoBlacklistModel } from '@server/models/video/video-blacklist' | 14 | import { VideoBlacklistModel } from '@server/models/video/video-blacklist' |
14 | import { MPlugin } from '@server/types/models' | 15 | import { MPlugin } from '@server/types/models' |
15 | import { PeerTubeHelpers } from '@server/types/plugins' | 16 | import { PeerTubeHelpers } from '@server/types/plugins' |
16 | import { VideoBlacklistCreate } from '@shared/models' | 17 | import { VideoBlacklistCreate, VideoStorage } from '@shared/models' |
17 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' | 18 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' |
18 | import { ServerConfigManager } from '../server-config-manager' | 19 | import { ServerConfigManager } from '../server-config-manager' |
19 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' | 20 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' |
20 | import { UserModel } from '@server/models/user/user' | 21 | import { VideoPathManager } from '../video-path-manager' |
21 | 22 | ||
22 | function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { | 23 | function 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 | ||
3 | import 'mocha' | 3 | import 'mocha' |
4 | import { expect } from 'chai' | 4 | import { expect } from 'chai' |
5 | import { pathExists } from 'fs-extra' | ||
5 | import { | 6 | import { |
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' |
17 | import { HttpStatusCode } from '@shared/models' | 19 | import { HttpStatusCode, ThumbnailType } from '@shared/models' |
18 | 20 | ||
19 | function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) { | 21 | function 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: { |