From 5f9f1ceeba9cba070eef4e439e2cc5960fef5b64 Mon Sep 17 00:00:00 2001 From: Tben Date: Thu, 10 Aug 2023 16:01:16 +0100 Subject: [PATCH] updates --- package.json | 2 +- utils/upload-file.js | 101 ++++++++++++++++++++++++------------------ utils/upload-image.js | 101 ++++++++++++++++++++++++------------------ 3 files changed, 117 insertions(+), 87 deletions(-) diff --git a/package.json b/package.json index c7eb93e..c374174 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.4.4", + "version": "1.4.5", "description": "Cloud-based SQL data management tool", "main": "index.js", "scripts": { diff --git a/utils/upload-file.js b/utils/upload-file.js index 52c0124..17cee84 100644 --- a/utils/upload-file.js +++ b/utils/upload-file.js @@ -18,6 +18,7 @@ const https = require("https"); * @property {{ * urlPath: string, * }} payload - Payload containing the url for the image and its thumbnail + * @property {string} [msg] - An optional message */ /** @@ -39,58 +40,72 @@ const https = require("https"); * @returns { Promise } - Return Object */ async function uploadImage({ key, payload }) { - /** - * Make https request - * - * @description make a request to datasquirel.com - */ - const httpResponse = await new Promise((resolve, reject) => { - const reqPayload = JSON.stringify(payload); + try { + /** + * Make https request + * + * @description make a request to datasquirel.com + */ + const httpResponse = await new Promise((resolve, reject) => { + const reqPayload = JSON.stringify(payload); - const httpsRequest = https.request( - { - method: "POST", - headers: { - "Content-Type": "application/json", - "Content-Length": Buffer.from(reqPayload).length, - Authorization: key, + const httpsRequest = https.request( + { + method: "POST", + headers: { + "Content-Type": "application/json", + "Content-Length": Buffer.from(reqPayload).length, + Authorization: key, + }, + port: 443, + hostname: "datasquirel.com", + path: `/api/query/add-file`, }, - port: 443, - hostname: "datasquirel.com", - path: `/api/query/add-file`, - }, - /** - * Callback Function - * - * @description https request callback - */ - (response) => { - var str = ""; + /** + * Callback Function + * + * @description https request callback + */ + (response) => { + var str = ""; - response.on("data", function (chunk) { - str += chunk; - }); + response.on("data", function (chunk) { + str += chunk; + }); - response.on("end", function () { - resolve(JSON.parse(str)); - }); + response.on("end", function () { + resolve(JSON.parse(str)); + }); - response.on("error", (err) => { - reject(err); - }); - } - ); + response.on("error", (err) => { + reject(err); + }); + } + ); - httpsRequest.write(reqPayload); - httpsRequest.end(); - }); + httpsRequest.write(reqPayload); + httpsRequest.end(); + }); - /** ********************************************** */ - /** ********************************************** */ - /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ - return httpResponse; + return httpResponse; + } catch (error) { + /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ + + console.log("Error in uploading file: ", error.message); + + return { + success: false, + payload: null, + msg: error.message, + }; + } } /** ********************************************** */ diff --git a/utils/upload-image.js b/utils/upload-image.js index 1fbe5b8..0c480ac 100644 --- a/utils/upload-image.js +++ b/utils/upload-image.js @@ -19,6 +19,7 @@ const https = require("https"); * urlPath: string, * urlThumbnailPath: string * }} payload - Payload containing the url for the image and its thumbnail + * @property {string} [msg] - An optional message */ /** @@ -41,58 +42,72 @@ const https = require("https"); * @returns { Promise } - Return Object */ async function uploadImage({ key, payload }) { - /** - * Make https request - * - * @description make a request to datasquirel.com - */ - const httpResponse = await new Promise((resolve, reject) => { - const reqPayload = JSON.stringify(payload); + try { + /** + * Make https request + * + * @description make a request to datasquirel.com + */ + const httpResponse = await new Promise((resolve, reject) => { + const reqPayload = JSON.stringify(payload); - const httpsRequest = https.request( - { - method: "POST", - headers: { - "Content-Type": "application/json", - "Content-Length": Buffer.from(reqPayload).length, - Authorization: key, + const httpsRequest = https.request( + { + method: "POST", + headers: { + "Content-Type": "application/json", + "Content-Length": Buffer.from(reqPayload).length, + Authorization: key, + }, + port: 443, + hostname: "datasquirel.com", + path: `/api/query/add-image`, }, - port: 443, - hostname: "datasquirel.com", - path: `/api/query/add-image`, - }, - /** - * Callback Function - * - * @description https request callback - */ - (response) => { - var str = ""; + /** + * Callback Function + * + * @description https request callback + */ + (response) => { + var str = ""; - response.on("data", function (chunk) { - str += chunk; - }); + response.on("data", function (chunk) { + str += chunk; + }); - response.on("end", function () { - resolve(JSON.parse(str)); - }); + response.on("end", function () { + resolve(JSON.parse(str)); + }); - response.on("error", (err) => { - reject(err); - }); - } - ); + response.on("error", (err) => { + reject(err); + }); + } + ); - httpsRequest.write(reqPayload); - httpsRequest.end(); - }); + httpsRequest.write(reqPayload); + httpsRequest.end(); + }); - /** ********************************************** */ - /** ********************************************** */ - /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ - return httpResponse; + return httpResponse; + } catch (error) { + /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ + + console.log("Error in uploading image: ", error.message); + + return { + success: false, + payload: null, + msg: error.message, + }; + } } /** ********************************************** */