1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { pathExists, readdir, stat } from 'fs-extra'
5 import { join } from 'path'
6 import { buildAbsoluteFixturePath } from '@shared/core-utils'
7 import { sha1 } from '@shared/extra-utils'
8 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
9 import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, setDefaultVideoChannel } from '@shared/server-commands'
11 const expect = chai.expect
13 // Most classic resumable upload tests are done in other test suites
15 describe('Test resumable upload', function () {
16 const defaultFixture = 'video_short.mp4'
17 let server: PeerTubeServer
19 let userAccessToken: string
20 let userChannelId: number
22 async function buildSize (fixture: string, size?: number) {
23 if (size !== undefined) return size
25 const baseFixture = buildAbsoluteFixturePath(fixture)
26 return (await stat(baseFixture)).size
29 async function prepareUpload (options: {
36 const { token, originalName, lastModified } = options
38 const size = await buildSize(defaultFixture, options.size)
42 channelId: options.channelId ?? server.store.channel.id,
43 privacy: VideoPrivacy.PUBLIC,
44 fixture: defaultFixture
47 const mimetype = 'video/mp4'
49 const res = await server.videos.prepareResumableUpload({ token, attributes, size, mimetype, originalName, lastModified })
51 return res.header['location'].split('?')[1]
54 async function sendChunks (options: {
58 expectedStatus?: HttpStatusCode
59 contentLength?: number
61 contentRangeBuilder?: (start: number, chunk: any) => string
62 digestBuilder?: (chunk: any) => string
64 const { token, pathUploadId, expectedStatus, contentLength, contentRangeBuilder, digestBuilder } = options
66 const size = await buildSize(defaultFixture, options.size)
67 const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
69 return server.videos.sendResumableChunks({
72 videoFilePath: absoluteFilePath,
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 = server.servers.buildDirectory(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 = server.servers.buildDirectory(subPath)
102 const files = await readdir(filePath)
106 before(async function () {
109 server = await createSingleServer(1)
110 await setAccessTokensToServers([ server ])
111 await setDefaultVideoChannel([ server ])
113 const body = await server.users.getMyInfo()
117 userAccessToken = await server.users.generateUserAndToken('user1')
118 const { videoChannels } = await server.users.getMyInfo({ token: userAccessToken })
119 userChannelId = videoChannels[0].id
122 await server.users.update({ userId: rootId, videoQuota: 10_000_000 })
125 describe('Directory cleaning', function () {
127 // FIXME: https://github.com/kukhariev/node-uploadx/pull/524/files#r852989382
128 // it('Should correctly delete files after an upload', async function () {
129 // const uploadId = await prepareUpload()
130 // await sendChunks({ pathUploadId: uploadId })
131 // await server.videos.endResumableUpload({ pathUploadId: uploadId })
133 // expect(await countResumableUploads()).to.equal(0)
136 it('Should not delete files after an unfinished upload', async function () {
137 await prepareUpload()
139 expect(await countResumableUploads()).to.equal(2)
142 it('Should not delete recent uploads', async function () {
143 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
145 expect(await countResumableUploads()).to.equal(2)
148 it('Should delete old uploads', async function () {
149 await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
151 expect(await countResumableUploads()).to.equal(0)
155 describe('Resumable upload and chunks', function () {
157 it('Should accept the same amount of chunks', async function () {
158 const uploadId = await prepareUpload()
159 await sendChunks({ pathUploadId: uploadId })
161 await checkFileSize(uploadId, null)
164 it('Should not accept more chunks than expected', async function () {
165 const uploadId = await prepareUpload({ size: 100 })
167 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
168 await checkFileSize(uploadId, 0)
171 it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
172 const uploadId = await prepareUpload({ size: 1500 })
174 // Content length check seems to have changed in v16
175 if (process.version.startsWith('v16')) {
176 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409, contentLength: 1000 })
177 await checkFileSize(uploadId, 1000)
179 await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
180 await checkFileSize(uploadId, 0)
184 it('Should not accept more chunks than expected with an invalid content length', async function () {
185 const uploadId = await prepareUpload({ size: 500 })
189 // Content length check seems to have changed in v16
190 const expectedStatus = process.version.startsWith('v16')
191 ? HttpStatusCode.CONFLICT_409
192 : HttpStatusCode.BAD_REQUEST_400
194 const contentRangeBuilder = (start: number) => `bytes ${start}-${start + size - 1}/${size}`
195 await sendChunks({ pathUploadId: uploadId, expectedStatus, contentRangeBuilder, contentLength: size })
196 await checkFileSize(uploadId, 0)
199 it('Should be able to accept 2 PUT requests', async function () {
200 const uploadId = await prepareUpload()
202 const result1 = await sendChunks({ pathUploadId: uploadId })
203 const result2 = await sendChunks({ pathUploadId: uploadId })
205 expect(result1.body.video.uuid).to.exist
206 expect(result1.body.video.uuid).to.equal(result2.body.video.uuid)
208 expect(result1.headers['x-resumable-upload-cached']).to.not.exist
209 expect(result2.headers['x-resumable-upload-cached']).to.equal('true')
211 await checkFileSize(uploadId, null)
214 it('Should not have the same upload id with 2 different users', async function () {
215 const originalName = 'toto.mp4'
216 const lastModified = new Date().getTime()
218 const uploadId1 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
219 const uploadId2 = await prepareUpload({ originalName, lastModified, channelId: userChannelId, token: userAccessToken })
221 expect(uploadId1).to.not.equal(uploadId2)
224 it('Should have the same upload id with the same user', async function () {
225 const originalName = 'toto.mp4'
226 const lastModified = new Date().getTime()
228 const uploadId1 = await prepareUpload({ originalName, lastModified })
229 const uploadId2 = await prepareUpload({ originalName, lastModified })
231 expect(uploadId1).to.equal(uploadId2)
234 it('Should not cache a request with 2 different users', async function () {
235 const originalName = 'toto.mp4'
236 const lastModified = new Date().getTime()
238 const uploadId = await prepareUpload({ originalName, lastModified, token: server.accessToken })
240 await sendChunks({ pathUploadId: uploadId, token: server.accessToken })
241 await sendChunks({ pathUploadId: uploadId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
244 it('Should not cache a request after a delete', async function () {
245 const originalName = 'toto.mp4'
246 const lastModified = new Date().getTime()
247 const uploadId1 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
249 await sendChunks({ pathUploadId: uploadId1 })
250 await server.videos.endResumableUpload({ pathUploadId: uploadId1 })
252 const uploadId2 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
253 expect(uploadId1).to.equal(uploadId2)
255 const result2 = await sendChunks({ pathUploadId: uploadId1 })
256 expect(result2.headers['x-resumable-upload-cached']).to.not.exist
259 it('Should refuse an invalid digest', async function () {
260 const uploadId = await prepareUpload({ token: server.accessToken })
263 pathUploadId: uploadId,
264 token: server.accessToken,
265 digestBuilder: () => 'sha=' + 'a'.repeat(40),
270 it('Should accept an appropriate digest', async function () {
271 const uploadId = await prepareUpload({ token: server.accessToken })
274 pathUploadId: uploadId,
275 token: server.accessToken,
276 digestBuilder: (chunk: Buffer) => {
277 return 'sha1=' + sha1(chunk, 'base64')
283 after(async function () {
284 await cleanupTests([ server ])