datasquirel/utils/upload-image.js

102 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-05-03 06:16:29 +00:00
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
2023-05-22 08:23:21 +00:00
/**
* @typedef {Object} FunctionReturn
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* urlThumbnailPath: string
* }} payload - Payload containing the url for the image and its thumbnail
*/
2023-05-03 06:16:29 +00:00
/**
* ==============================================================================
* Main Function
* ==============================================================================
2023-05-22 08:24:08 +00:00
* @async
*
2023-05-22 08:23:21 +00:00
* @param {Object} params - API Key
* @param {String} params.key - API Key
* @param {{
* imageData: string,
* imageName: string,
* mimeType: [string],
* thumbnailSize: [number],
* folder: [string],
* }} params.payload - Image Data Eg.
*
* @returns { Promise<FunctionReturn> } - Return Object
2023-05-03 06:16:29 +00:00
*/
2023-07-07 19:13:13 +00:00
async function uploadImage({ key, payload }) {
2023-05-03 06:16:29 +00:00
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
2023-05-06 12:18:44 +00:00
const reqPayload = JSON.stringify(payload);
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
2023-05-22 10:52:27 +00:00
"Content-Length": Buffer.from(reqPayload).length,
2023-05-06 12:18:44 +00:00
Authorization: key,
2023-05-03 06:16:29 +00:00
},
2023-05-06 12:18:44 +00:00
port: 443,
hostname: "datasquirel.com",
2023-05-12 14:54:58 +00:00
path: `/api/query/add-image`,
2023-05-06 12:18:44 +00:00
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
2023-05-03 06:16:29 +00:00
2023-05-06 12:18:44 +00:00
response.on("data", function (chunk) {
str += chunk;
});
2023-05-03 06:16:29 +00:00
2023-05-06 12:18:44 +00:00
response.on("end", function () {
resolve(JSON.parse(str));
});
2023-05-03 06:16:29 +00:00
2023-05-06 12:18:44 +00:00
response.on("error", (err) => {
reject(err);
});
}
);
2023-05-03 06:16:29 +00:00
2023-05-06 12:18:44 +00:00
httpsRequest.write(reqPayload);
httpsRequest.end();
2023-05-03 06:16:29 +00:00
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
2023-07-07 19:13:13 +00:00
}
2023-05-03 06:16:29 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
2023-07-07 19:13:13 +00:00
module.exports = uploadImage;