1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { pathExists, readdir, stat } from 'fs-extra'
6 import { join } from 'path'
7 import { HttpStatusCode } from '@shared/core-utils'
9 buildAbsoluteFixturePath,
14 prepareResumableUpload,
17 setAccessTokensToServers,
18 setDefaultVideoChannel,
20 } from '@shared/extra-utils'
21 import { MyUser, VideoPrivacy } from '@shared/models'
23 const expect = chai.expect
25 // Most classic resumable upload tests are done in other test suites
27 describe('Test resumable upload', function () {
28 const defaultFixture = 'video_short.mp4'
29 let server: ServerInfo
32 async function buildSize (fixture: string, size?: number) {
33 if (size !== undefined) return size
35 const baseFixture = buildAbsoluteFixturePath(fixture)
36 return (await stat(baseFixture)).size
39 async function prepareUpload (sizeArg?: number) {
40 const size = await buildSize(defaultFixture, sizeArg)
44 channelId: server.videoChannel.id,
45 privacy: VideoPrivacy.PUBLIC,
46 fixture: defaultFixture
49 const mimetype = 'video/mp4'
51 const res = await prepareResumableUpload({ url: server.url, token: server.accessToken, attributes, size, mimetype })
53 return res.header['location'].split('?')[1]
56 async function sendChunks (options: {
59 expectedStatus?: HttpStatusCode
60 contentLength?: number
62 contentRangeBuilder?: (start: number, chunk: any) => string
64 const { pathUploadId, expectedStatus, contentLength, contentRangeBuilder } = options
66 const size = await buildSize(defaultFixture, options.size)
67 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
69 return sendResumableChunks({
71 token: server.accessToken,
73 videoFilePath: absoluteFilePath,
77 specialStatus: expectedStatus
81 async function checkFileSize (uploadIdArg: string, expectedSize: number | null) {
82 const uploadId = uploadIdArg.replace(/^upload_id=/, '')
84 const subPath = join('tmp', 'resumable-uploads', uploadId)
85 const filePath = buildServerDirectory(server, subPath)
86 const exists = await pathExists(filePath)
88 if (expectedSize === null) {
89 expect(exists).to.be.false
93 expect(exists).to.be.true
95 expect((await stat(filePath)).size).to.equal(expectedSize)
98 async function countResumableUploads () {
99 const subPath = join('tmp', 'resumable-uploads')
100 const filePath = buildServerDirectory(server, subPath)
102 const files = await readdir(filePath)
106 before(async function () {
109 server = await flushAndRunServer(1)
110 await setAccessTokensToServers([ server ])
111 await setDefaultVideoChannel([ server ])
113 const res = await getMyUserInformation(server.url, server.accessToken)
114 rootId = (res.body as MyUser).id
119 accessToken: server.accessToken,
120 videoQuota: 10_000_000
124 describe('Directory cleaning', function () {
126 it('Should correctly delete files after an upload', async function () {
127 const uploadId = await prepareUpload()
128 await sendChunks({ pathUploadId: uploadId })
130 expect(await countResumableUploads()).to.equal(0)
133 it('Should not delete files after an unfinished upload', async function () {
134 await prepareUpload()
136 expect(await countResumableUploads()).to.equal(2)
139 it('Should not delete recent uploads', async function () {
140 await server.debugCommand.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
142 expect(await countResumableUploads()).to.equal(2)
145 it('Should delete old uploads', async function () {
146 await server.debugCommand.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
148 expect(await countResumableUploads()).to.equal(0)
152 describe('Resumable upload and chunks', function () {
154 it('Should accept the same amount of chunks', async function () {
155 const uploadId = await prepareUpload()
156 await sendChunks({ pathUploadId: uploadId })
158 await checkFileSize(uploadId, null)
161 it('Should not accept more chunks than expected', async function () {
163 const uploadId = await prepareUpload(size)
165 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
166 await checkFileSize(uploadId, 0)
169 it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
170 const uploadId = await prepareUpload(1500)
172 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
173 await checkFileSize(uploadId, 0)
176 it('Should not accept more chunks than expected with an invalid content length', async function () {
177 const uploadId = await prepareUpload(500)
181 const contentRangeBuilder = start => `bytes ${start}-${start + size - 1}/${size}`
182 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentRangeBuilder, contentLength: size })
183 await checkFileSize(uploadId, 0)
187 after(async function () {
188 await cleanupTests([ server ])