2021-05-10 04:13:41 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
2022-08-17 08:44:32 -05:00
|
|
|
import { expect } from 'chai'
|
2021-05-10 04:13:41 -05:00
|
|
|
import { pathExists, readdir, stat } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
2021-12-17 04:58:15 -06:00
|
|
|
import { buildAbsoluteFixturePath } from '@shared/core-utils'
|
2022-04-19 07:25:33 -05:00
|
|
|
import { sha1 } from '@shared/extra-utils'
|
2021-07-16 07:27:30 -05:00
|
|
|
import { HttpStatusCode, VideoPrivacy } from '@shared/models'
|
2021-12-17 04:58:15 -06:00
|
|
|
import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers, setDefaultVideoChannel } from '@shared/server-commands'
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
// Most classic resumable upload tests are done in other test suites
|
|
|
|
|
|
|
|
describe('Test resumable upload', function () {
|
|
|
|
const defaultFixture = 'video_short.mp4'
|
2021-07-16 02:47:51 -05:00
|
|
|
let server: PeerTubeServer
|
2021-05-10 04:13:41 -05:00
|
|
|
let rootId: number
|
2021-11-10 02:42:37 -06:00
|
|
|
let userAccessToken: string
|
|
|
|
let userChannelId: number
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
async function buildSize (fixture: string, size?: number) {
|
|
|
|
if (size !== undefined) return size
|
|
|
|
|
|
|
|
const baseFixture = buildAbsoluteFixturePath(fixture)
|
|
|
|
return (await stat(baseFixture)).size
|
|
|
|
}
|
|
|
|
|
2021-11-10 02:42:37 -06:00
|
|
|
async function prepareUpload (options: {
|
|
|
|
channelId?: number
|
|
|
|
token?: string
|
|
|
|
size?: number
|
|
|
|
originalName?: string
|
|
|
|
lastModified?: number
|
|
|
|
} = {}) {
|
|
|
|
const { token, originalName, lastModified } = options
|
|
|
|
|
|
|
|
const size = await buildSize(defaultFixture, options.size)
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
const attributes = {
|
|
|
|
name: 'video',
|
2021-11-10 02:42:37 -06:00
|
|
|
channelId: options.channelId ?? server.store.channel.id,
|
2021-05-10 04:13:41 -05:00
|
|
|
privacy: VideoPrivacy.PUBLIC,
|
|
|
|
fixture: defaultFixture
|
|
|
|
}
|
|
|
|
|
|
|
|
const mimetype = 'video/mp4'
|
|
|
|
|
2021-11-10 02:42:37 -06:00
|
|
|
const res = await server.videos.prepareResumableUpload({ token, attributes, size, mimetype, originalName, lastModified })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
return res.header['location'].split('?')[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendChunks (options: {
|
2021-11-10 02:42:37 -06:00
|
|
|
token?: string
|
2021-05-10 04:13:41 -05:00
|
|
|
pathUploadId: string
|
|
|
|
size?: number
|
|
|
|
expectedStatus?: HttpStatusCode
|
|
|
|
contentLength?: number
|
|
|
|
contentRange?: string
|
|
|
|
contentRangeBuilder?: (start: number, chunk: any) => string
|
2022-04-19 07:25:33 -05:00
|
|
|
digestBuilder?: (chunk: any) => string
|
2021-05-10 04:13:41 -05:00
|
|
|
}) {
|
2022-04-19 07:25:33 -05:00
|
|
|
const { token, pathUploadId, expectedStatus, contentLength, contentRangeBuilder, digestBuilder } = options
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
const size = await buildSize(defaultFixture, options.size)
|
|
|
|
const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture)
|
|
|
|
|
2021-07-16 02:04:35 -05:00
|
|
|
return server.videos.sendResumableChunks({
|
2021-11-10 02:42:37 -06:00
|
|
|
token,
|
2021-05-10 04:13:41 -05:00
|
|
|
pathUploadId,
|
|
|
|
videoFilePath: absoluteFilePath,
|
|
|
|
size,
|
|
|
|
contentLength,
|
|
|
|
contentRangeBuilder,
|
2022-04-19 07:25:33 -05:00
|
|
|
digestBuilder,
|
2021-07-15 03:02:54 -05:00
|
|
|
expectedStatus
|
2021-05-10 04:13:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkFileSize (uploadIdArg: string, expectedSize: number | null) {
|
|
|
|
const uploadId = uploadIdArg.replace(/^upload_id=/, '')
|
|
|
|
|
2023-05-02 06:51:06 -05:00
|
|
|
const subPath = join('tmp', 'resumable-uploads', `${rootId}-${uploadId}.mp4`)
|
2021-07-16 02:04:35 -05:00
|
|
|
const filePath = server.servers.buildDirectory(subPath)
|
2021-05-10 04:13:41 -05:00
|
|
|
const exists = await pathExists(filePath)
|
|
|
|
|
|
|
|
if (expectedSize === null) {
|
|
|
|
expect(exists).to.be.false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(exists).to.be.true
|
|
|
|
|
|
|
|
expect((await stat(filePath)).size).to.equal(expectedSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function countResumableUploads () {
|
|
|
|
const subPath = join('tmp', 'resumable-uploads')
|
2021-07-16 02:04:35 -05:00
|
|
|
const filePath = server.servers.buildDirectory(subPath)
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
const files = await readdir(filePath)
|
|
|
|
return files.length
|
|
|
|
}
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
this.timeout(30000)
|
|
|
|
|
2021-07-16 02:47:51 -05:00
|
|
|
server = await createSingleServer(1)
|
2021-07-22 07:28:03 -05:00
|
|
|
await setAccessTokensToServers([ server ])
|
|
|
|
await setDefaultVideoChannel([ server ])
|
2021-05-10 04:13:41 -05:00
|
|
|
|
2021-07-16 02:04:35 -05:00
|
|
|
const body = await server.users.getMyInfo()
|
2021-07-13 07:23:01 -05:00
|
|
|
rootId = body.id
|
2021-05-10 04:13:41 -05:00
|
|
|
|
2021-11-10 02:42:37 -06:00
|
|
|
{
|
|
|
|
userAccessToken = await server.users.generateUserAndToken('user1')
|
|
|
|
const { videoChannels } = await server.users.getMyInfo({ token: userAccessToken })
|
|
|
|
userChannelId = videoChannels[0].id
|
|
|
|
}
|
|
|
|
|
2021-07-16 02:04:35 -05:00
|
|
|
await server.users.update({ userId: rootId, videoQuota: 10_000_000 })
|
2021-05-10 04:13:41 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Directory cleaning', function () {
|
|
|
|
|
2022-04-19 07:25:33 -05:00
|
|
|
// FIXME: https://github.com/kukhariev/node-uploadx/pull/524/files#r852989382
|
|
|
|
// it('Should correctly delete files after an upload', async function () {
|
|
|
|
// const uploadId = await prepareUpload()
|
|
|
|
// await sendChunks({ pathUploadId: uploadId })
|
|
|
|
// await server.videos.endResumableUpload({ pathUploadId: uploadId })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
2022-04-19 07:25:33 -05:00
|
|
|
// expect(await countResumableUploads()).to.equal(0)
|
|
|
|
// })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
it('Should not delete files after an unfinished upload', async function () {
|
|
|
|
await prepareUpload()
|
|
|
|
|
|
|
|
expect(await countResumableUploads()).to.equal(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not delete recent uploads', async function () {
|
2021-07-16 02:04:35 -05:00
|
|
|
await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
expect(await countResumableUploads()).to.equal(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should delete old uploads', async function () {
|
2021-07-16 02:04:35 -05:00
|
|
|
await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
expect(await countResumableUploads()).to.equal(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Resumable upload and chunks', function () {
|
|
|
|
|
|
|
|
it('Should accept the same amount of chunks', async function () {
|
|
|
|
const uploadId = await prepareUpload()
|
|
|
|
await sendChunks({ pathUploadId: uploadId })
|
|
|
|
|
|
|
|
await checkFileSize(uploadId, null)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not accept more chunks than expected', async function () {
|
2021-11-10 02:42:37 -06:00
|
|
|
const uploadId = await prepareUpload({ size: 100 })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 })
|
|
|
|
await checkFileSize(uploadId, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not accept more chunks than expected with an invalid content length/content range', async function () {
|
2021-11-10 02:42:37 -06:00
|
|
|
const uploadId = await prepareUpload({ size: 1500 })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
2022-09-09 04:11:47 -05:00
|
|
|
// Content length check can be different depending on the node version
|
|
|
|
try {
|
2021-07-22 07:58:25 -05:00
|
|
|
await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409, contentLength: 1000 })
|
2022-09-09 04:11:47 -05:00
|
|
|
await checkFileSize(uploadId, 0)
|
|
|
|
} catch {
|
2021-07-22 07:58:25 -05:00
|
|
|
await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 })
|
|
|
|
await checkFileSize(uploadId, 0)
|
|
|
|
}
|
2021-05-10 04:13:41 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not accept more chunks than expected with an invalid content length', async function () {
|
2021-11-10 02:42:37 -06:00
|
|
|
const uploadId = await prepareUpload({ size: 500 })
|
2021-05-10 04:13:41 -05:00
|
|
|
|
|
|
|
const size = 1000
|
|
|
|
|
2021-07-23 04:20:00 -05:00
|
|
|
// Content length check seems to have changed in v16
|
|
|
|
const expectedStatus = process.version.startsWith('v16')
|
|
|
|
? HttpStatusCode.CONFLICT_409
|
|
|
|
: HttpStatusCode.BAD_REQUEST_400
|
|
|
|
|
2021-07-22 07:28:03 -05:00
|
|
|
const contentRangeBuilder = (start: number) => `bytes ${start}-${start + size - 1}/${size}`
|
2021-07-23 04:20:00 -05:00
|
|
|
await sendChunks({ pathUploadId: uploadId, expectedStatus, contentRangeBuilder, contentLength: size })
|
2021-05-10 04:13:41 -05:00
|
|
|
await checkFileSize(uploadId, 0)
|
|
|
|
})
|
2021-10-25 10:42:20 -05:00
|
|
|
|
|
|
|
it('Should be able to accept 2 PUT requests', async function () {
|
|
|
|
const uploadId = await prepareUpload()
|
|
|
|
|
|
|
|
const result1 = await sendChunks({ pathUploadId: uploadId })
|
|
|
|
const result2 = await sendChunks({ pathUploadId: uploadId })
|
|
|
|
|
|
|
|
expect(result1.body.video.uuid).to.exist
|
|
|
|
expect(result1.body.video.uuid).to.equal(result2.body.video.uuid)
|
|
|
|
|
|
|
|
expect(result1.headers['x-resumable-upload-cached']).to.not.exist
|
|
|
|
expect(result2.headers['x-resumable-upload-cached']).to.equal('true')
|
|
|
|
|
|
|
|
await checkFileSize(uploadId, null)
|
|
|
|
})
|
2021-11-10 02:42:37 -06:00
|
|
|
|
|
|
|
it('Should not have the same upload id with 2 different users', async function () {
|
|
|
|
const originalName = 'toto.mp4'
|
|
|
|
const lastModified = new Date().getTime()
|
|
|
|
|
|
|
|
const uploadId1 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
|
|
|
|
const uploadId2 = await prepareUpload({ originalName, lastModified, channelId: userChannelId, token: userAccessToken })
|
|
|
|
|
|
|
|
expect(uploadId1).to.not.equal(uploadId2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should have the same upload id with the same user', async function () {
|
|
|
|
const originalName = 'toto.mp4'
|
|
|
|
const lastModified = new Date().getTime()
|
|
|
|
|
|
|
|
const uploadId1 = await prepareUpload({ originalName, lastModified })
|
|
|
|
const uploadId2 = await prepareUpload({ originalName, lastModified })
|
|
|
|
|
|
|
|
expect(uploadId1).to.equal(uploadId2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not cache a request with 2 different users', async function () {
|
|
|
|
const originalName = 'toto.mp4'
|
|
|
|
const lastModified = new Date().getTime()
|
|
|
|
|
|
|
|
const uploadId = await prepareUpload({ originalName, lastModified, token: server.accessToken })
|
|
|
|
|
|
|
|
await sendChunks({ pathUploadId: uploadId, token: server.accessToken })
|
|
|
|
await sendChunks({ pathUploadId: uploadId, token: userAccessToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not cache a request after a delete', async function () {
|
|
|
|
const originalName = 'toto.mp4'
|
|
|
|
const lastModified = new Date().getTime()
|
|
|
|
const uploadId1 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
|
|
|
|
|
|
|
|
await sendChunks({ pathUploadId: uploadId1 })
|
|
|
|
await server.videos.endResumableUpload({ pathUploadId: uploadId1 })
|
|
|
|
|
|
|
|
const uploadId2 = await prepareUpload({ originalName, lastModified, token: server.accessToken })
|
|
|
|
expect(uploadId1).to.equal(uploadId2)
|
|
|
|
|
|
|
|
const result2 = await sendChunks({ pathUploadId: uploadId1 })
|
|
|
|
expect(result2.headers['x-resumable-upload-cached']).to.not.exist
|
|
|
|
})
|
2022-04-19 07:25:33 -05:00
|
|
|
|
|
|
|
it('Should refuse an invalid digest', async function () {
|
|
|
|
const uploadId = await prepareUpload({ token: server.accessToken })
|
|
|
|
|
|
|
|
await sendChunks({
|
|
|
|
pathUploadId: uploadId,
|
|
|
|
token: server.accessToken,
|
|
|
|
digestBuilder: () => 'sha=' + 'a'.repeat(40),
|
2023-04-21 08:00:01 -05:00
|
|
|
expectedStatus: 460 as any
|
2022-04-19 07:25:33 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should accept an appropriate digest', async function () {
|
|
|
|
const uploadId = await prepareUpload({ token: server.accessToken })
|
|
|
|
|
|
|
|
await sendChunks({
|
|
|
|
pathUploadId: uploadId,
|
|
|
|
token: server.accessToken,
|
|
|
|
digestBuilder: (chunk: Buffer) => {
|
|
|
|
return 'sha1=' + sha1(chunk, 'base64')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-05-10 04:13:41 -05:00
|
|
|
})
|
|
|
|
|
2021-07-01 09:47:14 -05:00
|
|
|
after(async function () {
|
2021-07-22 07:28:03 -05:00
|
|
|
await cleanupTests([ server ])
|
2021-07-01 09:47:14 -05:00
|
|
|
})
|
2021-05-10 04:13:41 -05:00
|
|
|
})
|