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/models'
9 buildAbsoluteFixturePath,
13 setAccessTokensToServers,
14 setDefaultVideoChannel
15 } from '@shared/extra-utils'
16 import { VideoPrivacy } from '@shared/models'
18 const expect = chai.expect
20 // Most classic resumable upload tests are done in other test suites
22 describe('Test resumable upload', function () {
23 const defaultFixture = 'video_short.mp4'
24 let server: PeerTubeServer
27 async function buildSize (fixture: string, size?: number) {
28 if (size !== undefined) return size
30 const baseFixture = buildAbsoluteFixturePath(fixture)
31 return (await stat(baseFixture)).size
34 async function prepareUpload (sizeArg?: number) {
35 const size = await buildSize(defaultFixture, sizeArg)
39 channelId: server.store.channel.id,
40 privacy: VideoPrivacy.PUBLIC,
41 fixture: defaultFixture
44 const mimetype = 'video/mp4'
46 const res = await server.videos.prepareResumableUpload({ attributes, size, mimetype })
48 return res.header['location'].split('?')[1]
51 async function sendChunks (options: {
54 expectedStatus?: HttpStatusCode
55 contentLength?: number
57 contentRangeBuilder?: (start: number, chunk: any) => string
59 const { pathUploadId, expectedStatus, contentLength, contentRangeBuilder } = options
61 const size = await buildSize(defaultFixture, options.size)
62 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
64 return server.videos.sendResumableChunks({
66 videoFilePath: absoluteFilePath,
74 async function checkFileSize (uploadIdArg: string, expectedSize: number | null) {
75 const uploadId = uploadIdArg.replace(/^upload_id=/, '')
77 const subPath = join('tmp', 'resumable-uploads', uploadId)
78 const filePath = server.servers.buildDirectory(subPath)
79 const exists = await pathExists(filePath)
81 if (expectedSize === null) {
82 expect(exists).to.be.false
86 expect(exists).to.be.true
88 expect((await stat(filePath)).size).to.equal(expectedSize)
91 async function countResumableUploads () {
92 const subPath = join('tmp', 'resumable-uploads')
93 const filePath = server.servers.buildDirectory(subPath)
95 const files = await readdir(filePath)
99 before(async function () {
102 server = await createSingleServer(1)
103 await setAccessTokensToServers([ server ])
104 await setDefaultVideoChannel([ server ])
106 const body = await server.users.getMyInfo()
109 await server.users.update({ userId: rootId, videoQuota: 10_000_000 })
112 describe('Directory cleaning', function () {
114 it('Should correctly delete files after an upload', async function () {
115 const uploadId = await prepareUpload()
116 await sendChunks({ pathUploadId: uploadId })
118 expect(await countResumableUploads()).to.equal(0)
121 it('Should not delete files after an unfinished upload', async function () {
122 await prepareUpload()
124 expect(await countResumableUploads()).to.equal(2)
127 it('Should not delete recent uploads', async function () {
128 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
130 expect(await countResumableUploads()).to.equal(2)
133 it('Should delete old uploads', async function () {
134 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
136 expect(await countResumableUploads()).to.equal(0)
140 describe('Resumable upload and chunks', function () {
142 it('Should accept the same amount of chunks', async function () {
143 const uploadId = await prepareUpload()
144 await sendChunks({ pathUploadId: uploadId })
146 await checkFileSize(uploadId, null)
149 it('Should not accept more chunks than expected', async function () {
151 const uploadId = await prepareUpload(size)
153 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
154 await checkFileSize(uploadId, 0)
157 it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
158 const uploadId = await prepareUpload(1500)
160 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
161 await checkFileSize(uploadId, 0)
164 it('Should not accept more chunks than expected with an invalid content length', async function () {
165 const uploadId = await prepareUpload(500)
169 const contentRangeBuilder = start => `bytes ${start}-${start + size - 1}/${size}`
170 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentRangeBuilder, contentLength: size })
171 await checkFileSize(uploadId, 0)
175 after(async function () {
176 await cleanupTests([ server ])