Skip to content

Commit

Permalink
Merge pull request #400 from evershopcommerce/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
treoden authored Nov 20, 2023
2 parents 7162d4d + 0b701dc commit 98f8212
Show file tree
Hide file tree
Showing 43 changed files with 1,065 additions and 852 deletions.
80 changes: 0 additions & 80 deletions packages/azure_file_storage/api/fileBrowser/azureFileBrowser.js

This file was deleted.

62 changes: 0 additions & 62 deletions packages/azure_file_storage/api/fileDelete/azureFileDelete.js

This file was deleted.

57 changes: 0 additions & 57 deletions packages/azure_file_storage/api/folderCreate/azureFolderCreate.js

This file was deleted.

This file was deleted.

43 changes: 43 additions & 0 deletions packages/azure_file_storage/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { addProcessor } = require('@evershop/evershop/src/lib/util/registry');
const { azureFileUploader } = require('./services/azureFileUploader');
const { azureFileDeleter } = require('./services/azureFileDeleter');
const { azureFolderCreator } = require('./services/azureFolderCreator');
const { azureFileBrowser } = require('./services/azureFileBrowser');

module.exports = () => {
addProcessor('fileUploader', function (value) {
const {config} = this;
if (config === 'azure') {
return azureFileUploader;
} else {
return value;
}
});

addProcessor('fileDeleter', function (value) {
const {config} = this;
if (config === 'azure') {
return azureFileDeleter;
} else {
return value;
}
});

addProcessor('folderCreator', function (value) {
const {config} = this;
if (config === 'azure') {
return azureFolderCreator;
} else {
return value;
}
});

addProcessor('fileBrowser', function (value) {
const {config} = this;
if (config === 'azure') {
return azureFileBrowser;
} else {
return value;
}
});
};
50 changes: 50 additions & 0 deletions packages/azure_file_storage/services/azureFileBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { BlobServiceClient } = require('@azure/storage-blob');
const { getEnv } = require('@evershop/evershop/src/lib/util/getEnv');

module.exports.azureFileBrowser = {
list: async (path) => {
const blobServiceClient = BlobServiceClient.fromConnectionString(
getEnv('AZURE_STORAGE_CONNECTION_STRING')
);
const containerName = getEnv('AZURE_STORAGE_CONTAINER_NAME', 'images');
const containerClient = blobServiceClient.getContainerClient(containerName);
// Create a container if it does not exist with access level set to public
await containerClient.createIfNotExists({
access: 'blob'
});

if (path !== '') {
// eslint-disable-next-line no-param-reassign
path = `${path}/`;
}
const blobs = containerClient.listBlobsFlat({ prefix: path });
const subfolders = new Set();
const files = [];
// eslint-disable-next-line no-restricted-syntax
for await (const blob of blobs) {
const blobName = blob.name;
const blobUrl = `${containerClient.url}/${blobName}`;
const relativePath = blobName.substring(path.length);
const slashIndex = relativePath.indexOf('/');
if (slashIndex === -1) {
// It's a file
if (blob.properties.contentLength) {
files.push({
name: relativePath,
url: blobUrl
});
}
} else {
// It's a subfolder
const subfolder = relativePath.substring(0, slashIndex);
if (subfolder !== '') {
subfolders.add(subfolder);
}
}
}
return {
folders: Array.from(subfolders),
files
};
}
};
20 changes: 20 additions & 0 deletions packages/azure_file_storage/services/azureFileDeleter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { BlobServiceClient } = require('@azure/storage-blob');
const { getEnv } = require('@evershop/evershop/src/lib/util/getEnv');

module.exports.azureFileDeleter = {
delete: async (path) => {
const blobServiceClient = BlobServiceClient.fromConnectionString(
getEnv('AZURE_STORAGE_CONNECTION_STRING')
);
const containerName = getEnv('AZURE_STORAGE_CONTAINER_NAME', 'images');
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(path);
const blobProperties = await blobClient.getProperties();

if (blobProperties.contentType) {
await blobClient.delete();
} else {
throw new Error(`Path "${path}" does not exist.`);
}
}
};
Loading

0 comments on commit 98f8212

Please sign in to comment.