Repository navigation
Expand file tree
/
Copy pathsmil.js
More file actions
359 lines (304 loc) · 10.6 KB
/
Copy pathsmil.js
File metadata and controls
359 lines (304 loc) · 10.6 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
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
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
(function() {
'use strict';
// characters not allowed in smil filenames
var unsafeFilenamePattern = /[^a-zA-Z0-9_#.()?&%-]/g;
// This encoder is aimed for encoding the string by 'utf-8'.
var encoder = new TextEncoder('UTF-8');
// For utilizing sending/receiving DOM API, we need to handle 2 basic object
// first: SMIL document and attachment. SMIL document is used for
// representing the layout of the mms message, and attachment contains media
// source and text. In mms message layout, one message could be split into
// one or more slides. One slide could contain at most one media with/without
// a text string. For example:
/*
Layout of a mms message with 3 slides
===================
---------
| img 1 |
--------- <- Slide 1
string 1
===================
---------
| img 2 |
--------- <- Slide 2
===================
-----------
| video 1 |
----------- <- Slide 3
string 2
===================
*/
// In this case, all the media files(img1/img2/video1) and text string
// (string1/string2) will be converted into 5 attachments, and SMIL should
// illustrate that this mms message contains 3 slides and the attachment's
// name/layout... in each slide. Here we provide parse and generate utils
// for developers to focus on the necessary information in slides.
// this is a reduce function applied to the array of slides passed to
// SMIL.generate - data will have an attachments and slides array
// slide will be a single "slide", which can have text and an attachment
// See the description of these formats in the comment before
function SMIL_generateSlides(data, slide, slideIndex) {
// default duration to 5 seconds per slide
const DURATION = 5000;
var id;
var blobType;
// each slide can have a piece of media and/or text
var media = '';
var text = '';
var name = '';
if (slide.blob) {
blobType = Utils.typeFromMimeType(slide.blob.type);
if (blobType) {
name = slide.name.substr(slide.name.lastIndexOf('/') + 1);
// just to be safe, remove any non-standard characters from the filename
name = name.replace(unsafeFilenamePattern, '#');
name = SMIL_generateUniqueLocation(data, name);
media = '<' + blobType + ' src="' + name + '" region="Image"/>';
data.attachments.push({
id: '<' + name + '>',
location: name,
content: slide.blob
});
}
}
if (slide.text) {
// Set text region.
id = 'text_' + slideIndex + '.txt';
text = '<text src="' + id + '" region="Text"/>';
// The text of the content blob should always be encoded by 'utf-8'.
data.attachments.push({
id: '<' + id + '>',
location: id,
content: new Blob([encoder.encode(slide.text)], {type: 'text/plain'})
});
}
data.parts.push('<par dur="' + DURATION + 'ms">' + media + text + '</par>');
return data;
}
function SMIL_generateUniqueLocation(data, location) {
// if the location is already being used by the attachment
function SMIL_uniqueLocationMatches(attachment) {
return attachment.location === result;
}
// we will add our number right before the '.' if it exists
var index = location.lastIndexOf('.');
if (index === -1) {
index = location.length;
}
// start with the location given to us
var result = location;
var dupIndex = 2;
// while any attachment already has this location:
while (data.attachments.some(SMIL_uniqueLocationMatches)) {
result = location.slice(0, index) +
'_' + (dupIndex++) + location.slice(index);
}
return result;
}
var SMIL = window.SMIL = {
// SMIL.parse - takes a message from the DOM API's and converts to a
// simple array format:
// message.smil = valid SMIL string, or falsey
// message.attachments = []
// message.attachments[].content = blob
// message.attachments[].location = src attr in smil
// callback(parsedArray):
// parsedArray = []
// parsedArray[].text = 'plain text'
// parsedArray[].name = name of key
// parsedArray[].blob = data blob
parse: function SMIL_parse(message, callback) {
var smil = message.smil;
var attachments = message.attachments;
var slides = [];
var activeReaders = 0;
var attachmentsNotFound = false;
var workingText = [];
var doc;
var parTags;
function readTextBlob(blob, callback) {
// short circuit on null blobs
if (!blob) {
return callback('');
}
var textReader = new FileReader();
textReader. {
activeReaders--;
callback(event, event.target.result);
};
textReader. {
console.error('Error reading text blob');
activeReaders--;
callback(event, '');
};
activeReaders++;
// The text blob must be encoded as 'utf-8' by Gecko.
textReader.readAsText(blob, 'UTF-8');
}
function exitPoint() {
if (!activeReaders) {
callback(slides);
}
}
function findAttachment(name) {
var index = 0;
var length = attachments.length;
// strip the cid: prefix from some MMS encoders
name = name.replace(/^cid:/, '');
for (; index < length; index++) {
if (attachments[index].location === name ||
attachments[index].id === '<' + name + '>') {
return attachments[index];
}
}
return null;
}
function convertWbmpToPng(slide) {
var reader;
slide.name = slide.name.slice(0, -5) + '.png';
reader = new FileReader();
reader. {
WBMP.decode(event.target.result, function callback(blob) {
activeReaders--;
slide.blob = blob;
exitPoint();
});
};
reader. {
activeReaders--;
console.error('Error reading text blob');
exitPoint();
};
activeReaders++;
reader.readAsArrayBuffer(slide.blob);
};
// handle mms messages without smil
// aggregate all text attachments into last slide
function SMIL_parseWithoutSMIL(attachment) {
var slide;
var textIndex = workingText.length;
var blob = attachment.content;
if (!blob) {
return;
}
var type = Utils.typeFromMimeType(blob.type);
// handle text blobs by reading them and converting to text on the
// last slide
if (type === 'text') {
workingText.push('');
readTextBlob(blob, function SMIL_parseAttachmentRead(event, text) {
workingText[textIndex] = text;
// when the last reader finishs, we will join the text together
if (!activeReaders) {
text = workingText.join(' ');
if (slides.length) {
slides[slides.length - 1].text = text;
} else {
slides.push({
text: text
});
}
exitPoint();
}
});
// make sure the type was something we want, otherwise ignore it
} else if (type) {
slide = { name: attachment.location, blob: attachment.content };
if (slide.name && slide.name.slice(-5) === '.wbmp') {
convertWbmpToPng(slide);
}
slides.push(slide);
}
}
function SMIL_parseHandleParTag(par, index) {
// stop parsing as soon as we fail to find one attachment
if (attachmentsNotFound) {
return;
}
var mediaElements = par.querySelectorAll('img, video, audio');
var textElement = par.querySelector('text');
var attachment, src;
Array.prototype.forEach.call(mediaElements, function setSlide(element) {
var slide = {};
src = element.getAttribute('src');
attachment = findAttachment(src);
if (attachment) {
// every media attachment starts its own slide in our format
slide = { name: attachment.location, blob: attachment.content };
slides.push(slide);
if (slide.name && slide.name.slice(-5) === '.wbmp') {
convertWbmpToPng(slide);
}
} else {
attachmentsNotFound = true;
}
});
if (textElement) {
src = textElement.getAttribute('src');
attachment = findAttachment(src);
if (attachment) {
// check for text on the last slide
var slide = slides[slides.length - 1];
// if the last slide doesn't exist, or the last slide has text
// already, we create a new slide to store the text
if (!slide || typeof slide.text !== 'undefined') {
slide = {};
slides.push(slide);
}
// Init slide text to avoid text replaced by later blob
slide.text = '';
// read the text blob, and store it in the "slide" this function
// will hold onto
readTextBlob(attachment.content,
function SMIL_parseSMILAttachmentRead(event, text) {
slide.text = text;
exitPoint();
}
);
} else {
attachmentsNotFound = true;
}
}
}
// handle MMS messages with SMIL
if (smil) {
doc = (new DOMParser()).parseFromString(smil, 'application/xml');
parTags = doc.documentElement.getElementsByTagName('par');
Array.prototype.forEach.call(parTags, SMIL_parseHandleParTag);
}
// handle MMS attachments without SMIL / malformed SMIL
if (!smil || attachmentsNotFound || !slides.length) {
// reset slides in the attachments not found case
slides = [];
attachments.forEach(SMIL_parseWithoutSMIL);
}
exitPoint();
},
// SMIL.generate - takes a array with slides and return smil string and
// attachment array.
generate: function SMIL_generate(slides) {
const HEADER = '<head><layout>' +
'<root-layout width="320px" height="480px"/>' +
'<region id="Image" left="0px" top="0px"' +
' width="320px" height="320px" fit="meet"/>' +
'<region id="Text" left="0px" top="320px"' +
' width="320px" height="160px" fit="meet"/>' +
'</layout></head>';
// generate html from each slide while storing the actual attachment
// in the attachments array.
var data = slides.reduce(SMIL_generateSlides, {
attachments: [],
parts: []
});
// join the html parts together
data.smil = '<smil>' + HEADER + '<body>' +
data.parts.join('') +
'</body></smil>';
// the API doesn't care about 'parts', clean it up
delete data.parts;
return data;
}
};
})();