-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
246 lines (204 loc) · 5.99 KB
/
index.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
;(function() {
if(typeof Blob === 'undefined')
return console.warn('Screw-FileReader is only meant to work in those' +
'engine who already has some basic support for blob')
var blob = Blob.prototype
var fullStreamSupport = false
var basicStreamSupport = false
var fetchTransform = false
var URL = window.URL || window.webkitURL
function promisify(obj) {
return new Promise(function(resolve, reject) {
obj.onload =
obj.onerror = function(evt) {
obj.onload =
obj.onerror = null
evt.type === 'load'
? resolve(obj.result || obj)
: reject(new Error('Failed to read the blob/file'))
}
})
}
function toImage(url) {
var img = new Image
img.src = url
return promisify(img)
}
function getRotation(buffer) {
var view = new DataView(buffer)
if (view.getUint16(0, false) != 0xFFD8) return -2
var length = view.byteLength
var offset = 2
while (offset < length) {
var marker = view.getUint16(offset, false)
offset += 2
if (marker == 0xFFE1) {
if (view.getUint32(offset += 2, false) != 0x45786966) {
return -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 view.getUint16(offset + (i * 12) + 8, little);
}
else if ((marker & 0xFF00) != 0xFF00) break
else offset += view.getUint16(offset, false)
}
return -1
}
function rotate(img, angle) {
var width = img.width
var height = img.height
var canvas = document.createElement('canvas')
var ctx = canvas.getContext('2d')
// revoke memory
URL.revokeObjectURL(img.src)
// set proper canvas dimensions before transform & export
if (angle > 4 && angle < 9) {
canvas.width = height
canvas.height = width
} else {
canvas.width = width
canvas.height = height
}
// transform context before drawing image
switch (angle) {
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
}
// draw image
ctx.drawImage(img, 0, 0)
// export blob
return new Promise(function (resolve) {
canvas.toBlob(resolve)
})
}
function fixRotation(blob) {
// read only the headers
return blob.slice(0, 65536)
.arrayBuffer()
.then(getRotation)
.then(function (angle) {
// get a image object
return Promise.resolve(blob.url() || blob.dataURL())
.then(toImage)
.then(function(img) {
// return img unless it needs rotation
return angle < 2 ? img :
// rotate the image
rotate(img, angle).then(function (blob) {
return blob.url() || blob.dataURL()
}).then(toImage)
})
})
}
try {
new ReadableStream({})
basicStreamSupport = true
} catch (e) {}
try {
new ReadableStream({type: 'bytes'})
fullStreamSupport = true
} catch (e) {}
try {
(new Response(new Blob)).getReader()
fetchTransform = true
} catch (e) {}
if (!blob.arrayBuffer) {
blob.arrayBuffer = function arrayBuffer() {
var fr = new FileReader
fr.readAsArrayBuffer(this)
return promisify(fr)
}
}
if (!blob.text) {
blob.text = function text() {
var fr = new FileReader
fr.readAsText(this)
return promisify(fr)
}
}
if (!blob.dataURL) {
blob.dataURL = function dataURL() {
var fr = new FileReader
fr.readAsDataURL(this)
return promisify(fr)
}
}
if (!blob.url) {
blob.url = function url() {
return URL ? URL.createObjectURL(this) : null
}
}
if (!blob.json) {
blob.json = function json() {
return this.text().then(JSON.parse)
}
}
if (!blob.image) {
blob.image = function image(preventRevoke) {
return fixRotation(this)
.then(function(img) {
!preventRevoke && URL.revokeObjectURL(img.src)
return img
})
}
}
if (!blob.stream) {
blob.stream =
fullStreamSupport ? function stream() {
var position = 0
var blob = this
return new ReadableStream({
type: 'bytes',
autoAllocateChunkSize: 524288,
pull: function (controller) {
var v = controller.byobRequest.view
var chunk = blob.slice(position, position + v.byteLength)
return chunk.arrayBuffer()
.then(function (buffer) {
var uint8array = new Uint8Array(buffer)
var bytesRead = uint8array.byteLength
position += bytesRead
v.set(uint8array)
controller.byobRequest.respond(bytesRead)
if(position >= blob.size)
controller.close()
})
}
})
}:
// basic stream support
basicStreamSupport ? function stream(blob){
var position = 0
var blob = this
return new ReadableStream({
pull: function (controller) {
var chunk = blob.slice(position, position + 524288)
return chunk.arrayBuffer().then(function (buffer) {
position += buffer.byteLength
var uint8array = new Uint8Array(buffer)
controller.enqueue(uint8array)
if(position == blob.size)
controller.close()
})
}
})
}:
// fetchTransform
fetchTransform ? function stream() {
return (new Response(this)).body
}:
function stream() {
throw new Error('Include https://github.com/creatorrr/web-streams-polyfill')
}
}
}());