]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/videos.ts
Reorganize imports
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / videos.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2
3 import { expect } from 'chai'
4 import { pathExists, readdir } from 'fs-extra'
5 import { join } from 'path'
6 import { getLowercaseExtension } from '@server/helpers/core-utils'
7 import { HttpStatusCode } from '@shared/models'
8 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
9 import { dateIsValid, testImage, webtorrentAdd } from '../miscs'
10 import { makeRawRequest } from '../requests/requests'
11 import { waitJobs } from '../server'
12 import { PeerTubeServer } from '../server/server'
13 import { VideoEdit } from './videos-command'
14
15 async function checkVideoFilesWereRemoved (
16 videoUUID: string,
17 server: PeerTubeServer,
18 directories = [
19 'redundancy',
20 'videos',
21 'thumbnails',
22 'torrents',
23 'previews',
24 'captions',
25 join('playlists', 'hls'),
26 join('redundancy', 'hls')
27 ]
28 ) {
29 for (const directory of directories) {
30 const directoryPath = server.servers.buildDirectory(directory)
31
32 const directoryExists = await pathExists(directoryPath)
33 if (directoryExists === false) continue
34
35 const files = await readdir(directoryPath)
36 for (const file of files) {
37 expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
38 }
39 }
40 }
41
42 function checkUploadVideoParam (
43 server: PeerTubeServer,
44 token: string,
45 attributes: Partial<VideoEdit>,
46 expectedStatus = HttpStatusCode.OK_200,
47 mode: 'legacy' | 'resumable' = 'legacy'
48 ) {
49 return mode === 'legacy'
50 ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
51 : server.videos.buildResumeUpload({ token, attributes, expectedStatus })
52 }
53
54 async function completeVideoCheck (
55 server: PeerTubeServer,
56 video: any,
57 attributes: {
58 name: string
59 category: number
60 licence: number
61 language: string
62 nsfw: boolean
63 commentsEnabled: boolean
64 downloadEnabled: boolean
65 description: string
66 publishedAt?: string
67 support: string
68 originallyPublishedAt?: string
69 account: {
70 name: string
71 host: string
72 }
73 isLocal: boolean
74 tags: string[]
75 privacy: number
76 likes?: number
77 dislikes?: number
78 duration: number
79 channel: {
80 displayName: string
81 name: string
82 description
83 isLocal: boolean
84 }
85 fixture: string
86 files: {
87 resolution: number
88 size: number
89 }[]
90 thumbnailfile?: string
91 previewfile?: string
92 }
93 ) {
94 if (!attributes.likes) attributes.likes = 0
95 if (!attributes.dislikes) attributes.dislikes = 0
96
97 const host = new URL(server.url).host
98 const originHost = attributes.account.host
99
100 expect(video.name).to.equal(attributes.name)
101 expect(video.category.id).to.equal(attributes.category)
102 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
103 expect(video.licence.id).to.equal(attributes.licence)
104 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
105 expect(video.language.id).to.equal(attributes.language)
106 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
107 expect(video.privacy.id).to.deep.equal(attributes.privacy)
108 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
109 expect(video.nsfw).to.equal(attributes.nsfw)
110 expect(video.description).to.equal(attributes.description)
111 expect(video.account.id).to.be.a('number')
112 expect(video.account.host).to.equal(attributes.account.host)
113 expect(video.account.name).to.equal(attributes.account.name)
114 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
115 expect(video.channel.name).to.equal(attributes.channel.name)
116 expect(video.likes).to.equal(attributes.likes)
117 expect(video.dislikes).to.equal(attributes.dislikes)
118 expect(video.isLocal).to.equal(attributes.isLocal)
119 expect(video.duration).to.equal(attributes.duration)
120 expect(dateIsValid(video.createdAt)).to.be.true
121 expect(dateIsValid(video.publishedAt)).to.be.true
122 expect(dateIsValid(video.updatedAt)).to.be.true
123
124 if (attributes.publishedAt) {
125 expect(video.publishedAt).to.equal(attributes.publishedAt)
126 }
127
128 if (attributes.originallyPublishedAt) {
129 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
130 } else {
131 expect(video.originallyPublishedAt).to.be.null
132 }
133
134 const videoDetails = await server.videos.get({ id: video.uuid })
135
136 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
137 expect(videoDetails.tags).to.deep.equal(attributes.tags)
138 expect(videoDetails.account.name).to.equal(attributes.account.name)
139 expect(videoDetails.account.host).to.equal(attributes.account.host)
140 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
141 expect(video.channel.name).to.equal(attributes.channel.name)
142 expect(videoDetails.channel.host).to.equal(attributes.account.host)
143 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
144 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
145 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
146 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
147 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
148
149 for (const attributeFile of attributes.files) {
150 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
151 expect(file).not.to.be.undefined
152
153 let extension = getLowercaseExtension(attributes.fixture)
154 // Transcoding enabled: extension will always be .mp4
155 if (attributes.files.length > 1) extension = '.mp4'
156
157 expect(file.magnetUri).to.have.lengthOf.above(2)
158
159 expect(file.torrentDownloadUrl).to.equal(`http://${host}/download/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
160 expect(file.torrentUrl).to.equal(`http://${host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
161
162 expect(file.fileUrl).to.equal(`http://${originHost}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
163 expect(file.fileDownloadUrl).to.equal(`http://${originHost}/download/videos/${videoDetails.uuid}-${file.resolution.id}${extension}`)
164
165 await Promise.all([
166 makeRawRequest(file.torrentUrl, 200),
167 makeRawRequest(file.torrentDownloadUrl, 200),
168 makeRawRequest(file.metadataUrl, 200),
169 // Backward compatibility
170 makeRawRequest(`http://${originHost}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`, 200)
171 ])
172
173 expect(file.resolution.id).to.equal(attributeFile.resolution)
174 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
175
176 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
177 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
178 expect(
179 file.size,
180 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
181 ).to.be.above(minSize).and.below(maxSize)
182
183 const torrent = await webtorrentAdd(file.magnetUri, true)
184 expect(torrent.files).to.be.an('array')
185 expect(torrent.files.length).to.equal(1)
186 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
187 }
188
189 expect(videoDetails.thumbnailPath).to.exist
190 await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
191
192 if (attributes.previewfile) {
193 expect(videoDetails.previewPath).to.exist
194 await testImage(server.url, attributes.previewfile, videoDetails.previewPath)
195 }
196 }
197
198 // serverNumber starts from 1
199 async function uploadRandomVideoOnServers (
200 servers: PeerTubeServer[],
201 serverNumber: number,
202 additionalParams?: VideoEdit & { prefixName?: string }
203 ) {
204 const server = servers.find(s => s.serverNumber === serverNumber)
205 const res = await server.videos.randomUpload({ wait: false, additionalParams })
206
207 await waitJobs(servers)
208
209 return res
210 }
211
212 // ---------------------------------------------------------------------------
213
214 export {
215 checkUploadVideoParam,
216 completeVideoCheck,
217 uploadRandomVideoOnServers,
218 checkVideoFilesWereRemoved
219 }