-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-uploader.ts
More file actions
182 lines (156 loc) · 5.06 KB
/
Copy pathobject-uploader.ts
File metadata and controls
182 lines (156 loc) · 5.06 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as core from '@actions/core';
import * as fs from 'fs';
import * as path from 'path';
import { RunloopSDK } from '@runloop/api-client';
export interface UploadResult {
objectId: string;
objectName: string;
}
/**
* Upload a tar.gz file directly.
*/
export async function uploadTarFile(
client: RunloopSDK,
filePath: string,
ttlDays?: number,
isPublic?: boolean
): Promise<UploadResult> {
core.info(`Uploading tar file: ${filePath}`);
const fileBuffer = fs.readFileSync(filePath);
const fileName = path.basename(filePath);
// Determine content type based on file extension
let contentType: 'tgz' | 'tar' | 'gzip';
if (fileName.endsWith('.tar.gz') || fileName.endsWith('.tgz')) {
contentType = 'tgz';
} else if (fileName.endsWith('.tar')) {
contentType = 'tar';
} else if (fileName.endsWith('.gz')) {
contentType = 'gzip';
} else {
throw new Error(`Unsupported tar file extension: ${fileName}`);
}
return uploadBuffer(client, fileBuffer, fileName, contentType, ttlDays, isPublic);
}
/**
* Upload a single file (text or binary).
* Always uses 'binary' content type so rage places the file as-is
* instead of extracting it (which it would do for tar/tgz).
*/
export async function uploadSingleFile(
client: RunloopSDK,
filePath: string,
ttlDays?: number,
isPublic?: boolean
): Promise<UploadResult> {
core.info(`Uploading single file: ${filePath}`);
const fileBuffer = fs.readFileSync(filePath);
const fileName = path.basename(filePath);
return uploadBuffer(client, fileBuffer, fileName, 'binary', ttlDays, isPublic);
}
/**
* Upload a buffer to Runloop as an object.
* Implements the three-step upload process:
* 1. Create object (get presigned URL)
* 2. Upload to presigned URL
* 3. Complete the upload
*/
async function uploadBuffer(
client: RunloopSDK,
buffer: Buffer,
objectName: string,
contentType: 'text' | 'binary' | 'gzip' | 'tar' | 'tgz',
ttlDays?: number,
isPublic?: boolean
): Promise<UploadResult> {
core.info(`Starting object upload: ${objectName} (${buffer.length} bytes, type: ${contentType})`);
// Step 1: Create object and get presigned URL
const ttlMs = ttlDays ? ttlDays * 24 * 60 * 60 * 1000 : undefined;
const createParams = {
name: objectName,
content_type: contentType,
metadata: {
source: 'github-action',
uploaded_at: new Date().toISOString(),
},
...(ttlMs && { ttl_ms: ttlMs }),
...(isPublic && { is_public: true }),
};
core.info('Creating object...');
const createdObject = await client.api.objects.create(createParams);
if (!createdObject.upload_url) {
throw new Error('Object creation did not return an upload URL');
}
core.info(`Object created: ${createdObject.id}`);
core.info(`Upload URL obtained: ${createdObject.upload_url.substring(0, 50)}...`);
// Step 2: Upload to presigned URL
core.info('Uploading to presigned URL...');
const uploadResponse = await fetch(createdObject.upload_url, {
method: 'PUT',
body: buffer,
headers: {
'Content-Type': getContentTypeHeader(contentType),
'Content-Length': buffer.length.toString(),
},
});
if (!uploadResponse.ok) {
const errorText = await uploadResponse.text();
throw new Error(
`Upload to presigned URL failed: ${uploadResponse.status} ${uploadResponse.statusText}\n${errorText}`
);
}
core.info('Upload successful');
// Step 3: Complete the upload
core.info('Completing object upload...');
const completedObject = await client.api.objects.complete(createdObject.id);
if (completedObject.state !== 'READ_ONLY') {
core.warning(`Object state is ${completedObject.state}, expected READ_ONLY`);
}
core.info(`Object upload completed: ${completedObject.id}`);
return {
objectId: completedObject.id,
objectName: objectName,
};
}
/**
* Determine content type based on file characteristics.
*/
export function determineContentType(
fileName: string,
buffer: Buffer
): 'text' | 'binary' | 'gzip' | 'tar' | 'tgz' {
// Check file extension first
if (fileName.endsWith('.tar.gz') || fileName.endsWith('.tgz')) {
return 'tgz';
}
if (fileName.endsWith('.tar')) {
return 'tar';
}
if (fileName.endsWith('.gz')) {
return 'gzip';
}
// Check if file is text by looking at content
// If it's mostly ASCII/UTF-8 printable characters, treat as text
const sample = buffer.slice(0, Math.min(1024, buffer.length));
let textBytes = 0;
for (const byte of sample) {
// Count printable ASCII and common whitespace
if ((byte >= 32 && byte <= 126) || byte === 9 || byte === 10 || byte === 13) {
textBytes++;
}
}
const textRatio = textBytes / sample.length;
return textRatio > 0.85 ? 'text' : 'binary';
}
/**
* Get HTTP Content-Type header for object upload.
*/
function getContentTypeHeader(contentType: string): string {
const contentTypeMap: Record<string, string> = {
text: 'text/plain',
binary: 'application/octet-stream',
gzip: 'application/gzip',
tar: 'application/x-tar',
tgz: 'application/gzip',
};
return contentTypeMap[contentType] || 'application/octet-stream';
}