MCPcopy Create free account
hub / github.com/Xchristech2/Zenitsu-Bot / uploadImage

Function uploadImage

lib/uploadImage.js:16–73  ·  view source on GitHub ↗

* Upload file to qu.ax * Supported mimetypes: * - `image/jpeg` * - `image/jpg` * - `image/png` * @param {Buffer} buffer File Buffer * @return {Promise }

(buffer)

Source from the content-addressed store, hash-verified

14 * @return {Promise<string>}
15 */
16async function uploadImage(buffer) {
17 try {
18 // Create temp directory if it doesn't exist
19 const tmpDir = path.join(process.cwd(), 'tmp');
20 if (!fs.existsSync(tmpDir)) {
21 fs.mkdirSync(tmpDir, { recursive: true });
22 }
23
24 // Get file type
25 const fileType = await FileType.fromBuffer(buffer);
26 const { ext, mime } = fileType || { ext: 'png', mime: 'image/png' };
27 const tempFile = path.join(tmpDir, `temp_${Date.now()}.${ext}`);
28
29 // Save buffer to temp file
30 fs.writeFileSync(tempFile, buffer);
31
32 // Create form data
33 const form = new FormData();
34 form.append('files[]', fs.createReadStream(tempFile));
35
36 // Upload to qu.ax
37 const response = await fetch('https://qu.ax/upload.php', {
38 method: 'POST',
39 body: form,
40 headers: form.getHeaders()
41 });
42
43 // Clean up temp file
44 fs.unlinkSync(tempFile);
45
46 const result = await response.json();
47 if (result && result.success) {
48 return result.files[0].url;
49 } else {
50 // Fallback to telegraph if qu.ax fails
51 const telegraphForm = new FormData();
52 telegraphForm.append('file', buffer, {
53 filename: `upload.${ext}`,
54 contentType: mime
55 });
56
57 const telegraphResponse = await fetch('https://telegra.ph/upload', {
58 method: 'POST',
59 body: telegraphForm
60 });
61
62 const img = await telegraphResponse.json();
63 if (img[0]?.src) {
64 return 'https://telegra.ph' + img[0].src;
65 }
66
67 throw new Error('Failed to upload image to both services');
68 }
69 } catch (error) {
70 console.error('Upload error:', error);
71 throw error;
72 }
73}

Callers 3

getQuotedOrOwnImageUrlFunction · 0.85
getQuotedOrOwnImageUrlFunction · 0.85
getQuotedOrOwnImageUrlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected