UNPKG

4.78 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
3import { func, obj } from '../../util';
4import { uid, errorCode } from '../util';
5import defaultRequest from './request';
6
7var noop = func.noop;
8
9var Uploader = function () {
10 function Uploader(options) {
11 _classCallCheck(this, Uploader);
12
13 this.options = _extends({
14 beforeUpload: noop,
15 onProgress: noop,
16 onSuccess: noop,
17 onError: noop,
18 data: {},
19 name: 'file',
20 method: 'post'
21 }, options);
22 this.reqs = {};
23 }
24
25 Uploader.prototype.setOptions = function setOptions(options) {
26 _extends(this.options, options);
27 };
28
29 Uploader.prototype.startUpload = function startUpload(files) {
30 var _this = this;
31
32 var filesArr = files.length ? Array.prototype.slice.call(files) : [files];
33 filesArr.forEach(function (file) {
34 file.uid = file.uid || uid();
35 _this.upload(file);
36 });
37 };
38
39 Uploader.prototype.abort = function abort(file) {
40 var reqs = this.reqs;
41
42 if (file) {
43 var _uid = file;
44 if (file && file.uid) {
45 _uid = file.uid;
46 }
47 if (reqs[_uid]) {
48 reqs[_uid].abort();
49 delete reqs[_uid];
50 }
51 } else {
52 Object.keys(reqs).forEach(function (uid) {
53 if (reqs[uid] && reqs[uid].abort) {
54 reqs[uid].abort();
55 }
56 delete reqs[uid];
57 });
58 }
59 };
60
61 Uploader.prototype.upload = function upload(file) {
62 var _this2 = this;
63
64 var _options = this.options,
65 beforeUpload = _options.beforeUpload,
66 action = _options.action,
67 name = _options.name,
68 headers = _options.headers,
69 timeout = _options.timeout,
70 withCredentials = _options.withCredentials,
71 method = _options.method,
72 data = _options.data;
73
74 var before = beforeUpload(file, {
75 action: action,
76 name: name,
77 headers: headers,
78 timeout: timeout,
79 withCredentials: withCredentials,
80 method: method,
81 data: data
82 });
83
84 func.promiseCall(before, function (options) {
85 if (options === false) {
86 var err = new Error(errorCode.BEFOREUPLOAD_REJECT);
87 err.code = errorCode.BEFOREUPLOAD_REJECT;
88 return _this2.options.onError(err, null, file);
89 }
90 _this2.post(file, obj.isPlainObject(options) ? options : undefined);
91 }, function (error) {
92 var err = void 0;
93 if (error) {
94 err = error;
95 } else {
96 err = new Error(errorCode.BEFOREUPLOAD_REJECT);
97 err.code = errorCode.BEFOREUPLOAD_REJECT;
98 }
99 _this2.options.onError(err, null, file);
100 });
101 };
102
103 Uploader.prototype.post = function post(file) {
104 var _this3 = this;
105
106 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
107
108 var requestOptions = _extends({}, this.options, options);
109 var action = requestOptions.action,
110 name = requestOptions.name,
111 headers = requestOptions.headers,
112 timeout = requestOptions.timeout,
113 withCredentials = requestOptions.withCredentials,
114 _onProgress = requestOptions.onProgress,
115 _onSuccess = requestOptions.onSuccess,
116 _onError = requestOptions.onError,
117 method = requestOptions.method;
118
119
120 var data = requestOptions.data;
121 if (typeof data === 'function') {
122 data = data(file);
123 }
124
125 var uid = file.uid;
126
127
128 var request = typeof requestOptions.request === 'function' ? requestOptions.request : defaultRequest;
129 this.reqs[uid] = request({
130 action: action,
131 filename: name,
132 file: file,
133 data: data,
134 timeout: timeout,
135 headers: headers,
136 withCredentials: withCredentials,
137 method: method,
138 onProgress: function onProgress(e) {
139 _onProgress(e, file);
140 },
141 onSuccess: function onSuccess(ret) {
142 delete _this3.reqs[uid];
143 _onSuccess(ret, file);
144 },
145 onError: function onError(err, ret) {
146 delete _this3.reqs[uid];
147 _onError(err, ret, file);
148 }
149 });
150 };
151
152 return Uploader;
153}();
154
155export { Uploader as default };
\No newline at end of file