]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
5 | import { pathExists, readdir, stat } from 'fs-extra' | |
6 | import { join } from 'path' | |
7 | import { | |
8 | buildAbsoluteFixturePath, | |
9 | cleanupTests, | |
10 | createSingleServer, | |
11 | PeerTubeServer, | |
12 | setAccessTokensToServers, | |
13 | setDefaultVideoChannel | |
14 | } from '@shared/extra-utils' | |
15 | import { HttpStatusCode, VideoPrivacy } from '@shared/models' | |
16 | ||
17 | const expect = chai.expect | |
18 | ||
19 | // Most classic resumable upload tests are done in other test suites | |
20 | ||
21 | describe('Test resumable upload', function () { | |
22 | const defaultFixture = 'video_short.mp4' | |
23 | let server: PeerTubeServer | |
24 | let rootId: number | |
25 | ||
26 | async function buildSize (fixture: string, size?: number) { | |
27 | if (size !== undefined) return size | |
28 | ||
29 | const baseFixture = buildAbsoluteFixturePath(fixture) | |
30 | return (await stat(baseFixture)).size | |
31 | } | |
32 | ||
33 | async function prepareUpload (sizeArg?: number) { | |
34 | const size = await buildSize(defaultFixture, sizeArg) | |
35 | ||
36 | const attributes = { | |
37 | name: 'video', | |
38 | channelId: server.store.channel.id, | |
39 | privacy: VideoPrivacy.PUBLIC, | |
40 | fixture: defaultFixture | |
41 | } | |
42 | ||
43 | const mimetype = 'video/mp4' | |
44 | ||
45 | const res = await server.videos.prepareResumableUpload({ attributes, size, mimetype }) | |
46 | ||
47 | return res.header['location'].split('?')[1] | |
48 | } | |
49 | ||
50 | async function sendChunks (options: { | |
51 | pathUploadId: string | |
52 | size?: number | |
53 | expectedStatus?: HttpStatusCode | |
54 | contentLength?: number | |
55 | contentRange?: string | |
56 | contentRangeBuilder?: (start: number, chunk: any) => string | |
57 | }) { | |
58 | const { pathUploadId, expectedStatus, contentLength, contentRangeBuilder } = options | |
59 | ||
60 | const size = await buildSize(defaultFixture, options.size) | |
61 | const absoluteFilePath = buildAbsoluteFixturePath(defaultFixture) | |
62 | ||
63 | return server.videos.sendResumableChunks({ | |
64 | pathUploadId, | |
65 | videoFilePath: absoluteFilePath, | |
66 | size, | |
67 | contentLength, | |
68 | contentRangeBuilder, | |
69 | expectedStatus | |
70 | }) | |
71 | } | |
72 | ||
73 | async function checkFileSize (uploadIdArg: string, expectedSize: number | null) { | |
74 | const uploadId = uploadIdArg.replace(/^upload_id=/, '') | |
75 | ||
76 | const subPath = join('tmp', 'resumable-uploads', uploadId) | |
77 | const filePath = server.servers.buildDirectory(subPath) | |
78 | const exists = await pathExists(filePath) | |
79 | ||
80 | if (expectedSize === null) { | |
81 | expect(exists).to.be.false | |
82 | return | |
83 | } | |
84 | ||
85 | expect(exists).to.be.true | |
86 | ||
87 | expect((await stat(filePath)).size).to.equal(expectedSize) | |
88 | } | |
89 | ||
90 | async function countResumableUploads () { | |
91 | const subPath = join('tmp', 'resumable-uploads') | |
92 | const filePath = server.servers.buildDirectory(subPath) | |
93 | ||
94 | const files = await readdir(filePath) | |
95 | return files.length | |
96 | } | |
97 | ||
98 | before(async function () { | |
99 | this.timeout(30000) | |
100 | ||
101 | server = await createSingleServer(1) | |
102 | await setAccessTokensToServers([ server ]) | |
103 | await setDefaultVideoChannel([ server ]) | |
104 | ||
105 | const body = await server.users.getMyInfo() | |
106 | rootId = body.id | |
107 | ||
108 | await server.users.update({ userId: rootId, videoQuota: 10_000_000 }) | |
109 | }) | |
110 | ||
111 | describe('Directory cleaning', function () { | |
112 | ||
113 | it('Should correctly delete files after an upload', async function () { | |
114 | const uploadId = await prepareUpload() | |
115 | await sendChunks({ pathUploadId: uploadId }) | |
116 | await server.videos.endResumableUpload({ pathUploadId: uploadId }) | |
117 | ||
118 | expect(await countResumableUploads()).to.equal(0) | |
119 | }) | |
120 | ||
121 | it('Should not delete files after an unfinished upload', async function () { | |
122 | await prepareUpload() | |
123 | ||
124 | expect(await countResumableUploads()).to.equal(2) | |
125 | }) | |
126 | ||
127 | it('Should not delete recent uploads', async function () { | |
128 | await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } }) | |
129 | ||
130 | expect(await countResumableUploads()).to.equal(2) | |
131 | }) | |
132 | ||
133 | it('Should delete old uploads', async function () { | |
134 | await server.debug.sendCommand({ body: { command: 'remove-dandling-resumable-uploads' } }) | |
135 | ||
136 | expect(await countResumableUploads()).to.equal(0) | |
137 | }) | |
138 | }) | |
139 | ||
140 | describe('Resumable upload and chunks', function () { | |
141 | ||
142 | it('Should accept the same amount of chunks', async function () { | |
143 | const uploadId = await prepareUpload() | |
144 | await sendChunks({ pathUploadId: uploadId }) | |
145 | ||
146 | await checkFileSize(uploadId, null) | |
147 | }) | |
148 | ||
149 | it('Should not accept more chunks than expected', async function () { | |
150 | const uploadId = await prepareUpload(100) | |
151 | ||
152 | await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409 }) | |
153 | await checkFileSize(uploadId, 0) | |
154 | }) | |
155 | ||
156 | it('Should not accept more chunks than expected with an invalid content length/content range', async function () { | |
157 | const uploadId = await prepareUpload(1500) | |
158 | ||
159 | // Content length check seems to have changed in v16 | |
160 | if (process.version.startsWith('v16')) { | |
161 | await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.CONFLICT_409, contentLength: 1000 }) | |
162 | await checkFileSize(uploadId, 1000) | |
163 | } else { | |
164 | await sendChunks({ pathUploadId: uploadId, expectedStatus: HttpStatusCode.BAD_REQUEST_400, contentLength: 1000 }) | |
165 | await checkFileSize(uploadId, 0) | |
166 | } | |
167 | }) | |
168 | ||
169 | it('Should not accept more chunks than expected with an invalid content length', async function () { | |
170 | const uploadId = await prepareUpload(500) | |
171 | ||
172 | const size = 1000 | |
173 | ||
174 | // Content length check seems to have changed in v16 | |
175 | const expectedStatus = process.version.startsWith('v16') | |
176 | ? HttpStatusCode.CONFLICT_409 | |
177 | : HttpStatusCode.BAD_REQUEST_400 | |
178 | ||
179 | const contentRangeBuilder = (start: number) => `bytes ${start}-${start + size - 1}/${size}` | |
180 | await sendChunks({ pathUploadId: uploadId, expectedStatus, contentRangeBuilder, contentLength: size }) | |
181 | await checkFileSize(uploadId, 0) | |
182 | }) | |
183 | }) | |
184 | ||
185 | after(async function () { | |
186 | await cleanupTests([ server ]) | |
187 | }) | |
188 | }) |