]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/videos/videos.ts
chore(refactor): remove shared folder dependencies to the server
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / videos.ts
index 19f0df8b873da4704cb6d447c9c31a4a84de102f..2c3464aa83efc472282737a43ff7f029efde8300 100644 (file)
 
 import { expect } from 'chai'
 import { pathExists, readdir } from 'fs-extra'
-import { join } from 'path'
-import { getLowercaseExtension } from '@server/helpers/core-utils'
-import { HttpStatusCode } from '@shared/core-utils'
-import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
-import { dateIsValid, testImage, webtorrentAdd } from '../miscs'
-import { makeRawRequest } from '../requests/requests'
+import { basename, join } from 'path'
+import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models'
 import { waitJobs } from '../server'
-import { ServerInfo } from '../server/servers'
+import { PeerTubeServer } from '../server/server'
 import { VideoEdit } from './videos-command'
 
-async function checkVideoFilesWereRemoved (
-  videoUUID: string,
-  server: ServerInfo,
-  directories = [
-    'redundancy',
-    'videos',
-    'thumbnails',
-    'torrents',
-    'previews',
-    'captions',
-    join('playlists', 'hls'),
-    join('redundancy', 'hls')
-  ]
-) {
-  for (const directory of directories) {
-    const directoryPath = server.serversCommand.buildDirectory(directory)
+async function checkVideoFilesWereRemoved (options: {
+  server: PeerTubeServer
+  video: VideoDetails
+  captions?: VideoCaption[]
+  onlyVideoFiles?: boolean // default false
+}) {
+  const { video, server, captions = [], onlyVideoFiles = false } = options
+
+  const webtorrentFiles = video.files || []
+  const hlsFiles = video.streamingPlaylists[0]?.files || []
+
+  const thumbnailName = basename(video.thumbnailPath)
+  const previewName = basename(video.previewPath)
+
+  const torrentNames = webtorrentFiles.concat(hlsFiles).map(f => basename(f.torrentUrl))
+
+  const captionNames = captions.map(c => basename(c.captionPath))
+
+  const webtorrentFilenames = webtorrentFiles.map(f => basename(f.fileUrl))
+  const hlsFilenames = hlsFiles.map(f => basename(f.fileUrl))
+
+  let directories: { [ directory: string ]: string[] } = {
+    videos: webtorrentFilenames,
+    redundancy: webtorrentFilenames,
+    [join('playlists', 'hls')]: hlsFilenames,
+    [join('redundancy', 'hls')]: hlsFilenames
+  }
+
+  if (onlyVideoFiles !== true) {
+    directories = {
+      ...directories,
+
+      thumbnails: [ thumbnailName ],
+      previews: [ previewName ],
+      torrents: torrentNames,
+      captions: captionNames
+    }
+  }
+
+  for (const directory of Object.keys(directories)) {
+    const directoryPath = server.servers.buildDirectory(directory)
 
     const directoryExists = await pathExists(directoryPath)
     if (directoryExists === false) continue
 
-    const files = await readdir(directoryPath)
-    for (const file of files) {
-      expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
+    const existingFiles = await readdir(directoryPath)
+    for (const existingFile of existingFiles) {
+      for (const shouldNotExist of directories[directory]) {
+        expect(existingFile, `File ${existingFile} should not exist in ${directoryPath}`).to.not.contain(shouldNotExist)
+      }
     }
   }
 }
 
+async function saveVideoInServers (servers: PeerTubeServer[], uuid: string) {
+  for (const server of servers) {
+    server.store.videoDetails = await server.videos.get({ id: uuid })
+  }
+}
+
 function checkUploadVideoParam (
-  server: ServerInfo,
+  server: PeerTubeServer,
   token: string,
   attributes: Partial<VideoEdit>,
   expectedStatus = HttpStatusCode.OK_200,
   mode: 'legacy' | 'resumable' = 'legacy'
 ) {
   return mode === 'legacy'
-    ? server.videosCommand.buildLegacyUpload({ token, attributes, expectedStatus })
-    : server.videosCommand.buildResumeUpload({ token, attributes, expectedStatus })
-}
-
-async function completeVideoCheck (
-  server: ServerInfo,
-  video: any,
-  attributes: {
-    name: string
-    category: number
-    licence: number
-    language: string
-    nsfw: boolean
-    commentsEnabled: boolean
-    downloadEnabled: boolean
-    description: string
-    publishedAt?: string
-    support: string
-    originallyPublishedAt?: string
-    account: {
-      name: string
-      host: string
-    }
-    isLocal: boolean
-    tags: string[]
-    privacy: number
-    likes?: number
-    dislikes?: number
-    duration: number
-    channel: {
-      displayName: string
-      name: string
-      description
-      isLocal: boolean
-    }
-    fixture: string
-    files: {
-      resolution: number
-      size: number
-    }[]
-    thumbnailfile?: string
-    previewfile?: string
-  }
-) {
-  if (!attributes.likes) attributes.likes = 0
-  if (!attributes.dislikes) attributes.dislikes = 0
-
-  const host = new URL(server.url).host
-  const originHost = attributes.account.host
-
-  expect(video.name).to.equal(attributes.name)
-  expect(video.category.id).to.equal(attributes.category)
-  expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
-  expect(video.licence.id).to.equal(attributes.licence)
-  expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
-  expect(video.language.id).to.equal(attributes.language)
-  expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
-  expect(video.privacy.id).to.deep.equal(attributes.privacy)
-  expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
-  expect(video.nsfw).to.equal(attributes.nsfw)
-  expect(video.description).to.equal(attributes.description)
-  expect(video.account.id).to.be.a('number')
-  expect(video.account.host).to.equal(attributes.account.host)
-  expect(video.account.name).to.equal(attributes.account.name)
-  expect(video.channel.displayName).to.equal(attributes.channel.displayName)
-  expect(video.channel.name).to.equal(attributes.channel.name)
-  expect(video.likes).to.equal(attributes.likes)
-  expect(video.dislikes).to.equal(attributes.dislikes)
-  expect(video.isLocal).to.equal(attributes.isLocal)
-  expect(video.duration).to.equal(attributes.duration)
-  expect(dateIsValid(video.createdAt)).to.be.true
-  expect(dateIsValid(video.publishedAt)).to.be.true
-  expect(dateIsValid(video.updatedAt)).to.be.true
-
-  if (attributes.publishedAt) {
-    expect(video.publishedAt).to.equal(attributes.publishedAt)
-  }
-
-  if (attributes.originallyPublishedAt) {
-    expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
-  } else {
-    expect(video.originallyPublishedAt).to.be.null
-  }
-
-  const videoDetails = await server.videosCommand.get({ id: video.uuid })
-
-  expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
-  expect(videoDetails.tags).to.deep.equal(attributes.tags)
-  expect(videoDetails.account.name).to.equal(attributes.account.name)
-  expect(videoDetails.account.host).to.equal(attributes.account.host)
-  expect(video.channel.displayName).to.equal(attributes.channel.displayName)
-  expect(video.channel.name).to.equal(attributes.channel.name)
-  expect(videoDetails.channel.host).to.equal(attributes.account.host)
-  expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
-  expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
-  expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
-  expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
-  expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
-
-  for (const attributeFile of attributes.files) {
-    const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
-    expect(file).not.to.be.undefined
-
-    let extension = getLowercaseExtension(attributes.fixture)
-    // Transcoding enabled: extension will always be .mp4
-    if (attributes.files.length > 1) extension = '.mp4'
-
-    expect(file.magnetUri).to.have.lengthOf.above(2)
-
-    expect(file.torrentDownloadUrl).to.equal(`http://${host}/download/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
-    expect(file.torrentUrl).to.equal(`http://${host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
-
-    expect(file.fileUrl).to.equal(`http://${originHost}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
-    expect(file.fileDownloadUrl).to.equal(`http://${originHost}/download/videos/${videoDetails.uuid}-${file.resolution.id}${extension}`)
-
-    await Promise.all([
-      makeRawRequest(file.torrentUrl, 200),
-      makeRawRequest(file.torrentDownloadUrl, 200),
-      makeRawRequest(file.metadataUrl, 200),
-      // Backward compatibility
-      makeRawRequest(`http://${originHost}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`, 200)
-    ])
-
-    expect(file.resolution.id).to.equal(attributeFile.resolution)
-    expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
-
-    const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
-    const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
-    expect(
-      file.size,
-      'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
-    ).to.be.above(minSize).and.below(maxSize)
-
-    const torrent = await webtorrentAdd(file.magnetUri, true)
-    expect(torrent.files).to.be.an('array')
-    expect(torrent.files.length).to.equal(1)
-    expect(torrent.files[0].path).to.exist.and.to.not.equal('')
-  }
-
-  expect(videoDetails.thumbnailPath).to.exist
-  await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
-
-  if (attributes.previewfile) {
-    expect(videoDetails.previewPath).to.exist
-    await testImage(server.url, attributes.previewfile, videoDetails.previewPath)
-  }
+    ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
+    : server.videos.buildResumeUpload({ token, attributes, expectedStatus })
 }
 
 // serverNumber starts from 1
 async function uploadRandomVideoOnServers (
-  servers: ServerInfo[],
+  servers: PeerTubeServer[],
   serverNumber: number,
   additionalParams?: VideoEdit & { prefixName?: string }
 ) {
   const server = servers.find(s => s.serverNumber === serverNumber)
-  const res = await server.videosCommand.randomUpload({ wait: false, ...additionalParams })
+  const res = await server.videos.randomUpload({ wait: false, additionalParams })
 
   await waitJobs(servers)
 
@@ -213,7 +98,7 @@ async function uploadRandomVideoOnServers (
 
 export {
   checkUploadVideoParam,
-  completeVideoCheck,
   uploadRandomVideoOnServers,
-  checkVideoFilesWereRemoved
+  checkVideoFilesWereRemoved,
+  saveVideoInServers
 }