diff --git a/README.md b/README.md index 2f3e9c1..e0871b8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Chorddb is a simple, lightweight package which is a database like MongoDB which uses Discord as storage with encryption. It works with JSON data. -**NEXT UPDATE**: Image Upload (Will only work with Buffer) +**NEXT UPDATE**: Docs for image upload. # Docs diff --git a/package-lock.json b/package-lock.json index 41304d5..613d374 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { - "name": "chord-db", + "name": "chorddb", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "chord-db", + "name": "chorddb", "version": "1.0.0", "license": "MIT", "dependencies": { "aes256": "^1.1.0", - "axios": "^1.11.0" + "axios": "^1.11.0", + "form-data": "^4.0.5" } }, "node_modules/aes256": { @@ -150,9 +151,9 @@ } }, "node_modules/form-data": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.4.tgz", - "integrity": "sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==", + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz", + "integrity": "sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==", "license": "MIT", "dependencies": { "asynckit": "^0.4.0", diff --git a/package.json b/package.json index d0b2a72..a0d0034 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "license": "MIT", "dependencies": { "aes256": "^1.1.0", - "axios": "^1.11.0" + "axios": "^1.11.0", + "form-data": "^4.0.5" } } diff --git a/src/errors.js b/src/errors.js index d14c608..0f0fcb3 100644 --- a/src/errors.js +++ b/src/errors.js @@ -23,6 +23,13 @@ class InvalidChannelIdError extends Error { } } +class InvalidImageChannel extends Error { + constructor() { + super("Invalid Images channel Id"); + this.name = "InvalidImageChannel"; + } +} + class ChordDBNotStartedError extends Error { constructor() { super("ChordDB not started"); @@ -35,4 +42,5 @@ module.exports = { InvalidChannelIdError, InvalidTokenError, ChordDBNotStartedError, + InvalidImageChannel, }; diff --git a/src/main.js b/src/main.js index 2e5c73c..91fc061 100644 --- a/src/main.js +++ b/src/main.js @@ -1,19 +1,29 @@ // Main \\ const { encrypt, setup, decrypt, dc_call } = require("./functions"); +const FormData = require("form-data"); const { MessageTooLargeError, InvalidTokenError, InvalidChannelIdError, ChordDBNotStartedError, + InvalidImageChannel, } = require("./errors"); class UDB { - constructor(token, encryption_key, channel_id) { + constructor( + token, + encryption_key, + channel_id, + img_channel = null, + showChorddbMessage = true, + ) { this.token = token; this.enc_key = encryption_key; this.ch_id = channel_id; this.isStarted = false; + this.images = img_channel; + this.debugShow = showChorddbMessage; } async start() { @@ -23,24 +33,30 @@ class UDB { const channel_info = await dc_call(`/channels/${this.ch_id}`); if (user_info.id) { - console.log( - `\x1b[32m chorddb: connected as ${user_info.username}\x1b[0m`, - ); + if (this.debugShow) { + console.log( + `\x1b[32m chorddb: connected as ${user_info.username}\x1b[0m`, + ); + } } else { console.error(new InvalidTokenError()); process.exit(); } if (channel_info.id) { - console.log( - `\x1b[32m chorddb: linked to channel ${channel_info.name}\x1b[0m`, - ); + if (this.debugShow) { + console.log( + `\x1b[32m chorddb: linked to channel ${channel_info.name}\x1b[0m`, + ); + } } else { console.error(new InvalidChannelIdError()); process.exit(); } - console.log("chorddb: ChordDB is Ready."); + if (this.debugShow) { + console.log("chorddb: ChordDB is Ready."); + } this.isStarted = true; } @@ -58,8 +74,7 @@ class UDB { const enc_data = encrypt(data); if (enc_data.length > 2000) { - console.error(new MessageTooLargeError()); - return true; + throw new MessageTooLargeError(); } const r = await dc_call(`/channels/${this.ch_id}/messages`, "POST", { @@ -147,17 +162,56 @@ class UDB { msgs = await dc_call(`/channels/${this.ch_id}/messages`); for (const msg of msgs) { - final.push(JSON.parse(decrypt(msg.content))); + final.push(JSON.parse(decrypt(msg.content.toString()))); } - if (final == []) { + if (final.length === 0) { return null; } else { return final; } } + + async sendImg(name, key, buffer) { + let Form = new FormData(); + this._checkStarted(); + + if (!this.images) { + throw new InvalidImageChannel(); + } + + Form.append("file", buffer, name); + Form.append( + "payload_json", + JSON.stringify({ + content: key, + }), + ); + + const res = await dc_call(`/channels/${this.ch_id}/messages`, "POST", Form); + + return res; + } + + async findImg(key) { + this._checkStarted(); + + if (!this.images) { + throw new InvalidImageChannel(); + } + + const res = await dc_call(`/channels/${this.ch_id}/messages`); + + if (res) { + for (const msg of res) { + if (msg.content === key) { + return msg.attachments[0]; + } + } + } else { + return null; + } + } } -module.exports = { - UDB, -}; +module.exports = UDB; diff --git a/tests/image.png b/tests/image.png new file mode 100644 index 0000000..f8262cf Binary files /dev/null and b/tests/image.png differ diff --git a/tests/maintest.js b/tests/maintest.js index 11a7bab..6ac32d3 100644 --- a/tests/maintest.js +++ b/tests/maintest.js @@ -1,10 +1,11 @@ -const { UDB } = require("../src/main"); +const path = require("path"); +const UDB = require("../src/main"); -const db = new UDB("TOKEN", "ENCRYPTION_KEY", "CHANNEL_ID"); - -db.start(); +const db = new UDB("TOKEN", "ENCRYPTION_KEY", "CHANNEL_ID", true); async function test() { + await db.start(); + console.log("\n--- TESTING WRITE ---"); const testData = { id: 1, name: "Test Item", value: 100 }; const writeResult = await db.write(testData); @@ -25,6 +26,20 @@ async function test() { console.log("\n--- VERIFY EDIT ---"); const verify = await db.find({ key: "id", value: 1 }); console.log("Updated item:", verify); + + console.log("\n --- Test Image Write ---"); + const imgWriteResult = await db.sendImg( + "verycoolimg.png", + "coolimage", + Buffer.from(path.join(__dirname, "/image.png")), + ); + + console.log(imgWriteResult); + + console.log("\n --- Test Image Write ---"); + const imgReadResult = await db.findImg("coolimage"); + + console.log(imgReadResult); } test();