-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFile.js
executable file
·188 lines (156 loc) · 4.28 KB
/
File.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
/*
import Elm.Kernel.Json exposing (decodePrim, expecting)
import Elm.Kernel.List exposing (fromArray)
import Elm.Kernel.Scheduler exposing (binding, succeed)
import Elm.Kernel.Utils exposing (Tuple0, Tuple2)
import Result exposing (Ok)
import String exposing (join)
import Time exposing (millisToPosix)
*/
// DECODER
var _File_decoder = __Json_decodePrim(function(value) {
// NOTE: checks if `File` exists in case this is run on node
return (typeof File !== 'undefined' && value instanceof File)
? __Result_Ok(value)
: __Json_expecting('a FILE', value);
});
// METADATA
function _File_name(file) { return file.name; }
function _File_mime(file) { return file.type; }
function _File_size(file) { return file.size; }
function _File_lastModified(file)
{
return __Time_millisToPosix(file.lastModified);
}
// DOWNLOAD
var _File_downloadNode;
function _File_getDownloadNode()
{
return _File_downloadNode || (_File_downloadNode = document.createElement('a'));
}
var _File_download = F3(function(name, mime, content)
{
return __Scheduler_binding(function(callback)
{
var blob = new Blob([content], {type: mime});
// for IE10+
if (navigator.msSaveOrOpenBlob)
{
navigator.msSaveOrOpenBlob(blob, name);
return;
}
// for HTML5
var node = _File_getDownloadNode();
var objectUrl = URL.createObjectURL(blob);
node.href = objectUrl;
node.download = name;
_File_click(node);
URL.revokeObjectURL(objectUrl);
});
});
function _File_downloadUrl(href)
{
return __Scheduler_binding(function(callback)
{
var node = _File_getDownloadNode();
node.href = href;
node.download = '';
node.origin === location.origin || (node.target = '_blank');
_File_click(node);
});
}
// IE COMPATIBILITY
function _File_makeBytesSafeForInternetExplorer(bytes)
{
// only needed by IE10 and IE11 to fix https://github.com/elm/file/issues/10
// all other browsers can just run `new Blob([bytes])` directly with no problem
//
return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
}
function _File_click(node)
{
// only needed by IE10 and IE11 to fix https://github.com/elm/file/issues/11
// all other browsers have MouseEvent and do not need this conditional stuff
//
if (typeof MouseEvent === 'function')
{
node.dispatchEvent(new MouseEvent('click'));
}
else
{
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
document.body.appendChild(node);
node.dispatchEvent(event);
document.body.removeChild(node);
}
}
// UPLOAD
var _File_node;
function _File_uploadOne(mimes)
{
return __Scheduler_binding(function(callback)
{
_File_node = document.createElement('input');
_File_node.type = 'file';
_File_node.accept = A2(__String_join, ',', mimes);
_File_node.addEventListener('change', function(event)
{
callback(__Scheduler_succeed(event.target.files[0]));
});
_File_click(_File_node);
});
}
function _File_uploadOneOrMore(mimes)
{
return __Scheduler_binding(function(callback)
{
_File_node = document.createElement('input');
_File_node.type = 'file';
_File_node.multiple = true;
_File_node.accept = A2(__String_join, ',', mimes);
_File_node.addEventListener('change', function(event)
{
var elmFiles = __List_fromArray(event.target.files);
callback(__Scheduler_succeed(__Utils_Tuple2(elmFiles.a, elmFiles.b)));
});
_File_click(_File_node);
});
}
// CONTENT
function _File_toString(blob)
{
return __Scheduler_binding(function(callback)
{
var reader = new FileReader();
reader.addEventListener('loadend', function() {
callback(__Scheduler_succeed(reader.result));
});
reader.readAsText(blob);
return function() { reader.abort(); };
});
}
function _File_toBytes(blob)
{
return __Scheduler_binding(function(callback)
{
var reader = new FileReader();
reader.addEventListener('loadend', function() {
callback(__Scheduler_succeed(new DataView(reader.result)));
});
reader.readAsArrayBuffer(blob);
return function() { reader.abort(); };
});
}
function _File_toUrl(blob)
{
return __Scheduler_binding(function(callback)
{
var reader = new FileReader();
reader.addEventListener('loadend', function() {
callback(__Scheduler_succeed(reader.result));
});
reader.readAsDataURL(blob);
return function() { reader.abort(); };
});
}