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,
12 prepareResumableUpload,
15 setAccessTokensToServers,
16 setDefaultVideoChannel
17 } from '@shared/extra-utils'
18 import { VideoPrivacy } from '@shared/models'
20 const expect = chai.expect
22 // Most classic resumable upload tests are done in other test suites
24 describe('Test resumable upload', function () {
25 const defaultFixture = 'video_short.mp4'
26 let server: ServerInfo
29 async function buildSize (fixture: string, size?: number) {
30 if (size !== undefined) return size
32 const baseFixture = buildAbsoluteFixturePath(fixture)
33 return (await stat(baseFixture)).size
36 async function prepareUpload (sizeArg?: number) {
37 const size = await buildSize(defaultFixture, sizeArg)
41 channelId: server.videoChannel.id,
42 privacy: VideoPrivacy.PUBLIC,
43 fixture: defaultFixture
46 const mimetype = 'video/mp4'
48 const res = await prepareResumableUpload({ url: server.url, token: server.accessToken, attributes, size, mimetype })
50 return res.header['location'].split('?')[1]
53 async function sendChunks (options: {
56 expectedStatus?: HttpStatusCode
57 contentLength?: number
59 contentRangeBuilder?: (start: number, chunk: any) => string
61 const { pathUploadId, expectedStatus, contentLength, contentRangeBuilder } = options
63 const size = await buildSize(defaultFixture, options.size)
64 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
66 return sendResumableChunks({
68 token: server.accessToken,
70 videoFilePath: absoluteFilePath,
74 specialStatus: expectedStatus
78 async function checkFileSize (uploadIdArg: string, expectedSize: number | null) {
79 const uploadId = uploadIdArg.replace(/^upload_id=/, '')
81 const subPath = join('tmp', 'resumable-uploads', uploadId)
82 const filePath = server.serversCommand.buildDirectory(subPath)
83 const exists = await pathExists(filePath)
85 if (expectedSize === null) {
86 expect(exists).to.be.false
90 expect(exists).to.be.true
92 expect((await stat(filePath)).size).to.equal(expectedSize)
95 async function countResumableUploads () {
96 const subPath = join('tmp', 'resumable-uploads')
97 const filePath = server.serversCommand.buildDirectory(subPath)
99 const files = await readdir(filePath)
103 before(async function () {
106 server = await flushAndRunServer(1)
107 await setAccessTokensToServers([ server ])
108 await setDefaultVideoChannel([ server ])
110 const body = await server.usersCommand.getMyInfo()
113 await server.usersCommand.update({ userId: rootId, videoQuota: 10_000_000 })
116 describe('Directory cleaning', function () {
118 it('Should correctly delete files after an upload', async function () {
119 const uploadId = await prepareUpload()
120 await sendChunks({ pathUploadId: uploadId })
122 expect(await countResumableUploads()).to.equal(0)
125 it('Should not delete files after an unfinished upload', async function () {
126 await prepareUpload()
128 expect(await countResumableUploads()).to.equal(2)
131 it('Should not delete recent uploads', async function () {
132 await server.debugCommand.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
134 expect(await countResumableUploads()).to.equal(2)
137 it('Should delete old uploads', async function () {
138 await server.debugCommand.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
140 expect(await countResumableUploads()).to.equal(0)
144 describe('Resumable upload and chunks', function () {
146 it('Should accept the same amount of chunks', async function () {
147 const uploadId = await prepareUpload()
148 await sendChunks({ pathUploadId: uploadId })
150 await checkFileSize(uploadId, null)
153 it('Should not accept more chunks than expected', async function () {
155 const uploadId = await prepareUpload(size)
157 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
158 await checkFileSize(uploadId, 0)
161 it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
162 const uploadId = await prepareUpload(1500)
164 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
165 await checkFileSize(uploadId, 0)
168 it('Should not accept more chunks than expected with an invalid content length', async function () {
169 const uploadId = await prepareUpload(500)
173 const contentRangeBuilder = start => `bytes ${start}-${start + size - 1}/${size}`
174 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentRangeBuilder, contentLength: size })
175 await checkFileSize(uploadId, 0)
179 after(async function () {
180 await cleanupTests([ server ])