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 _request = require('request');
|
30 |
|
31 | var _request2 = _interopRequireDefault(_request);
|
32 |
|
33 | var _util = require('./util');
|
34 |
|
35 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
36 |
|
37 | 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; }
|
38 |
|
39 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
40 |
|
41 | var Service = function () {
|
42 | function Service(options) {
|
43 | _classCallCheck(this, Service);
|
44 |
|
45 | if (!options) {
|
46 | throw new Error('feathers-blob-store: constructor `options` must be provided');
|
47 | }
|
48 |
|
49 | if (!options.Model) {
|
50 | throw new Error('feathers-blob-store: constructor `options.Model` must be provided');
|
51 | }
|
52 |
|
53 | this.Model = options.Model;
|
54 | this.id = options.id || 'id';
|
55 | }
|
56 |
|
57 | _createClass(Service, [{
|
58 | key: 'extend',
|
59 | value: function extend(obj) {
|
60 | return _uberproto2.default.extend(obj, this);
|
61 | }
|
62 | }, {
|
63 | key: 'get',
|
64 | value: function get(id) {
|
65 | var _this = this;
|
66 |
|
67 | var ext = (0, _path.extname)(id);
|
68 | var contentType = _mimeTypes2.default.lookup(ext);
|
69 |
|
70 | return new Promise(function (resolve, reject) {
|
71 | _this.Model.createReadStream({
|
72 | key: id
|
73 | }).on('error', reject).pipe((0, _concatStream2.default)(function (buffer) {
|
74 | var _resolve;
|
75 |
|
76 | var uri = (0, _dauria.getBase64DataURI)(buffer, contentType);
|
77 |
|
78 | resolve((_resolve = {}, _defineProperty(_resolve, _this.id, id), _defineProperty(_resolve, 'uri', uri), _defineProperty(_resolve, 'size', buffer.length), _resolve));
|
79 | }));
|
80 | });
|
81 | }
|
82 | }, {
|
83 | key: 'create',
|
84 | value: function create(body) {
|
85 | var _this2 = this;
|
86 |
|
87 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
88 | var id = body.id,
|
89 | uri = body.uri;
|
90 |
|
91 |
|
92 | var out = void 0;
|
93 | var size = 0;
|
94 |
|
95 | if (uri.startsWith('http')) {
|
96 | out = (0, _request2.default)(uri);
|
97 | var urlParts = uri.split('/');
|
98 | id = id || urlParts[urlParts.length - 1];
|
99 | } else {
|
100 | var parsedUri = (0, _dauria.parseDataURI)(uri);
|
101 | var buffer = parsedUri.buffer;
|
102 |
|
103 | var hash = (0, _util.bufferToHash)(buffer);
|
104 | var ext = _mimeTypes2.default.extension(parsedUri.MIME);
|
105 |
|
106 | id = id || hash + '.' + ext;
|
107 |
|
108 | out = (0, _util.fromBuffer)(buffer);
|
109 | }
|
110 |
|
111 | return new Promise(function (resolve, reject) {
|
112 | out.on('data', function (buf) {
|
113 | size += buf.length;
|
114 | }).pipe(_this2.Model.createWriteStream({
|
115 | key: id,
|
116 | params: params.s3
|
117 | }, function (error, test) {
|
118 | var _resolve2;
|
119 |
|
120 | return error ? reject(error) : resolve((_resolve2 = {}, _defineProperty(_resolve2, _this2.id, id), _defineProperty(_resolve2, 'uri', uri), _defineProperty(_resolve2, 'size', size), _resolve2));
|
121 | })).on('error', reject);
|
122 | });
|
123 | }
|
124 | }, {
|
125 | key: 'remove',
|
126 | value: function remove(id) {
|
127 | var _this3 = this;
|
128 |
|
129 | return new Promise(function (resolve, reject) {
|
130 | _this3.Model.remove({
|
131 | key: id
|
132 | }, function (error) {
|
133 | return error ? reject(error) : resolve();
|
134 | });
|
135 | });
|
136 | }
|
137 | }]);
|
138 |
|
139 | return Service;
|
140 | }();
|
141 |
|
142 | function init(options) {
|
143 | return new Service(options);
|
144 | }
|
145 |
|
146 | init.Service = Service;
|
147 | module.exports = exports['default']; |
\ | No newline at end of file |