multer-azure-blob-storage
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -8,2 +8,6 @@ # Change log | ||
- Initial release | ||
- Initial release | ||
## 1.0.0 - 2017-11-20 | ||
- Added the ability to include metadata per file uploaded |
@@ -7,4 +7,8 @@ /// <reference types="express" /> | ||
import { Stream } from "stream"; | ||
export declare type MetadataObj = { | ||
[k: string]: string; | ||
}; | ||
export declare type ContainerAccessLevel = 'blob' | 'container' | 'private'; | ||
export declare type MASNameResolver = (req: Request, file: Express.Multer.File) => Promise<string>; | ||
export declare type MASObjectResolver = (req: Request, file: Express.Multer.File) => Promise<Object>; | ||
export interface IMASOptions { | ||
@@ -17,2 +21,3 @@ accessKey?: string; | ||
containerName: MASNameResolver | string; | ||
metadata?: MASObjectResolver | MetadataObj; | ||
containerAccessLevel?: ContainerAccessLevel; | ||
@@ -46,2 +51,3 @@ } | ||
private _urlExpirationTime; | ||
private _metadata; | ||
private _containerName; | ||
@@ -61,2 +67,3 @@ private _containerAccessLevel; | ||
private _promisifyStaticValue(value); | ||
private _promisifyStaticObj<T>(value); | ||
} |
@@ -122,2 +122,20 @@ "use strict"; | ||
} | ||
// Check for metadata | ||
if (!options.metadata) { | ||
this._metadata = null; | ||
} | ||
else { | ||
switch (typeof options.metadata) { | ||
case "object": | ||
this._metadata = this._promisifyStaticObj(options.metadata); | ||
break; | ||
case "function": | ||
this._metadata = options.metadata; | ||
break; | ||
default: | ||
// Nullify all other types | ||
this._metadata = null; | ||
break; | ||
} | ||
} | ||
// Set proper blob name | ||
@@ -137,3 +155,3 @@ this._blobName = options.blobName ? options.blobName : this._generateBlobName; | ||
var _this = this; | ||
var blobName_1, containerName_1, blobStream, hFError_1; | ||
var blobName_1, containerName_1, blobStream, metadata, hFError_1; | ||
return __generator(this, function (_a) { | ||
@@ -151,3 +169,3 @@ switch (_a.label) { | ||
case 1: | ||
_a.trys.push([1, 5, , 6]); | ||
_a.trys.push([1, 8, , 9]); | ||
return [4 /*yield*/, this._blobName(req, file)]; | ||
@@ -164,2 +182,4 @@ case 2: | ||
_a.sent(); | ||
blobStream = void 0; | ||
if (!(this._metadata == null)) return [3 /*break*/, 5]; | ||
blobStream = this._blobService.createWriteStreamToBlockBlob(containerName_1, blobName_1, function (cWSTBBError, result, response) { | ||
@@ -173,2 +193,18 @@ if (cWSTBBError) { | ||
}); | ||
return [3 /*break*/, 7]; | ||
case 5: return [4 /*yield*/, this._metadata(req, file)]; | ||
case 6: | ||
metadata = _a.sent(); | ||
blobStream = this._blobService.createWriteStreamToBlockBlob(containerName_1, blobName_1, { | ||
metadata: metadata, | ||
}, function (cWSTBBError, result, response) { | ||
if (cWSTBBError) { | ||
cb(cWSTBBError); | ||
} | ||
else { | ||
// All good. Continue... | ||
} | ||
}); | ||
_a.label = 7; | ||
case 7: | ||
// Upload away | ||
@@ -204,8 +240,8 @@ file.stream.pipe(blobStream); | ||
}); | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
return [3 /*break*/, 9]; | ||
case 8: | ||
hFError_1 = _a.sent(); | ||
cb(hFError_1); | ||
return [3 /*break*/, 6]; | ||
case 6: return [2 /*return*/]; | ||
return [3 /*break*/, 9]; | ||
case 9: return [2 /*return*/]; | ||
} | ||
@@ -347,2 +383,9 @@ }); | ||
}; | ||
MulterAzureStorage.prototype._promisifyStaticObj = function (value) { | ||
return function (req, file) { | ||
return new Promise(function (resolve, reject) { | ||
resolve(value); | ||
}); | ||
}; | ||
}; | ||
return MulterAzureStorage; | ||
@@ -349,0 +392,0 @@ }()); |
{ | ||
"name": "multer-azure-blob-storage", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "ES5/6 & Typescript friendly multer storage engine for Azure's blob storage.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
## Multer Azure Blob Storage | ||
[![npm version](https://badge.fury.io/js/multer-azure-blob-storage.svg)](https://badge.fury.io/js/multer-azure-blob-storage) [![Build Status](https://travis-ci.org/Sliverb/multer-azure-blob-storage.svg?branch=master)](https://travis-ci.org/Sliverb/multer-azure-blob-storage) | ||
@@ -31,2 +32,10 @@ ES6 & Typescript friendly [Multer](https://github.com/expressjs/multer) storage engine for Azure's blob storage. | ||
export type MetadataObj = { [k: string]: string }; | ||
const resolveMetadata: MASObjectResolver = (req: any, file: Express.Multer.File): Promise<MetadataObj> => { | ||
return new Promise<MetadataObj>((resolve, reject) => { | ||
const metadata: MetadataObj = yourCustomLogic(req, file); | ||
resolve(metadata); | ||
}); | ||
}; | ||
const azureStorage: MulterAzureStorage = new MulterAzureStorage({ | ||
@@ -38,2 +47,3 @@ connectionString: 'DefaultEndpointsProtocol=https;AccountName=mystorageaccountname;AccountKey=wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY;EndpointSuffix=core.windows.net', | ||
blobName: resolveBlobName, | ||
metadata: resolveMetadata, | ||
containerAccessLevel: 'blob', | ||
@@ -76,2 +86,9 @@ urlExpirationTime: 60 | ||
const resolveMetadata = (req, file) => { | ||
return new Promise((resolve, reject) => { | ||
const metadata = yourCustomLogic(req, file); | ||
resolve(metadata); | ||
}); | ||
}; | ||
const azureStorage = new MulterAzureStorage({ | ||
@@ -83,2 +100,3 @@ connectionString: 'DefaultEndpointsProtocol=https;AccountName=mystorageaccountname;AccountKey=wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY;EndpointSuffix=core.windows.net', | ||
blobName: resolveBlobName, | ||
metadata: resolveMetadata, | ||
containerAccessLevel: 'blob', | ||
@@ -127,2 +145,3 @@ urlExpirationTime: 60 | ||
| `containerName` | `string` or `function: MASNameResolver` | `'documents'` or `(req: any, file: Express.Multer.File) => Promise<string>` | | ||
| `metadata` | `{ [k: string]: string }` or `function: MASObjectResolver` | `'{author: John Doe; album: ASOT}'` or `(req: any, file: Express.Multer.File) => Promise<{[k: string]: string}>` | | ||
| `blobName` | `function: MASNameResolver` (optional) | `(req: any, file: Express.Multer.File) => Promise<string>` | | ||
@@ -158,2 +177,13 @@ | `containerAccessLevel` | `string` (optional) | `'blob'` or `'container'` or `'private'` | | ||
`multer-azure-blob-storage` also allows you to add/customize `metadata` per request before uploading the file. This can be done by proving a `MASObjectResolver` function in the configuation object for the desired parameter. | ||
``` javascript | ||
export type MetadataObj = { [k: string]: string }; | ||
const resolveMetadata: MASObjectResolver = (req: any, file: Express.Multer.File): Promise<MetadataObj> => { | ||
return new Promise<MetadataObj>((resolve, reject) => { | ||
const metadata: MetadataObj = yourCustomLogic(req, file); | ||
resolve(metadata); | ||
}); | ||
}; | ||
``` | ||
### Azure | ||
@@ -180,8 +210,13 @@ | ||
### Tests | ||
Not implemented yet | ||
### Thanks | ||
All great things are built on the shoulder of giants. I want to thank my giants for lending their shoulders: | ||
- [mckalexee](https://github.com/mckalexee/multer-azure) | ||
- [MantaCodeDevs](https://github.com/MantaCodeDevs/multer-azure-storage) | ||
### License | ||
[MIT](LICENSE) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33338
9
458
215