Skip to content

Commit 37928b3

Browse files
authored
Merge pull request #47 from reynaldichernando/push
Update `/batch` drivers call to use `puter.fs.upload`
2 parents 644e3d6 + bd63aef commit 37928b3

File tree

1 file changed

+24
-159
lines changed

1 file changed

+24
-159
lines changed

src/commands/files.js

Lines changed: 24 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -572,62 +572,17 @@ export async function createFile(args = []) {
572572
}
573573
}
574574

575-
// Step 4: Create the file using /batch
576-
const operationId = crypto.randomUUID(); // Generate a unique operation ID
577-
const socketId = 'undefined'; // Placeholder socket ID
578-
const boundary = `----WebKitFormBoundary${crypto.randomUUID().replace(/-/g, '')}`;
575+
// Step 4: Create the file
579576
const fileBlob = new Blob([content || ''], { type: 'text/plain' });
580577

581-
const formData = `--${boundary}\r\n` +
582-
`Content-Disposition: form-data; name="operation_id"\r\n\r\n${operationId}\r\n` +
583-
`--${boundary}\r\n` +
584-
`Content-Disposition: form-data; name="socket_id"\r\n\r\n${socketId}\r\n` +
585-
`--${boundary}\r\n` +
586-
`Content-Disposition: form-data; name="original_client_socket_id"\r\n\r\n${socketId}\r\n` +
587-
`--${boundary}\r\n` +
588-
`Content-Disposition: form-data; name="fileinfo"\r\n\r\n${JSON.stringify({
589-
name: fileName,
590-
type: 'text/plain',
591-
size: fileBlob.size
592-
})}\r\n` +
593-
`--${boundary}\r\n` +
594-
`Content-Disposition: form-data; name="operation"\r\n\r\n${JSON.stringify({
595-
op: 'write',
596-
dedupe_name: dedupeName,
597-
overwrite: overwrite,
598-
operation_id: operationId,
599-
path: dirName,
600-
name: fileName,
601-
item_upload_id: 0
602-
})}\r\n` +
603-
`--${boundary}\r\n` +
604-
`Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n` +
605-
`Content-Type: text/plain\r\n\r\n${content || ''}\r\n` +
606-
`--${boundary}--\r\n`;
607-
608-
// Send the request
609-
const createResponse = await fetch(`${API_BASE}/batch`, {
610-
method: 'POST',
611-
headers: getHeaders(`multipart/form-data; boundary=${boundary}`),
612-
body: formData
578+
const createData = await puter.fs.upload(fileBlob, dirName, {
579+
overwrite: overwrite,
580+
dedupeName: dedupeName,
581+
name: fileName
613582
});
614-
615-
if (!createResponse.ok) {
616-
const errorText = await createResponse.text();
617-
console.error(chalk.red(`Failed to create file. Server response: ${errorText}. status: ${createResponse.status}`));
618-
return false;
619-
}
620-
621-
const createData = await createResponse.json();
622-
if (createData && createData.results && createData.results.length > 0) {
623-
const file = createData.results[0];
624-
console.log(chalk.green(`File "${filePath}" created successfully!`));
625-
console.log(chalk.dim(`Path: ${file.path}`));
626-
console.log(chalk.dim(`UID: ${file.uid}`));
627-
} else {
628-
console.error(chalk.red('Failed to create file. Invalid response from server.'));
629-
return false;
630-
}
583+
console.log(chalk.green(`File "${createData.name}" created successfully!`));
584+
console.log(chalk.dim(`Path: ${createData.path}`));
585+
console.log(chalk.dim(`UID: ${createData.uid}`));
631586
} catch (error) {
632587
console.error(chalk.red(`Failed to create file.\nError: ${error.message}`));
633588
return false;
@@ -711,63 +666,17 @@ export async function uploadFile(args = []) {
711666
// Step 3: Upload each file
712667
for (const filePath of files) {
713668
const fileName = path.basename(filePath);
714-
const fileContent = fs.readFileSync(filePath, 'utf8');
715-
716-
// Prepare the upload request
717-
const operationId = crypto.randomUUID(); // Generate a unique operation ID
718-
const socketId = 'undefined'; // Placeholder socket ID
719-
const boundary = `----WebKitFormBoundary${crypto.randomUUID().replace(/-/g, '')}`;
720-
721-
// Prepare FormData
722-
const formData = `--${boundary}\r\n` +
723-
`Content-Disposition: form-data; name="operation_id"\r\n\r\n${operationId}\r\n` +
724-
`--${boundary}\r\n` +
725-
`Content-Disposition: form-data; name="socket_id"\r\n\r\n${socketId}\r\n` +
726-
`--${boundary}\r\n` +
727-
`Content-Disposition: form-data; name="original_client_socket_id"\r\n\r\n${socketId}\r\n` +
728-
`--${boundary}\r\n` +
729-
`Content-Disposition: form-data; name="fileinfo"\r\n\r\n${JSON.stringify({
730-
name: fileName,
731-
type: 'text/plain',
732-
size: Buffer.byteLength(fileContent, 'utf8')
733-
})}\r\n` +
734-
`--${boundary}\r\n` +
735-
`Content-Disposition: form-data; name="operation"\r\n\r\n${JSON.stringify({
736-
op: 'write',
737-
dedupe_name: dedupeName,
738-
overwrite: overwrite,
739-
operation_id: operationId,
740-
path: remotePath,
741-
name: fileName,
742-
item_upload_id: 0
743-
})}\r\n` +
744-
`--${boundary}\r\n` +
745-
`Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n` +
746-
`Content-Type: text/plain\r\n\r\n${fileContent}\r\n` +
747-
`--${boundary}--\r\n`;
748-
749-
// Send the upload request
750-
const uploadResponse = await fetch(`${API_BASE}/batch`, {
751-
method: 'POST',
752-
headers: getHeaders(`multipart/form-data; boundary=${boundary}`),
753-
body: formData
754-
});
669+
const fileContent = fs.readFileSync(filePath);
670+
const blob = new Blob([fileContent]);
755671

756-
if (!uploadResponse.ok) {
757-
const errorText = await uploadResponse.text();
758-
console.error(chalk.red(`Failed to upload file "${fileName}". Server response: ${errorText}`));
759-
continue;
760-
}
761-
762-
const uploadData = await uploadResponse.json();
763-
if (uploadData && uploadData.results && uploadData.results.length > 0) {
764-
const file = uploadData.results[0];
765-
console.log(chalk.green(`File "${fileName}" uploaded successfully!`));
766-
console.log(chalk.dim(`Path: ${file.path}`));
767-
console.log(chalk.dim(`UID: ${file.uid}`));
768-
} else {
769-
console.error(chalk.red(`Failed to upload file "${fileName}". Invalid response from server.`));
770-
}
672+
const uploadData = await puter.fs.upload(blob, remotePath, {
673+
overwrite: overwrite,
674+
dedupeName: dedupeName,
675+
name: fileName
676+
});
677+
console.log(chalk.green(`File "${uploadData.name}" uploaded successfully!`));
678+
console.log(chalk.dim(`Path: ${uploadData.path}`));
679+
console.log(chalk.dim(`UID: ${uploadData.uid}`));
771680
}
772681
} catch (error) {
773682
console.error(chalk.red(`Failed to upload files.\nError: ${error.message}`));
@@ -1268,6 +1177,7 @@ export async function editFile(args = []) {
12681177

12691178
// Read the updated content after editor closes
12701179
const updatedContent = fs.readFileSync(tempFilePath, 'utf8');
1180+
const blob = new Blob([updatedContent]);
12711181
console.log(chalk.cyan('Uploading changes...'));
12721182

12731183
// Step 7: Upload the updated file content
@@ -1280,60 +1190,15 @@ export async function editFile(args = []) {
12801190
}
12811191

12821192
// Step 7.2: Uploading the updated file
1283-
const operationId = crypto.randomUUID(); // Generate a unique operation ID
1284-
const socketId = 'undefined'; // Placeholder socket ID
1285-
const boundary = `----WebKitFormBoundary${crypto.randomUUID().replace(/-/g, '')}`;
12861193
const fileName = path.basename(filePath);
12871194
const dirName = path.dirname(filePath);
12881195

1289-
// Prepare FormData
1290-
const formData = `--${boundary}\r\n` +
1291-
`Content-Disposition: form-data; name="operation_id"\r\n\r\n${operationId}\r\n` +
1292-
`--${boundary}\r\n` +
1293-
`Content-Disposition: form-data; name="socket_id"\r\n\r\n${socketId}\r\n` +
1294-
`--${boundary}\r\n` +
1295-
`Content-Disposition: form-data; name="original_client_socket_id"\r\n\r\n${socketId}\r\n` +
1296-
`--${boundary}\r\n` +
1297-
`Content-Disposition: form-data; name="fileinfo"\r\n\r\n${JSON.stringify({
1298-
name: fileName,
1299-
type: 'text/plain',
1300-
size: Buffer.byteLength(updatedContent, 'utf8')
1301-
})}\r\n` +
1302-
`--${boundary}\r\n` +
1303-
`Content-Disposition: form-data; name="operation"\r\n\r\n${JSON.stringify({
1304-
op: 'write',
1305-
dedupe_name: false,
1306-
overwrite: true,
1307-
operation_id: operationId,
1308-
path: dirName,
1309-
name: fileName,
1310-
item_upload_id: 0
1311-
})}\r\n` +
1312-
`--${boundary}\r\n` +
1313-
`Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n` +
1314-
`Content-Type: text/plain\r\n\r\n${updatedContent}\r\n` +
1315-
`--${boundary}--\r\n`;
1316-
1317-
// Send the upload request
1318-
const uploadResponse = await fetch(`${API_BASE}/batch`, {
1319-
method: 'POST',
1320-
headers: getHeaders(`multipart/form-data; boundary=${boundary}`),
1321-
body: formData
1196+
const uploadData = await puter.fs.upload(blob, dirName, {
1197+
overwrite: true,
1198+
dedupeName: false,
1199+
name: fileName
13221200
});
1323-
1324-
if (!uploadResponse.ok) {
1325-
const errorText = await uploadResponse.text();
1326-
console.log(chalk.red(`Failed to save file. Server response: ${errorText}`));
1327-
return;
1328-
}
1329-
1330-
const uploadData = await uploadResponse.json();
1331-
if (uploadData && uploadData.results && uploadData.results.length > 0) {
1332-
const file = uploadData.results[0];
1333-
console.log(chalk.green(`File saved: ${file.path}`));
1334-
} else {
1335-
console.log(chalk.red('Failed to save file. Invalid response from server.'));
1336-
}
1201+
console.log(chalk.green(`File saved: ${uploadData.path}`));
13371202
} catch (error) {
13381203
if (error.status === 130) {
13391204
// This is a SIGINT (Ctrl+C), which is normal for some editors

0 commit comments

Comments
 (0)