-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbwt-upload-behavior.html
360 lines (350 loc) · 14.2 KB
/
bwt-upload-behavior.html
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<script src="../pdfjs-dist/build/pdf.min.js"></script>
<script>
var resizer = (function () {
'use strict';
function Resize() {
}
Resize.prototype = {
init: function (outputQuality) {
this.outputQuality = (outputQuality === 'undefined' ? 0.8 : outputQuality);
},
resize: function (dataURL, maxSize, outputType, callback) {
var _this = this;
var image = new Image();
image.crossOrigin = "anonymous"; // This enables CORS
image.onload = function (imageEvent) {
// Resize image
debugger;
var canvas = document.createElement('canvas'),
width = image.width,
height = image.height;
if (width > height) {
if (width > maxSize) {
height *= maxSize / width;
width = maxSize;
}
} else {
if (height > maxSize) {
width *= maxSize / height;
height = maxSize;
}
}
canvas.width = width;
canvas.height = height;
if(_this.isiOSDevice()){
var arrayBuffer = _this.base64ToArrayBuffer(dataURL);
_this.getOrienataion(arrayBuffer,function(orientation){
canvas = _this.transformContext(canvas,orientation,width,height);
canvas.getContext('2d').drawImage(image, 0, 0, width, height);
_this.output(canvas, outputType, callback);
});
}else{
canvas.getContext('2d').drawImage(image, 0, 0, width, height);
_this.output(canvas, outputType, callback);
}
}
image.src = dataURL;
},
output: function (canvas, outputType, callback) {
switch (outputType) {
case 'file':
canvas.toBlob(function (blob) {
callback(blob);
}, 'image/jpeg', 0.8);
break;
case 'dataURL':
callback(canvas.toDataURL('image/jpeg', 0.8));
break;
}
},
base64ToArrayBuffer:function(base64, contentType) {
contentType = contentType || base64.match(/^data\:([^\;]+)\;base64,/mi)[1] || ''; // e.g. 'data:image/jpeg;base64,...' => 'image/jpeg'
base64 = base64.replace(/^data\:([^\;]+)\;base64,/gmi, '');
var binary = atob(base64);
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for (var i = 0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
return buffer;
},
getOrienataion:function(arrayBuffer,callback){
var view = new DataView(arrayBuffer);
if (view.getUint16(0, false) != 0xFFD8)
{
return callback(-2);
}
var length = view.byteLength, offset = 2;
while (offset < length)
{
var marker = view.getUint16(offset, false);
offset += 2;
if (marker == 0xFFE1)
{
if (view.getUint32(offset += 2, false) != 0x45786966)
{
return callback(-1);
}
var little = view.getUint16(offset += 6, false) == 0x4949;
offset += view.getUint32(offset + 4, little);
var tags = view.getUint16(offset, little);
offset += 2;
for (var i = 0; i < tags; i++)
{
if (view.getUint16(offset + (i * 12), little) == 0x0112)
{
return callback(view.getUint16(offset + (i * 12) + 8, little));
}
}
}
else if ((marker & 0xFF00) != 0xFF00)
{
break;
}
else
{
offset += view.getUint16(offset, false);
}
}
},
isiOSDevice:function(){
var iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/i);
return iOS ? true :false;
},
transformContext:function(canvas,orientation,width,height){
var ctx = canvas.getContext("2d");
switch (orientation) {
case 2:
ctx.transform(-1, 0, 0, 1, width, 0);
break;
case 3:
ctx.transform(-1, 0, 0, -1, width, height);
break;
case 4:
ctx.transform(1, 0, 0, -1, 0, height);
break;
case 5:
ctx.transform(0, 1, 1, 0, 0, 0);
break;
case 6:
ctx.transform(0, 1, -1, 0, height, 0);
break;
case 7:
ctx.transform(0, -1, -1, 0, height, width);
break;
case 8:
ctx.transform(0, -1, 1, 0, 0, width);
break;
default:
ctx.transform(1, 0, 0, 1, 0, 0);
}
return canvas;
}
}
return Resize;
}());
/*@polymerBehavior BwtUploadBehavior*/
BwtUploadBehavior = {
properties: {
error: {
type: String,
notify: true,
},
// private variable
file: {
type: Object,
value: {},
notify: true
},
/**
* The type of placeholder, accepts either one of two arguments; square or circle
**/
theme: {
type: String,
value: 'circle',
notify: true,
reflectToAttribute: true
},
// url of the image to be displayed, this can be used mixed with the placeholder
image: {
type: String,
value: '',
notify: true,
reflectToAttribute: true
},
/**
* location of the placeholder image
**/
placeholder: {
type: String,
value: '/bower_components/bwt-uploader/images/placeholder.png',
notify: true
},
placeHolderRemoved: {
type: Boolean,
value: false,
notify: true
},
customClass: {
type: Boolean,
value: false,
notify: true
}
},
_hasWrongMimeType(file, acceptedFileTypes) {
if (!acceptedFileTypes.includes(file.type))
throw new Error('Invalid file type, please upload an accepted file type');
},
_hasMoreThanOneFile(files) {
if (files.length > 1)
throw new Error('You can only upload one file at a time');
},
_validateFile(files) {
try {
this._hasMoreThanOneFile(files);
this._hasWrongMimeType(files[0], this.acceptedFileTypes);
if (!this.height) this._hasExceededMaximumFileSize(files[0]);
} catch (e) {
this.error = e.message;
}
},
// Only applies images that are captured or less than minute
_isRecentImage(file) {
if (file && file.lastModified) {
var diiference = Math.round(new Date().getTime() - file.lastModified) / 60000;
if (diiference <= 1) return true;
else return false;
}
},
_hasExceededMaximumFileSize(file) {
var _isRecentImage = this._isRecentImage(file);
if (file.size > (this.maxFileSize * 1024) && !_isRecentImage)
throw new Error('The uploaded file exceeds ' + this.maxFileSize + ' KB')
},
_uploadFile(files) {
this._validateFile(files);
if (this.error) return;
this.set('file', files[0]);
this.set('file.progress', 20);
this.notifyPath('file.progress');
this.file.error = false;
this.file.complete = false;
this._startPreviewAndAjax(this.file);
},
_startPreviewAndAjax(file) {
try {
if (file.type === 'application/pdf') {
this.set('file.progress', 40);
this.notifyPath('file.progress');
this._getDocumentPreview(file).then( (success) => {
success.task.then((done) => {
this.image = success.canvas.toDataURL("image/jpeg");
this.$.preview._updateImage(this.placeholder, this.image);
this._uploadPdf(file, this.image);
});
}).catch(error => {
throw new Error(error);
});
} else {
this.toDataURL(file).then(function(image) {
this.$.preview._createImagePreview(file, image);
if(this.resize && this.height){
var resize = new resizer();
resize.init();
resize.resize(image, this.height, 'file',function(newFile){
var newImage = newFile;
newImage.lastModified = file.lastModified;
this._hasExceededMaximumFileSize(newImage);
resize.resize(image, this.height, 'dataURL',function(newDataURL){
this.$.preview._createImagePreview(file, newDataURL);
this.set('file.progress', 60);
this.notifyPath('file.progress');
this._upload(newDataURL);
}.bind(this));
}.bind(this));
}else{
this.set('file.progress', 60);
this.notifyPath('file.progress');
this._upload(image);
}
}.bind(this));
}
} catch (e) {
console.error(e);
this.set('file.error', true);
this.notifyPath('file.error');
this.error = e.message || 'File Couldn\'t be uploaded';
}
},
_getDocumentPreview(file) {
return new Promise( (resolve, reject) => {
this.set('file.progress', 60);
this.notifyPath('file.progress');
PDFJS.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/1.7.246/pdf.worker.min.js';
// there is no proper way to load this thing other than a cdn
this._createArrayBufferFie(file)
.then(buffer => {
return PDFJS.getDocument(buffer);
})
.then(pdf => {
return pdf.getPage(1);
})
.then(page => {
var scale = 0.36;
var viewport = page.getViewport(scale);
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var task = page.render({
canvasContext: context,
viewport: viewport
});
resolve({ task: task.promise, canvas });
})
.catch((error) => {
reject(error);
});
})
},
_createArrayBufferFie(file) {
var fileReader = new FileReader();
return new Promise(function (resolve, reject) {
fileReader.onload = function (ev) {
resolve(fileReader.result);
}
fileReader.readAsArrayBuffer(file);
});
},
_upload(file, img) {
debugger;
this.set('file.progress', 80);
this.notifyPath('file.progress');
var ajax = this.$.ajaxElement;
ajax.contentType = "application/json";
ajax.body = this.body;
ajax.body.file = file;
if (img) ajax.body.thumbnail = img;
this.$.ajaxElement.generateRequest();
},
_uploadPdf(file, img) {
var fileReader = new FileReader();
fileReader.onload = function (ev) {
this._upload(ev.target.result, img);
}.bind(this);
fileReader.readAsDataURL(file);
this.set('file.progress', 80);
this.notifyPath('file.progress');
},
toDataURL(file) {
return new Promise((resolve, reject) => {
let fr = new FileReader();
fr.readAsDataURL(file);
fr.onload = (ev) => {
resolve(ev.target.result);
};
})
}
}
</script>