From b74e2d8aa17ecfb856bd3bfacdde8c2a11dc9aa6 Mon Sep 17 00:00:00 2001 From: Tben Date: Thu, 10 Aug 2023 14:07:41 +0100 Subject: [PATCH] updates --- package.json | 2 +- users/get-token.js | 16 +------ utils/upload-file.js | 100 ++++++++++++++++++++++++++++++++++++++++++ utils/upload-image.js | 7 +-- 4 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 utils/upload-file.js diff --git a/package.json b/package.json index fa5660a..23528e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.4.2", + "version": "1.4.3", "description": "Cloud-based SQL data management tool", "main": "index.js", "scripts": { diff --git a/users/get-token.js b/users/get-token.js index 9a5a7f0..360109b 100644 --- a/users/get-token.js +++ b/users/get-token.js @@ -26,12 +26,11 @@ const parseCookies = require("../utils/functions/parseCookies"); * @param {http.IncomingMessage} params.request - Http request object * @param {string} params.encryptionKey - Encryption Key * @param {string} params.encryptionSalt - Encryption Salt - * @param {("deep" | "normal")?} [params.level] - Optional. "Deep" value indicates an extra layer of security * @param {string} params.database - Database Name * * @returns {{ key: string | undefined, csrf: string | undefined }} */ -function getToken({ request, encryptionKey, encryptionSalt, level, database }) { +function getToken({ request, encryptionKey, encryptionSalt, database }) { try { /** * Grab the payload @@ -81,19 +80,6 @@ function getToken({ request, encryptionKey, encryptionSalt, level, database }) { /** ********************************************** */ /** ********************************************** */ - /** - * Grab the payload - * - * @description Grab the payload - */ - if (level?.match(/deep/i) && !csrf?.match(new RegExp(`${userObject.csrf_k}`))) { - return { key: undefined, csrf: undefined }; - } - - /** ********************************************** */ - /** ********************************************** */ - /** ********************************************** */ - /** * Return User Object * diff --git a/utils/upload-file.js b/utils/upload-file.js new file mode 100644 index 0000000..52c0124 --- /dev/null +++ b/utils/upload-file.js @@ -0,0 +1,100 @@ +/** + * ============================================================================== + * Imports + * ============================================================================== + */ +const https = require("https"); + +/** ****************************************************************************** */ +/** ****************************************************************************** */ +/** ****************************************************************************** */ +/** ****************************************************************************** */ +/** ****************************************************************************** */ +/** ****************************************************************************** */ + +/** + * @typedef {Object} FunctionReturn + * @property {boolean} success - Did the function run successfully? + * @property {{ + * urlPath: string, + * }} payload - Payload containing the url for the image and its thumbnail + */ + +/** + * ============================================================================== + * Main Function + * ============================================================================== + * @async + * + * @param {Object} params - Single Param object containing params + * @param {String} params.key - *FULL ACCESS API Key + * @param {{ + * fileData: string, + * fileName: string, + * mimeType?: string, + * folder?: string, + * isPrivate?: boolean, + * }} params.payload - Image Data Eg. + * + * @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); + + 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`, + }, + + /** + * Callback Function + * + * @description https request callback + */ + (response) => { + var str = ""; + + response.on("data", function (chunk) { + str += chunk; + }); + + response.on("end", function () { + resolve(JSON.parse(str)); + }); + + response.on("error", (err) => { + reject(err); + }); + } + ); + + httpsRequest.write(reqPayload); + httpsRequest.end(); + }); + + /** ********************************************** */ + /** ********************************************** */ + /** ********************************************** */ + + return httpResponse; +} + +/** ********************************************** */ +/** ********************************************** */ +/** ********************************************** */ + +module.exports = uploadImage; diff --git a/utils/upload-image.js b/utils/upload-image.js index 138a7d3..1fbe5b8 100644 --- a/utils/upload-image.js +++ b/utils/upload-image.js @@ -32,9 +32,10 @@ const https = require("https"); * @param {{ * imageData: string, * imageName: string, - * mimeType: [string], - * thumbnailSize: [number], - * folder: [string], + * mimeType?: string, + * thumbnailSize?: number, + * folder?: string, + * isPrivate?: boolean, * }} params.payload - Image Data Eg. * * @returns { Promise } - Return Object