Skip to content

Commit 36e137b

Browse files
committed
Ensure we always clean up split file parts
1 parent 5cffce6 commit 36e137b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

examples/shared/files.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,13 @@ async function uploadFile(filePath, fileName = path.basename(filePath)) {
170170
throw new Error(`Unsupported file type: ${fileName}`);
171171
}
172172

173-
try {
174-
let file, upload;
173+
let file,
174+
upload,
175+
parts = [];
175176

177+
try {
176178
if (needsMultiPart) {
177-
const parts = await splitFile.splitFileBySize(filePath, PART_SIZE);
179+
parts = await splitFile.splitFileBySize(filePath, PART_SIZE);
178180

179181
if (parts.length > MAX_PARTS) {
180182
throw new Error(`File is too large. Maximum number of parts is ${MAX_PARTS}.`);
@@ -195,11 +197,6 @@ async function uploadFile(filePath, fileName = path.basename(filePath)) {
195197

196198
// Complete the upload
197199
upload = await completeMultiPartUpload(file.id);
198-
199-
// Clean up temporary files
200-
for (const part of parts) {
201-
await fs.promises.unlink(part);
202-
}
203200
} else {
204201
// Single-part upload
205202
const fileStream = fs.createReadStream(filePath);
@@ -214,6 +211,11 @@ async function uploadFile(filePath, fileName = path.basename(filePath)) {
214211
throw new Error(`Upload failed with status: ${error.response.status}`);
215212
}
216213
throw error;
214+
} finally {
215+
// Clean up temporary files
216+
for (const part of parts) {
217+
await fs.promises.unlink(part);
218+
}
217219
}
218220
}
219221

0 commit comments

Comments
 (0)