Helper-esm / lib /uploader.js
Khrisna's picture
Update lib/uploader.js
a6c3c6f verified
raw
history blame
2.46 kB
import axios from "axios";
import fetch from "node-fetch";
import FormData from "form-data";
import {fileTypeFromBuffer} from 'file-type';
async function Uploader(input) {
try {
if(typeof input == "string") {
// data
const resp = await fetch(input);
const images = await resp.arrayBuffer();
const types = await fileTypeFromBuffer(images);
const filenames = "images_" + Math.floor(1000 + Math.random() * 9000) + "." types.ext;
// memasukan data ke formdata
const form = new FormData();
form.append("image", Buffer.from(images, "binary"), {
filename: filenames,
contentType: types.mime
});
form.append("pageTitle", "Tạo ảnh Anime AI theo ảnh của bạn");
form.append("pageURL", "https://taoanhdep.com/tao-anh-anime-ai-theo-anh-cua-ban/");
const res = await axios({
method: "POST",
url: "https://phimtat.vn/up/upload.php",
data: form,
headers: {
...form.getHeaders(),
"User-Agent": "okhttp/4.9.0",
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary9xpHSK9nPN4BEVJa",
"Origin": "https://taoanhdep.com"
}
});
return res.data;
} else if(typeof input == "object") {
// data
const types = await fileTypeFromBuffer(input);
const filenames = "images_" + Math.floor(1000 + Math.random() * 9000) + "." + types.ext;
// memasukan data ke formdata
const form = new FormData();
form.append("image", Buffer.from(input, "binary"), {
filename: filenames,
contentType: types.mime
});
form.append("pageTitle", "Tạo ảnh Anime AI theo ảnh của bạn");
form.append("pageURL", "https://taoanhdep.com/tao-anh-anime-ai-theo-anh-cua-ban/");
const res = await axios({
method: "POST",
url: "https://phimtat.vn/up/upload.php",
data: form,
headers: {
...form.getHeaders(),
"User-Agent": "okhttp/4.9.0",
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary9xpHSK9nPN4BEVJa",
"Origin": "https://taoanhdep.com"
}
});
return res.data;
}
} catch(e) {
return e.message;
}
}
export { Uploader }