many-cloud
Version:
A Node module for abstracting file management and interfacing with a variety of cloud storages.
58 lines (51 loc) • 1.8 kB
JavaScript
const { URL } = require("url");
const path = require("path");
const fs = require("fs");
const { ShareServiceClient, StorageSharedKeyCredential } = require("@azure/storage-file-share");
class AzureFileShareIntegration {
constructor(props) {
this.type = "AzureFileShare";
this.settings = props.settings || {};
if (!("autoMkdirOnFileUpload" in this.settings)) {
this.settings.autoMkdirOnFileUpload = true;
}
let account = null;
let accountKey = null;
let shareURL = null;
if (!props.account || !props.accountKey || !props.shareURL) {
if (fs.existsSync(path.join(__dirname, "../../../credentials/azure_file_share.json"))) {
let load = require("../../../credentials/azure_file_share.json");
account = load.account;
accountKey = load.accountKey;
shareURL = load.shareURL;
}
} else {
account = props.account;
accountKey = props.accountKey;
shareURL = props.shareURL;
}
if (account && accountKey && shareURL) {
shareURL = new URL(shareURL);
let storageOrigin = shareURL.origin;
let shareName = path.basename(shareURL.pathname);
this.shareServiceClient = new ShareServiceClient(
storageOrigin,
new StorageSharedKeyCredential(account, accountKey)
);
this.shareClient = this.shareServiceClient.getShareClient(shareName);
} else {
throw new Error("No way to connect to Azure File Share provided. Need account + accountKey + shareURL.");
}
}
/**
* Cross-platform consistent joining of path with /
*/
join_path(...list) {
return path.join(...list).split(path.sep).join("/");
}
}
module.exports = (props) => {
return new Promise((resolve, reject) => {
resolve(new AzureFileShareIntegration(props));
});
}