Skip to content

Commit 70becc0

Browse files
work
1 parent 5198176 commit 70becc0

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

scripts/uploader.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,43 +99,49 @@ function makeRequest(url, options, data) {
9999
}
100100

101101
/**
102-
* Upload a single chunk
102+
* Upload a single chunk using multipart/form-data with raw binary data
103103
*/
104104
async function uploadChunk(filePath, fileName, chunkIndex, chunkData, totalChunks, fileHash) {
105-
// Create form data for application/x-www-form-urlencoded
105+
// Create multipart form data with raw binary data (more efficient than base64)
106+
const boundary = '----WebKitFormBoundary' + Math.random().toString(36).substring(2, 15);
107+
106108
const formFields = {
107109
'api_key': API_KEY,
108110
'file_name': fileName,
109111
'chunk_index': chunkIndex.toString(),
110112
'total_chunks': totalChunks.toString(),
111-
'file_hash': fileHash,
112-
'chunk_data': chunkData.toString('base64')
113+
'file_hash': fileHash
113114
};
114115

115-
// Validate all required fields
116-
const requiredFields = ['api_key', 'file_name', 'chunk_index', 'total_chunks', 'file_hash', 'chunk_data'];
117-
for (const field of requiredFields) {
118-
if (!formFields[field] || formFields[field] === '') {
119-
throw new Error(`Missing or empty field: ${field}`);
120-
}
121-
}
116+
// Build multipart form data manually
117+
let formData = Buffer.alloc(0);
122118

123-
// Create URLSearchParams for proper form encoding
124-
const formData = new URLSearchParams();
119+
// Add text fields
125120
for (const [key, value] of Object.entries(formFields)) {
126-
formData.append(key, value);
121+
const fieldHeader = `--${boundary}\r\nContent-Disposition: form-data; name="${key}"\r\n\r\n${value}\r\n`;
122+
formData = Buffer.concat([formData, Buffer.from(fieldHeader)]);
127123
}
128124

129-
const formDataString = formData.toString();
130-
console.log(`Chunk ${chunkIndex + 1}/${totalChunks}: ${(chunkData.length / 1024 / 1024).toFixed(2)}MB`);
125+
// Add binary chunk data
126+
const binaryHeader = `--${boundary}\r\nContent-Disposition: form-data; name="chunk_data"; filename="chunk_${chunkIndex}"\r\nContent-Type: application/octet-stream\r\n\r\n`;
127+
const binaryFooter = `\r\n--${boundary}--\r\n`;
128+
129+
formData = Buffer.concat([
130+
formData,
131+
Buffer.from(binaryHeader),
132+
chunkData,
133+
Buffer.from(binaryFooter)
134+
]);
135+
136+
console.log(`📦 Chunk ${chunkIndex + 1}/${totalChunks}: ${(chunkData.length / 1024 / 1024).toFixed(2)}MB`);
131137

132138
const response = await makeRequest(UPLOAD_ENDPOINT, {
133139
method: 'POST',
134140
headers: {
135-
'Content-Type': 'application/x-www-form-urlencoded',
136-
'Content-Length': Buffer.byteLength(formDataString)
141+
'Content-Type': `multipart/form-data; boundary=${boundary}`,
142+
'Content-Length': formData.length
137143
}
138-
}, formDataString);
144+
}, formData);
139145

140146
if (response.status !== 200) {
141147
throw new Error(`HTTP ${response.status}: ${JSON.stringify(response.data)}`);
@@ -210,12 +216,12 @@ function findFilesToUpload(directory) {
210216
files.push({ path: fullPath, type: 'macos-app' });
211217
}
212218
} else if (item.isDirectory()) {
213-
// macOS app files (in mac/ directory)
219+
// macOS app files (in mac/ directory) - only add if zip doesn't already exist
214220
if (item.name === 'mac') {
215-
// Zip the entire mac directory for upload
216221
const macZipPath = path.join(directory, 'Sploder-macOS.zip');
217222
if (fs.existsSync(macZipPath)) {
218-
files.push({ path: macZipPath, type: 'macos-app' });
223+
// Zip file already exists, don't add the directory
224+
console.log(`📝 Note: Using existing ${macZipPath} instead of mac directory`);
219225
} else {
220226
console.log(`📝 Note: macOS app directory found but no zip file. Consider creating ${macZipPath}`);
221227
}

0 commit comments

Comments
 (0)