Uploader.js
5.19 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Uploader.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* Upload blobs or blob infos to the specified URL or handler.
*
* @private
* @class tinymce.file.Uploader
* @example
* var uploader = new Uploader({
* url: '/upload.php',
* basePath: '/base/path',
* credentials: true,
* handler: function(data, success, failure) {
* ...
* }
* });
*
* uploader.upload(blobInfos).then(function(result) {
* ...
* });
*/
define("tinymce/file/Uploader", [
"tinymce/util/Promise",
"tinymce/util/Tools",
"tinymce/util/Fun"
], function(Promise, Tools, Fun) {
return function(uploadStatus, settings) {
var pendingPromises = {};
function fileName(blobInfo) {
var ext, extensions;
extensions = {
'image/jpeg': 'jpg',
'image/jpg': 'jpg',
'image/gif': 'gif',
'image/png': 'png'
};
ext = extensions[blobInfo.blob().type.toLowerCase()] || 'dat';
return blobInfo.id() + '.' + ext;
}
function pathJoin(path1, path2) {
if (path1) {
return path1.replace(/\/$/, '') + '/' + path2.replace(/^\//, '');
}
return path2;
}
function blobInfoToData(blobInfo) {
return {
id: blobInfo.id,
blob: blobInfo.blob,
base64: blobInfo.base64,
filename: Fun.constant(fileName(blobInfo))
};
}
function defaultHandler(blobInfo, success, failure, progress) {
var xhr, formData;
xhr = new XMLHttpRequest();
xhr.open('POST', settings.url);
xhr.withCredentials = settings.credentials;
xhr.upload.onprogress = function(e) {
progress(e.loaded / e.total * 100);
};
xhr.onerror = function() {
failure("Image upload failed due to a XHR Transport error. Code: " + xhr.status);
};
xhr.onload = function() {
var json;
if (xhr.status != 200) {
failure("HTTP Error: " + xhr.status);
return;
}
json = JSON.parse(xhr.responseText);
if (!json || typeof json.location != "string") {
failure("Invalid JSON: " + xhr.responseText);
return;
}
success(pathJoin(settings.basePath, json.location));
};
formData = new FormData();
formData.append('file', blobInfo.blob(), fileName(blobInfo));
xhr.send(formData);
}
function noUpload() {
return new Promise(function(resolve) {
resolve([]);
});
}
function handlerSuccess(blobInfo, url) {
return {
url: url,
blobInfo: blobInfo,
status: true
};
}
function handlerFailure(blobInfo, error) {
return {
url: '',
blobInfo: blobInfo,
status: false,
error: error
};
}
function resolvePending(blobUri, result) {
Tools.each(pendingPromises[blobUri], function(resolve) {
resolve(result);
});
delete pendingPromises[blobUri];
}
function uploadBlobInfo(blobInfo, handler, openNotification) {
uploadStatus.markPending(blobInfo.blobUri());
return new Promise(function(resolve) {
var notification, progress;
var noop = function() {
};
try {
var closeNotification = function() {
if (notification) {
notification.close();
progress = noop; // Once it's closed it's closed
}
};
var success = function(url) {
closeNotification();
uploadStatus.markUploaded(blobInfo.blobUri(), url);
resolvePending(blobInfo.blobUri(), handlerSuccess(blobInfo, url));
resolve(handlerSuccess(blobInfo, url));
};
var failure = function() {
closeNotification();
uploadStatus.removeFailed(blobInfo.blobUri());
resolvePending(blobInfo.blobUri(), handlerFailure(blobInfo, failure));
resolve(handlerFailure(blobInfo, failure));
};
progress = function(percent) {
if (percent < 0 || percent > 100) {
return;
}
if (!notification) {
notification = openNotification();
}
notification.progressBar.value(percent);
};
handler(blobInfoToData(blobInfo), success, failure, progress);
} catch (ex) {
resolve(handlerFailure(blobInfo, ex.message));
}
});
}
function isDefaultHandler(handler) {
return handler === defaultHandler;
}
function pendingUploadBlobInfo(blobInfo) {
var blobUri = blobInfo.blobUri();
return new Promise(function(resolve) {
pendingPromises[blobUri] = pendingPromises[blobUri] || [];
pendingPromises[blobUri].push(resolve);
});
}
function uploadBlobs(blobInfos, openNotification) {
blobInfos = Tools.grep(blobInfos, function(blobInfo) {
return !uploadStatus.isUploaded(blobInfo.blobUri());
});
return Promise.all(Tools.map(blobInfos, function(blobInfo) {
return uploadStatus.isPending(blobInfo.blobUri()) ?
pendingUploadBlobInfo(blobInfo) : uploadBlobInfo(blobInfo, settings.handler, openNotification);
}));
}
function upload(blobInfos, openNotification) {
return (!settings.url && isDefaultHandler(settings.handler)) ? noUpload() : uploadBlobs(blobInfos, openNotification);
}
settings = Tools.extend({
credentials: false,
// We are adding a notify argument to this (at the moment, until it doesn't work)
handler: defaultHandler
}, settings);
return {
upload: upload
};
};
});