From 841c094030227c71d1fb68b62b07cc886aa0b6a0 Mon Sep 17 00:00:00 2001 From: Oisin Hickey <39494199+ironic833@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:59:52 +0000 Subject: [PATCH 1/3] Initial build - Added the base file to build on --- lib/azure.js | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/server.js | 2 + 2 files changed, 117 insertions(+) create mode 100644 lib/azure.js diff --git a/lib/azure.js b/lib/azure.js new file mode 100644 index 000000000..ac1d64f63 --- /dev/null +++ b/lib/azure.js @@ -0,0 +1,115 @@ +var Gun = require('../gun'); +var azureBlobStore; +let containerClient; + +const { Readable } = require('stream'); +const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob"); + +Gun.on('create', function(root) { + this.to.next(root); + var opt = root.opt; + if (!opt.azureBlob && !process.env.AZURE_BLOB_CONTAINER) { + return; + } + + try { + azureBlobStore = require('@azure/storage-blob'); + } catch (e) { + console.log("Please `npm install @azure/storage-blob` or add it to your package.json !"); + } + + var opts = opt.azureBlob || (opt.azureBlob = {}); + opts.accountName = opts.accountName || process.env.AZURE_BLOB_ACCOUNT_NAME; + opts.accountKey = opts.accountKey || process.env.AZURE_BLOB_ACCOUNT_KEY; + opts.containerName = opts.containerName || process.env.AZURE_BLOB_CONTIAINER_NAME; + + const storageAccountBaseUrl = `https://${opts.accountName}.blob.core.windows.net`, + sharedKeyCredential = new StorageSharedKeyCredential(opts.accountName, opts.accountKey); + + const blobServiceClient = new BlobServiceClient( + storageAccountBaseUrl, + sharedKeyCredential + ); + + containerClient = blobServiceClient.getContainerClient(opts.containerName); + + // Check if the container exists, if not, create it + containerClient.exists().then((exists) => { + if (!exists) { + containerClient.create(); + console.log(`Blob container ${opts.containerName} created successfully.`); + } + }).catch((error) => { + console.error("Error checking container existence:", error); + }); + + opt.store = Store(opt); +}); + +function Store(opt) { + opt = opt || {}; + opt.file = String(opt.file || 'radata'); + var opts = opt.azureBlob, + azureBlob = opts.azureBlob; + var c = { p: {}, g: {}, l: {} }; + + var store = function Store() {}; + if (Store[opt.file]) { + console.log("Warning: reusing same Azure Blob store and options as 1st."); + return Store[opt.file]; + } + Store[opt.file] = store; + + store.put = async function(file, data, cb) { + try { + const blobClient = containerClient.getBlockBlobClient(file); + const stream = Readable.from(data); + const uploadResponse = await blobClient.uploadStream(stream, data.length, undefined, undefined); + console.log("File uploaded successfully", uploadResponse); + cb(null, 'azure'); + } catch (error) { + console.error("Error uploading file:", error); + cb(error, null); + } + }; + + store.get = async function(file, cb) { + try { + const blobClient = containerClient.getBlockBlobClient(file); + const downloadResponse = await blobClient.download(0); + const downloadedData = []; + downloadResponse.readableStreamBody.on("data", (data) => { + downloadedData.push(data.toString()); + }); + downloadResponse.readableStreamBody.on("end", () => { + const data = downloadedData.join(''); + cb(null, data); + }); + downloadResponse.readableStreamBody.on("error", (err) => { + console.error("Error reading stream:", err); + cb(err, null); + }); + } catch (error) { + // console.error("Error downloading file:", error); + cb(error, null); + } + }; + + store.list = async function(cb) { + try { + const response = await containerClient.listBlobsFlat(); + const keys = []; + response.segment.blobItems.forEach((blob) => { + keys.push(blob.name); + }); + cb(keys); + } catch (error) { + //console.error("Error listing blobs:", error); + cb([]); + } + }; + + return store; +} + +module.exports = Store; diff --git a/lib/server.js b/lib/server.js index 17e40381e..d353047b6 100644 --- a/lib/server.js +++ b/lib/server.js @@ -15,6 +15,8 @@ require('./rfs'); require('./rs3'); require('./wire'); + require('./azure') + try{require('../sea');}catch(e){} try{require('../axe');}catch(e){} From d31de2004ddc5ca87f0581a4d23ff9fb9262b607 Mon Sep 17 00:00:00 2001 From: Oisin Hickey <39494199+ironic833@users.noreply.github.com> Date: Sat, 24 Feb 2024 20:55:31 +0000 Subject: [PATCH 2/3] Update azure.js --- lib/azure.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/azure.js b/lib/azure.js index ac1d64f63..bf6abc3ae 100644 --- a/lib/azure.js +++ b/lib/azure.js @@ -6,6 +6,7 @@ const { Readable } = require('stream'); const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob"); Gun.on('create', function(root) { + console.log("Please note this adapter is not fully working yet"); this.to.next(root); var opt = root.opt; if (!opt.azureBlob && !process.env.AZURE_BLOB_CONTAINER) { From 8e1fea794420c15312f3d43022664ec437fced4f Mon Sep 17 00:00:00 2001 From: Oisin Hickey <39494199+ironic833@users.noreply.github.com> Date: Sat, 24 Feb 2024 20:57:43 +0000 Subject: [PATCH 3/3] Update azure.js - Added comment explaining adapter development status --- lib/azure.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/azure.js b/lib/azure.js index bf6abc3ae..a79029adc 100644 --- a/lib/azure.js +++ b/lib/azure.js @@ -6,7 +6,7 @@ const { Readable } = require('stream'); const { BlobServiceClient, StorageSharedKeyCredential } = require("@azure/storage-blob"); Gun.on('create', function(root) { - console.log("Please note this adapter is not fully working yet"); + console.log("Please note this adapter for azure is not fully working yet"); this.to.next(root); var opt = root.opt; if (!opt.azureBlob && !process.env.AZURE_BLOB_CONTAINER) {