1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 |
|
10 |
|
11 | exports.default = init;
|
12 |
|
13 | var _path = require('path');
|
14 |
|
15 | var _uberproto = require('uberproto');
|
16 |
|
17 | var _uberproto2 = _interopRequireDefault(_uberproto);
|
18 |
|
19 | var _dauria = require('dauria');
|
20 |
|
21 | var _concatStream = require('concat-stream');
|
22 |
|
23 | var _concatStream2 = _interopRequireDefault(_concatStream);
|
24 |
|
25 | var _mimeTypes = require('mime-types');
|
26 |
|
27 | var _mimeTypes2 = _interopRequireDefault(_mimeTypes);
|
28 |
|
29 | var _util = require('./util');
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
34 |
|
35 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
36 |
|
37 | var Service = function () {
|
38 | function Service(options) {
|
39 | _classCallCheck(this, Service);
|
40 |
|
41 | if (!options) {
|
42 | throw new Error('feathers-blob-store: constructor `options` must be provided');
|
43 | }
|
44 |
|
45 | if (!options.Model) {
|
46 | throw new Error('feathers-blob-store: constructor `options.Model` must be provided');
|
47 | }
|
48 |
|
49 | this.Model = options.Model;
|
50 | this.id = options.id || 'id';
|
51 | }
|
52 |
|
53 | _createClass(Service, [{
|
54 | key: 'extend',
|
55 | value: function extend(obj) {
|
56 | return _uberproto2.default.extend(obj, this);
|
57 | }
|
58 | }, {
|
59 | key: 'get',
|
60 | value: function get(id) {
|
61 | var _this = this;
|
62 |
|
63 | var ext = (0, _path.extname)(id);
|
64 | var contentType = _mimeTypes2.default.lookup(ext);
|
65 |
|
66 | return new Promise(function (resolve, reject) {
|
67 | _this.Model.createReadStream({
|
68 | key: id
|
69 | }).then(function (stream) {
|
70 | stream.on('error', reject).pipe((0, _concatStream2.default)(function (buffer) {
|
71 | var _resolve;
|
72 |
|
73 | var uri = (0, _dauria.getBase64DataURI)(buffer, contentType);
|
74 |
|
75 | resolve((_resolve = {}, _defineProperty(_resolve, _this.id, id), _defineProperty(_resolve, 'uri', uri), _defineProperty(_resolve, 'size', buffer.length), _resolve));
|
76 | }));
|
77 | });
|
78 | });
|
79 | }
|
80 | }, {
|
81 | key: 'create',
|
82 | value: function create(body) {
|
83 | var _this2 = this;
|
84 |
|
85 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
86 | var id = body.id,
|
87 | uri = body.uri;
|
88 |
|
89 | var _parseDataURI = (0, _dauria.parseDataURI)(uri),
|
90 | buffer = _parseDataURI.buffer,
|
91 | contentType = _parseDataURI.MIME;
|
92 |
|
93 | var hash = (0, _util.bufferToHash)(buffer);
|
94 | var ext = _mimeTypes2.default.extension(contentType);
|
95 |
|
96 | id = id || hash + '.' + ext;
|
97 |
|
98 | return new Promise(function (resolve, reject) {
|
99 | _this2.Model.createWriteStream({
|
100 | key: id
|
101 | }, function (error) {
|
102 | var _resolve2;
|
103 |
|
104 | return error ? reject(error) : resolve((_resolve2 = {}, _defineProperty(_resolve2, _this2.id, id), _defineProperty(_resolve2, 'uri', uri), _defineProperty(_resolve2, 'size', buffer.length), _resolve2));
|
105 | }).then(function (stream) {
|
106 | (0, _util.fromBuffer)(buffer).pipe(stream).on('error', reject);
|
107 | });
|
108 | });
|
109 | }
|
110 | }, {
|
111 | key: 'remove',
|
112 | value: function remove(id) {
|
113 | var _this3 = this;
|
114 |
|
115 | return new Promise(function (resolve, reject) {
|
116 | _this3.Model.remove({
|
117 | key: id
|
118 | }, function (error) {
|
119 | return error ? reject(error) : resolve();
|
120 | });
|
121 | });
|
122 | }
|
123 | }]);
|
124 |
|
125 | return Service;
|
126 | }();
|
127 |
|
128 | function init(options) {
|
129 | return new Service(options);
|
130 | }
|
131 |
|
132 | init.Service = Service;
|
133 | module.exports = exports['default']; |
\ | No newline at end of file |