-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadBuild.ts
76 lines (70 loc) · 2.01 KB
/
uploadBuild.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as core from '@actions/core';
import fs from 'node:fs/promises';
import { CHUNK_SIZE } from './utils';
export async function uploadBuild({
apiKey,
apiBaseUrl,
gameId,
platform,
filePath,
buildId,
}: {
apiKey: string;
apiBaseUrl: string;
gameId: string;
platform: 'windows' | 'mac';
filePath: string;
buildId: string;
}) {
try {
core.info(`Starting ${platform} build upload with file: ${filePath}`);
const fileHandle = await fs.open(filePath);
const stats = await fileHandle.stat();
const totalChunks = Math.ceil(stats.size / CHUNK_SIZE);
for (let i = 0; i < totalChunks; i++) {
try {
core.info(`Uploading chunk ${i + 1}/${totalChunks}`);
const buffer = Buffer.alloc(CHUNK_SIZE);
const { bytesRead } = await fileHandle.read(
buffer,
0,
CHUNK_SIZE,
i * CHUNK_SIZE
);
core.info(`Read ${bytesRead} bytes for chunk ${i + 1}`);
const body = new FormData();
body.append('chunkNumber', (i + 1).toString());
body.append('platform', platform);
body.append('buildId', buildId);
body.append('buildChunk', new Blob([buffer.subarray(0, bytesRead)]));
const chunkResponse = await fetch(
`${apiBaseUrl}/v3/dashboard/games/${gameId}/builds`,
{
method: 'POST',
headers: {
'x-api-key': apiKey,
},
body,
}
);
if (!chunkResponse.ok) {
throw new Error(
`Failed to upload chunk ${i + 1}: ${chunkResponse.status}`
);
}
core.info(`Successfully uploaded chunk ${i + 1}/${totalChunks}`);
} catch (error) {
await fileHandle.close();
throw error;
}
}
await fileHandle.close();
core.info(`Completed uploading all chunks for platform: ${platform}`);
} catch (error) {
console.error(
`Error uploading build for platform ${platform} ${apiBaseUrl}:`,
error
);
throw error;
}
}