PeerTube/shared/extra-utils/videos/video-captions.ts

73 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-11-19 10:08:18 -06:00
import { makeDeleteRequest, makeGetRequest, makeUploadRequest } from '../requests/requests'
2018-07-12 12:02:00 -05:00
import * as request from 'supertest'
import * as chai from 'chai'
2018-11-19 10:08:18 -06:00
import { buildAbsoluteFixturePath } from '../miscs/miscs'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
2018-07-12 12:02:00 -05:00
const expect = chai.expect
function createVideoCaption (args: {
2020-01-31 09:56:52 -06:00
url: string
2018-07-12 12:02:00 -05:00
accessToken: string
videoId: string | number
language: string
2020-01-31 09:56:52 -06:00
fixture: string
mimeType?: string
2018-08-06 04:45:24 -05:00
statusCodeExpected?: number
2018-07-12 12:02:00 -05:00
}) {
const path = '/api/v1/videos/' + args.videoId + '/captions/' + args.language
2018-08-06 04:45:24 -05:00
const captionfile = buildAbsoluteFixturePath(args.fixture)
const captionfileAttach = args.mimeType ? [ captionfile, { contentType: args.mimeType } ] : captionfile
2018-07-12 12:02:00 -05:00
return makeUploadRequest({
method: 'PUT',
url: args.url,
path,
token: args.accessToken,
fields: {},
attaches: {
2018-08-06 04:45:24 -05:00
captionfile: captionfileAttach
2018-07-12 12:02:00 -05:00
},
statusCodeExpected: args.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
2018-07-12 12:02:00 -05:00
})
}
function listVideoCaptions (url: string, videoId: string | number) {
const path = '/api/v1/videos/' + videoId + '/captions'
return makeGetRequest({
url,
path,
statusCodeExpected: HttpStatusCode.OK_200
2018-07-12 12:02:00 -05:00
})
}
function deleteVideoCaption (url: string, token: string, videoId: string | number, language: string) {
const path = '/api/v1/videos/' + videoId + '/captions/' + language
return makeDeleteRequest({
url,
token,
path,
statusCodeExpected: HttpStatusCode.NO_CONTENT_204
2018-07-12 12:02:00 -05:00
})
}
async function testCaptionFile (url: string, captionPath: string, containsString: string) {
const res = await request(url)
.get(captionPath)
.expect(HttpStatusCode.OK_200)
2018-07-12 12:02:00 -05:00
expect(res.text).to.contain(containsString)
}
// ---------------------------------------------------------------------------
export {
createVideoCaption,
listVideoCaptions,
testCaptionFile,
deleteVideoCaption
}