This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader-angular-app.js
288 lines (250 loc) · 8.03 KB
/
uploader-angular-app.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use strict';
// Declare app level module
angular.module('uploaderApp', ['uploaderApp.controllers', 'uploaderApp.directives', 'uploaderApp.services']);
/* Directives */
angular.module('uploaderApp.directives', []).
directive('fileChange', [function() {
// Add a directive so we can trigger change events on file inputs
// Retrieved on 2013-07-28 from http://jsfiddle.net/neilsarkar/vQzKJ/
return {
link: function(scope, element, attrs) {
element[0].onchange = function() {
scope[attrs['fileChange']](element[0]);
};
}
};
}
]);
/* Services */
angular.module('uploaderApp.services', []).
factory('notify', ['$window', function($window) {
return function($scope, file) {
console.log("Upload completed", file);
}
}
]);
/* Controllers */
/**
* The uploader controller
*
* @param Scope $scope The Angular.js scope
*/
angular.module('uploaderApp.controllers', [])
.controller('UploaderCtrl', ['$scope', 'notify', function($scope, $uploadCompleteCallback) {
// Initialize our variables
$scope.files = [];
// Fit the input box the same size as its button
var inputFile = $('.input-file');
$(inputFile).css({
height: $(inputFile).parent().css('height'),
width: $(inputFile).parent().css('width'),
marginTop: '-' + $(inputFile).parent().css('paddingTop'),
marginLeft: '-' + $(inputFile).parent().css('paddingLeft')
});
$(inputFile).attr('multiple', 'multiple');
/**
* Get the total number of bytes to upload
*
* @return integer
*/
$scope.getBytesTotal = function () {
var bytesTotal = 0;
for (var i=0; i < $scope.files.length; i++) {
if ($scope.files[i].isTooBig) {
continue;
}
bytesTotal += $scope.files[i].size;
}
return bytesTotal;
};
/**
* Get the total number of bytes uploaded
*
* @return integer
*/
$scope.getBytesUploaded = function () {
var bytesUploaded = 0;
for (var i=0; i < $scope.files.length; i++) {
if ($scope.files[i].bytesUploaded !== undefined) {
bytesUploaded += $scope.files[i].bytesUploaded;
}
}
return bytesUploaded;
};
/**
* Return the upload progress as a percentage
*
* @return integer
*/
$scope.getUploadProgress = function () {
var bytesTotal = $scope.getBytesTotal();
var bytesUploaded = $scope.getBytesUploaded();
return (bytesUploaded / bytesTotal) * 100;
};
/**
* Get the number of files left to upload
*
* @return integer
*/
$scope.getFilesToUpload = function () {
return _.filter($scope.files, function (file) {
return !file.isUploaded && !file.isUploadError && !file.isTooBig;
}).length;
};
/**
* Add one or more files to the upload queue
*
* @param DOM element The file upload element
*/
$scope.addFiles = function(element) {
if ($scope.getUploadProgress() > 0) {
// Clear previously uploaded files
$scope.files = [];
}
$scope.$apply(function() {
// Add the files to the list
for (var i=0; i < element.files.length; i++) {
var file = element.files[i];
if (file.size > Config.maxUploadSize) {
file.isTooBig = true;
} else {
file.isTooBig = false;
}
file.sizeFormatted = number_format(file.size);
file.progress = 0;
file.url = null;
file.isUploaded = false;
file.isUploadError = false;
$scope.files.push(file);
}
// Make sure there are no dupes
$scope.files = _.uniq($scope.files);
});
};
/**
* Clear all files from the upload queue
*/
$scope.clearFiles = function () {
// Abort any pending uploads
for (var i=0; i < $scope.files.length; i++) {
if ($scope.files[i].xhr !== undefined) {
$scope.files[i].xhr.abort();
}
}
$scope.files = [];
};
/**
* Remove a single file from the upload queue
*
* @param File file The file to remove
*/
$scope.removeFile = function (file) {
if (file.xhr !== undefined) {
file.xhr.abort();
}
$scope.files = _.without($scope.files, file);
};
/**
* Upload the files in the queue
*
* @params Event $event The DOM event
*/
$scope.uploadFiles = function ($event) {
$event.preventDefault();
// Get the form data as a key => value array
var formObject = $('.form-upload').serializeObject();
var formUrl = $('.form-upload').attr('action');
var formMethod = $('.form-upload').attr('method');
for (var i=0; i < $scope.files.length; i++) {
var file = $scope.files[i];
// Don't upload the same file twice
if (file.isUploaded === true) {
continue;
}
// Don't upload files which exceed the max upload size
if (file.isTooBig === true) {
continue;
}
var u = new Uploader($scope, $uploadCompleteCallback, file, formObject, formUrl, formMethod);
u.send();
}
};
}]);
/**
* The uploader class
*
* @param Scope $scope The Angular.js scope
* @param function $uploadCompleteCallback The upload complete callback function
* @param File file The file to upload
* @param object formObject The form fields in a key => value object
* @param string formUrl The url to upload the file to
* @param string formMethod The form method
*/
function Uploader($scope, $uploadCompleteCallback, file, formObject, formUrl, formMethod) {
this.file = file;
this.formObject = formObject;
this.formUrl = formUrl;
this.formMethod = formMethod;
this.$scope = $scope;
this.$uploadCompleteCallback = $uploadCompleteCallback;
}
/**
* Upload the file
*/
Uploader.prototype.send = function () {
var $this = this;
var xhr = new XMLHttpRequest();
this.file.xhr = xhr;
/**
* Update the upload progress for this file
*
* @param Event e The event
*/
var uploadProgress = function(e) {
var percent = Math.round((e.loaded / e.total) * 100);
$this.$scope.$apply(function() {
$this.file.bytesUploaded = e.loaded;
$this.file.progress = percent;
});
};
xhr.upload.addEventListener("progress", uploadProgress, false);
/**
* Triggered when the upload is complete
*
* @param Event e The event
*/
var uploadComplete = function(e) {
if (e.target.status == 201) {
// Extract the path to the file
var imagePath = $(e.target.responseXML).find('Key').text();
var folder;
if (imagePath.match(/\//)) {
folder = dirname(imagePath);
} else {
folder = '';
}
var url = $(e.target.responseXML).find('Location').text();
$this.$scope.$apply(function() {
$this.file.isUploaded = true;
$this.file.url = url;
$this.file.folder = folder;
// Trigger the upload complete callback
$this.$uploadCompleteCallback($this.$scope, $this.file);
});
} else {
$this.$scope.$apply(function() {
$this.file.isUploadError = true;
});
}
};
xhr.addEventListener("load", uploadComplete, false);
// Assemble the form data
var form = new FormData();
for (var j in this.formObject) {
form.append(j, this.formObject[j]);
}
form.append('file', this.file);
// Super cEvin Attack Mode Go!
xhr.open(this.formMethod, this.formUrl, true);
xhr.send(form);
};