Skip to content

Instantly share code, notes, and snippets.

@schani
Created January 7, 2026 12:59
Show Gist options
  • Select an option

  • Save schani/9403f442d97dc3f45abc7c20c3d624e0 to your computer and use it in GitHub Desktop.

Select an option

Save schani/9403f442d97dc3f45abc7c20c3d624e0 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
// Replace these with your actual values
const APP_ID = "YOUR-APP-ID";
const API_KEY = "YOU-API-KEY";
const BASE_URL = "https://api.glideapps.com";
async function createUpload(contentType, contentLength, fileName) {
const response = await fetch(`${BASE_URL}/apps/${APP_ID}/uploads`, {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
contentType,
contentLength,
fileName,
}),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to create upload: ${response.status} ${text}`);
}
return response.json();
}
async function uploadFile(uploadLocation, fileBuffer, contentType) {
const response = await fetch(uploadLocation, {
method: "PUT",
headers: {
"Content-Type": contentType,
},
body: fileBuffer,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to upload file: ${response.status} ${text}`);
}
return response;
}
async function completeUpload(uploadID) {
const response = await fetch(
`${BASE_URL}/apps/${APP_ID}/uploads/${uploadID}/complete`,
{
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
},
}
);
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to complete upload: ${response.status} ${text}`);
}
return response.json();
}
async function downloadFile(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to download file: ${response.status}`);
}
const arrayBuffer = await response.arrayBuffer();
return Buffer.from(arrayBuffer);
}
async function testFileUpload(filePath) {
console.log(`Testing file upload with: ${filePath}`);
// Read the file
const fileBuffer = fs.readFileSync(filePath);
const fileName = path.basename(filePath);
const contentLength = fileBuffer.length;
// Determine content type based on extension
const ext = path.extname(filePath).toLowerCase();
const contentTypes = {
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".pdf": "application/pdf",
".txt": "text/plain",
".json": "application/json",
};
const contentType = contentTypes[ext] || "application/octet-stream";
console.log(`File: ${fileName}`);
console.log(`Size: ${contentLength} bytes`);
console.log(`Content-Type: ${contentType}`);
// Step 1: Create upload session
console.log("\n1. Creating upload session...");
const createResponse = await createUpload(contentType, contentLength, fileName);
console.log("Upload session created:", createResponse);
const { uploadID, uploadLocation } = createResponse.data;
// Step 2: Upload file to pre-signed URL
console.log("\n2. Uploading file to storage...");
await uploadFile(uploadLocation, fileBuffer, contentType);
console.log("File uploaded successfully");
// Step 3: Complete the upload
console.log("\n3. Completing upload...");
const completeResponse = await completeUpload(uploadID);
console.log("Upload completed:", completeResponse);
const publicUrl = completeResponse.data?.url;
// Step 4: Download and verify
console.log("\n4. Downloading file to verify...");
const downloadedBuffer = await downloadFile(publicUrl);
console.log(`Downloaded ${downloadedBuffer.length} bytes`);
// Step 5: Compare buffers
console.log("\n5. Verifying file integrity...");
if (fileBuffer.equals(downloadedBuffer)) {
console.log("✓ Verification PASSED: Downloaded file matches original");
} else {
console.log("✗ Verification FAILED: Downloaded file differs from original");
console.log(` Original size: ${fileBuffer.length}`);
console.log(` Downloaded size: ${downloadedBuffer.length}`);
throw new Error("File verification failed");
}
return completeResponse;
}
// Main
const filePath = process.argv[2];
if (!filePath) {
console.log("Usage: node test-file-upload.js <file-path>");
console.log("Example: node test-file-upload.js ./test-image.png");
process.exit(1);
}
if (!fs.existsSync(filePath)) {
console.error(`File not found: ${filePath}`);
process.exit(1);
}
testFileUpload(filePath)
.then((result) => {
console.log("\nSuccess! Public URL:", result.data?.url || result);
})
.catch((err) => {
console.error("\nError:", err.message);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment