]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/videos/videos-command.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / videos-command.ts
index f46d386f4f1e75c3e3449b87e7a55f904dcc6d3f..68241f06246ee943a0c9faf14e17a6cc825a86f2 100644 (file)
@@ -3,12 +3,13 @@
 import { expect } from 'chai'
 import { createReadStream, stat } from 'fs-extra'
 import got, { Response as GotResponse } from 'got'
-import { omit, pick } from 'lodash'
+import { omit } from 'lodash'
 import validator from 'validator'
 import { buildUUID } from '@server/helpers/uuid'
 import { loadLanguages } from '@server/initializers/constants'
-import { HttpStatusCode } from '@shared/models'
+import { pick } from '@shared/core-utils'
 import {
+  HttpStatusCode,
   ResultList,
   UserVideoRateType,
   Video,
@@ -17,8 +18,7 @@ import {
   VideoDetails,
   VideoFileMetadata,
   VideoPrivacy,
-  VideosCommonQuery,
-  VideosWithSearchCommonQuery
+  VideosCommonQuery
 } from '@shared/models'
 import { buildAbsoluteFixturePath, wait } from '../miscs'
 import { unwrapBody } from '../requests'
@@ -187,6 +187,17 @@ export class VideosCommand extends AbstractCommand {
     return id
   }
 
+  async listFiles (options: OverrideCommandOptions & {
+    id: number | string
+  }) {
+    const video = await this.get(options)
+
+    const files = video.files || []
+    const hlsFiles = video.streamingPlaylists[0]?.files || []
+
+    return files.concat(hlsFiles)
+  }
+
   // ---------------------------------------------------------------------------
 
   listMyVideos (options: OverrideCommandOptions & {
@@ -195,6 +206,7 @@ export class VideosCommand extends AbstractCommand {
     sort?: string
     search?: string
     isLive?: boolean
+    channelId?: number
   } = {}) {
     const path = '/api/v1/users/me/videos'
 
@@ -202,7 +214,7 @@ export class VideosCommand extends AbstractCommand {
       ...options,
 
       path,
-      query: pick(options, [ 'start', 'count', 'sort', 'search', 'isLive' ]),
+      query: pick(options, [ 'start', 'count', 'sort', 'search', 'isLive', 'channelId' ]),
       implicitToken: true,
       defaultExpectedStatus: HttpStatusCode.OK_200
     })
@@ -233,7 +245,7 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
-  listByAccount (options: OverrideCommandOptions & VideosWithSearchCommonQuery & {
+  listByAccount (options: OverrideCommandOptions & VideosCommonQuery & {
     handle: string
   }) {
     const { handle, search } = options
@@ -249,7 +261,7 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
-  listByChannel (options: OverrideCommandOptions & VideosWithSearchCommonQuery & {
+  listByChannel (options: OverrideCommandOptions & VideosCommonQuery & {
     handle: string
   }) {
     const { handle } = options
@@ -267,6 +279,16 @@ export class VideosCommand extends AbstractCommand {
 
   // ---------------------------------------------------------------------------
 
+  async find (options: OverrideCommandOptions & {
+    name: string
+  }) {
+    const { data } = await this.list(options)
+
+    return data.find(v => v.name === options.name)
+  }
+
+  // ---------------------------------------------------------------------------
+
   update (options: OverrideCommandOptions & {
     id: number | string
     attributes?: VideoEdit
@@ -332,7 +354,7 @@ export class VideosCommand extends AbstractCommand {
     attributes?: VideoEdit
     mode?: 'legacy' | 'resumable' // default legacy
   } = {}) {
-    const { mode = 'legacy', expectedStatus } = options
+    const { mode = 'legacy' } = options
     let defaultChannelId = 1
 
     try {
@@ -360,22 +382,23 @@ export class VideosCommand extends AbstractCommand {
       ...options.attributes
     }
 
-    const res = mode === 'legacy'
+    const created = mode === 'legacy'
       ? await this.buildLegacyUpload({ ...options, attributes })
       : await this.buildResumeUpload({ ...options, attributes })
 
     // Wait torrent generation
+    const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 })
     if (expectedStatus === HttpStatusCode.OK_200) {
       let video: VideoDetails
 
       do {
-        video = await this.getWithToken({ ...options, id: video.uuid })
+        video = await this.getWithToken({ ...options, id: created.uuid })
 
         await wait(50)
       } while (!video.files[0].torrentUrl)
     }
 
-    return res
+    return created
   }
 
   async buildLegacyUpload (options: OverrideCommandOptions & {
@@ -426,6 +449,10 @@ export class VideosCommand extends AbstractCommand {
 
       const result = await this.sendResumableChunks({ ...options, pathUploadId, videoFilePath, size })
 
+      if (result.statusCode === HttpStatusCode.OK_200) {
+        await this.endResumableUpload({ ...options, expectedStatus: HttpStatusCode.NO_CONTENT_204, pathUploadId })
+      }
+
       return result.body?.video || result.body as any
     }
 
@@ -519,6 +546,19 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
+  endResumableUpload (options: OverrideCommandOptions & {
+    pathUploadId: string
+  }) {
+    return this.deleteRequest({
+      ...options,
+
+      path: '/api/v1/videos/upload-resumable',
+      rawQuery: options.pathUploadId,
+      implicitToken: true,
+      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
+    })
+  }
+
   quickUpload (options: OverrideCommandOptions & {
     name: string
     nsfw?: boolean
@@ -535,13 +575,13 @@ export class VideosCommand extends AbstractCommand {
 
   async randomUpload (options: OverrideCommandOptions & {
     wait?: boolean // default true
-    additionalParams?: VideoEdit & { prefixName: string }
+    additionalParams?: VideoEdit & { prefixName?: string }
   } = {}) {
     const { wait = true, additionalParams } = options
     const prefixName = additionalParams?.prefixName || ''
     const name = prefixName + buildUUID()
 
-    const attributes = { name, additionalParams }
+    const attributes = { name, ...additionalParams }
 
     const result = await this.upload({ ...options, attributes })
 
@@ -564,7 +604,8 @@ export class VideosCommand extends AbstractCommand {
       'languageOneOf',
       'tagsOneOf',
       'tagsAllOf',
-      'filter',
+      'isLocal',
+      'include',
       'skipCount'
     ])
   }