]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/utils/videos/videos.ts
Add ability to limit videos history size
[github/Chocobozzz/PeerTube.git] / shared / utils / videos / videos.ts
index b3d24bc53dd136389c43b3ccbe124f43e920fd56..b5a07b792d55bd93eda3be9dfd9405cb4b819c66 100644 (file)
@@ -1,7 +1,7 @@
 /* tslint:disable:no-unused-expression */
 
 import { expect } from 'chai'
-import { existsSync, readdir, readFile } from 'fs-extra'
+import { pathExists, readdir, readFile } from 'fs-extra'
 import * as parseTorrent from 'parse-torrent'
 import { extname, join } from 'path'
 import * as request from 'supertest'
@@ -16,11 +16,13 @@ import {
   ServerInfo,
   testImage
 } from '../'
-
+import * as validator from 'validator'
 import { VideoDetails, VideoPrivacy } from '../../models/videos'
-import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
+import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, loadLanguages, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
 import { dateIsValid, webtorrentAdd } from '../miscs/miscs'
 
+loadLanguages()
+
 type VideoAttributes = {
   name?: string
   category?: number
@@ -223,6 +225,28 @@ function getVideoChannelVideos (
   })
 }
 
+function getPlaylistVideos (
+  url: string,
+  accessToken: string,
+  playlistId: number | string,
+  start: number,
+  count: number,
+  query: { nsfw?: boolean } = {}
+) {
+  const path = '/api/v1/video-playlists/' + playlistId + '/videos'
+
+  return makeGetRequest({
+    url,
+    path,
+    query: immutableAssign(query, {
+      start,
+      count
+    }),
+    token: accessToken,
+    statusCodeExpected: 200
+  })
+}
+
 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
   const path = '/api/v1/videos'
 
@@ -289,8 +313,8 @@ async function checkVideoFilesWereRemoved (
   for (const directory of directories) {
     const directoryPath = join(root(), testDirectory, directory)
 
-    const directoryExists = existsSync(directoryPath)
-    if (!directoryExists) continue
+    const directoryExists = await pathExists(directoryPath)
+    if (directoryExists === false) continue
 
     const files = await readdir(directoryPath)
     for (const file of files) {
@@ -575,12 +599,29 @@ async function completeVideoCheck (
   }
 }
 
+async function videoUUIDToId (url: string, id: number | string) {
+  if (validator.isUUID('' + id) === false) return id
+
+  const res = await getVideo(url, id)
+  return res.body.id
+}
+
+async function uploadVideoAndGetId (options: { server: ServerInfo, videoName: string, nsfw?: boolean, token?: string }) {
+  const videoAttrs: any = { name: options.videoName }
+  if (options.nsfw) videoAttrs.nsfw = options.nsfw
+
+  const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs)
+
+  return { id: res.body.video.id, uuid: res.body.video.uuid }
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   getVideoDescription,
   getVideoCategories,
   getVideoLicences,
+  videoUUIDToId,
   getVideoPrivacies,
   getVideoLanguages,
   getMyVideos,
@@ -601,5 +642,7 @@ export {
   parseTorrentVideo,
   getLocalVideos,
   completeVideoCheck,
-  checkVideoFilesWereRemoved
+  checkVideoFilesWereRemoved,
+  getPlaylistVideos,
+  uploadVideoAndGetId
 }