1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var invariant = require("fbjs/lib/invariant");
|
14 |
|
15 | var TEXT_CLIPPING_REGEX = /\.textClipping$/;
|
16 | var TEXT_TYPES = {
|
17 | 'text/plain': true,
|
18 | 'text/html': true,
|
19 | 'text/rtf': true
|
20 | };
|
21 |
|
22 | var TEXT_SIZE_UPPER_BOUND = 5000;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function getTextContentFromFiles(files, callback) {
|
28 | var readCount = 0;
|
29 | var results = [];
|
30 | files.forEach(function (
|
31 | /*blob*/
|
32 | file) {
|
33 | readFile(file, function (
|
34 | /*string*/
|
35 | text) {
|
36 | readCount++;
|
37 | text && results.push(text.slice(0, TEXT_SIZE_UPPER_BOUND));
|
38 |
|
39 | if (readCount == files.length) {
|
40 | callback(results.join('\r'));
|
41 | }
|
42 | });
|
43 | });
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function readFile(file, callback) {
|
51 | if (!global.FileReader || file.type && !(file.type in TEXT_TYPES)) {
|
52 | callback('');
|
53 | return;
|
54 | }
|
55 |
|
56 | if (file.type === '') {
|
57 | var _contents = '';
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | if (TEXT_CLIPPING_REGEX.test(file.name)) {
|
63 | _contents = file.name.replace(TEXT_CLIPPING_REGEX, '');
|
64 | }
|
65 |
|
66 | callback(_contents);
|
67 | return;
|
68 | }
|
69 |
|
70 | var reader = new FileReader();
|
71 |
|
72 | reader.onload = function () {
|
73 | var result = reader.result;
|
74 | !(typeof result === 'string') ? process.env.NODE_ENV !== "production" ? invariant(false, 'We should be calling "FileReader.readAsText" which returns a string') : invariant(false) : void 0;
|
75 | callback(result);
|
76 | };
|
77 |
|
78 | reader.onerror = function () {
|
79 | callback('');
|
80 | };
|
81 |
|
82 | reader.readAsText(file);
|
83 | }
|
84 |
|
85 | module.exports = getTextContentFromFiles; |
\ | No newline at end of file |