-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
65 lines (65 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var azure = require("azure-storage");
var Blob = (function () {
function Blob(opts) {
this.container = opts.container;
this.blobSvc = opts.connectionString ? azure.createBlobService(opts.connectionString) : azure.createBlobService(opts.account, opts.key);
this.createContainer(this.container);
this.blobPathResolver = opts.blobPathResolver;
this.error = null;
}
;
Blob.prototype.createContainer = function (name) {
var _this = this;
this.blobSvc.createContainerIfNotExists(name, function (error, result, response) {
if (error) {
_this.error = error;
}
});
};
Blob.prototype.uploadToBlob = function (req, file, cb) {
var that = this;
return function (something, blobPath) {
var blobStream = that.blobSvc.createWriteStreamToBlockBlob(that.container, blobPath, function (error) {
if (error) {
cb(error);
}
});
file.stream.pipe(blobStream);
blobStream.on("close", function () {
var fullUrl = that.blobSvc.getUrl(that.container, blobPath);
var fileClone = JSON.parse(JSON.stringify(file));
fileClone.container = that.container;
fileClone.blobPath = blobPath;
fileClone.url = fullUrl;
cb(null, fileClone);
});
blobStream.on("error", function (error) {
cb(error);
});
};
};
Blob.prototype._handleFile = function (req, file, cb) {
if (this.error) {
cb(this.error);
}
else if (this.blobPathResolver) {
this.blobPathResolver(req, file, this.uploadToBlob(req, file, cb));
}
else {
var re = /(?:\.([^.]+))?$/;
var ext = re.exec(file.originalname)[1];
var newName = Date.now() + '-' + encodeURIComponent(new Buffer(file.originalname).toString('base64')) + '.' + ext;
this.uploadToBlob(req, file, cb)(null, newName);
}
};
Blob.prototype._removeFile = function (req, file, cb) {
this.blobSvc.deleteBlob(this.container, file.filename, cb);
};
return Blob;
}());
module.exports = function (opts) {
return new Blob(opts);
};
//# sourceMappingURL=index.js.map