Loading snippets...
const fs = require("fs");
const path = require("path");
const https = require("https");
const CREDIT = "ShanMolvyr";
const API_URL = "https://cdn.faddlaninco.dev/api/upload";
const MAX_SIZE = 4.5 * 1024 * 1024;
function upload(filePath) {
return new Promise((resolve, reject) => {
const abs = path.resolve(filePath);
if (!fs.existsSync(abs)) return reject(new Error(`File not found: ${abs}`));
const size = fs.statSync(abs).size;
if (size > MAX_SIZE) {
return reject(new Error(`File too large (${(size / 1024 / 1024).toFixed(2)} MB), max ~4.5 MB`));
}
const fileName = path.basename(abs);
const boundary = "----FormBoundary" + Math.random().toString(16).slice(2);
const head = `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="${fileName}"\r\nContent-Type: application/octet-stream\r\n\r\n`;
const tail = `\r\n--${boundary}--\r\n`;
const url = new URL(API_URL);
const options = {
hostname: url.hostname,
path: url.pathname,
method: "POST",
headers: {
"Content-Type": `multipart/form-data; boundary=${boundary}`,
"Transfer-Encoding": "chunked",
},
};
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
try {
resolve(JSON.parse(data));
} catch {
reject(new Error("Invalid JSON response: " + data.slice(0, 300)));
}
});
});
req.on("error", reject);
req.write(head);
const stream = fs.createReadStream(abs);
stream.on("data", (chunk) => req.write(chunk));
stream.on("end", () => {
req.write(tail);
req.end();
});
stream.on("error", reject);
});
}
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log(
JSON.stringify({ error: "No file specified", credit: CREDIT }, null, 2)
);
process.exit(1);
}
const results = [];
for (const filePath of args) {
try {
const res = await upload(filePath);
results.push({
file: path.basename(filePath),
success: res.success ?? false,
url: res.url ?? null,
});
} catch (err) {
results.push({
file: path.basename(filePath),
success: false,
error: err.message,
});
}
}
const output =
results.length === 1
? { ...results[0], credit: CREDIT }
: { results, credit: CREDIT };
console.log(JSON.stringify(output, null, 2));
}
main();
bashnode nexcdn.js gambarnya.png
bashnode nexcdn.js gambar.png gambar.webp