]> git.p6c8.net - jirafeau.git/blob - lib/functions.js
Add poucentage of transfert in title
[jirafeau.git] / lib / functions.js
1 /*
2 * Jirafeau, your web file repository
3 * Copyright (C) 2012 Jerome Jutteau <j.jutteau@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 function show_link (url, reference, delete_code, date)
20 {
21 var download_link = url + 'file.php?h=' + reference;
22 var delete_link = download_link + '&amp;d=' + delete_code;
23 var delete_link_href = download_link + '&d=' + delete_code;
24 document.getElementById('upload_link').innerHTML = download_link;
25 document.getElementById('upload_link').href = download_link;
26 document.getElementById('delete_link').innerHTML = delete_link;
27 document.getElementById('delete_link').href = delete_link_href;
28 if (date)
29 {
30 document.getElementById('date').innerHTML = date;
31 document.getElementById('validity').style.display = '';
32 }
33 else
34 document.getElementById('validity').style.display = 'none';
35
36 document.getElementById('uploading').style.display = 'none';
37 document.getElementById('upload').style.display = 'none';
38 document.getElementById('upload_finished').style.display = '';
39 }
40
41 function show_upload_progression (p)
42 {
43 document.getElementById('uploaded_percentage').innerHTML = p;
44 document.title = 'Jirafeau - ' + p;
45 }
46
47 function upload_progress (e)
48 {
49 if (!e.lengthComputable)
50 return;
51 /* Show the user the operation do not reach 100%, the server need time
52 * to give a response before providing the link.
53 */
54 var p = Math.round (e.loaded * 99 / e.total);
55 show_upload_progression (p.toString() + '%');
56 }
57
58 function upload_failed (e)
59 {
60 /* Todo: Considere showing a error div. */
61 alert ('Sorry, upload failed');
62 }
63
64 function classic_upload (url, file, time, password, one_time)
65 {
66 var req = new XMLHttpRequest ();
67 req.upload.addEventListener ("progress", upload_progress, false);
68 req.addEventListener ("error", upload_failed, false);
69 req.addEventListener ("abort", upload_failed, false);
70 req.onreadystatechange = function ()
71 {
72 if (req.readyState == 4 && req.status == 200)
73 {
74 var res = req.responseText;
75 if (res == "Error")
76 return;
77 res = res.split ("\n");
78 if (time != 'none')
79 {
80 var d = new Date();
81 if (time == 'minute')
82 d.setSeconds (d.getSeconds() + 60);
83 else if (time == 'hour')
84 d.setSeconds (d.getSeconds() + 3600);
85 else if (time == 'day')
86 d.setSeconds (d.getSeconds() + 86400);
87 else if (time == 'week')
88 d.setSeconds (d.getSeconds() + 604800);
89 else if (time == 'month')
90 d.setSeconds (d.getSeconds() + 2419200);
91 else
92 return;
93 show_link (url, res[0], res[1], d.toString());
94 }
95 else
96 show_link (url, res[0], res[1]);
97 }
98 }
99 req.open ("POST", url + 'script.php' , true);
100
101 var form = new FormData();
102 form.append ("file", file);
103 if (time)
104 form.append ("time", time);
105 if (password)
106 form.append ("key", password);
107 if (one_time)
108 form.append ("one_time_download", '1');
109 req.send (form);
110 }
111
112 function check_html5_file_api ()
113 {
114 if (window.File && window.FileReader && window.FileList && window.Blob)
115 return true;
116 return false;
117 }
118
119 var async_global_transfered = 0;
120 var async_global_url = '';
121 var async_global_file;
122 var async_global_ref = '';
123 var async_global_max_size = 0;
124 var async_global_time;
125 var async_global_transfering = 0;
126
127 function async_upload_start (url, max_size, file, time, password, one_time)
128 {
129 async_global_transfered = 0;
130 async_global_url = url;
131 async_global_file = file;
132 async_global_max_size = max_size;
133 async_global_time = time;
134
135 var req = new XMLHttpRequest ();
136 req.addEventListener ("error", upload_failed, false);
137 req.addEventListener ("abort", upload_failed, false);
138 req.onreadystatechange = function ()
139 {
140 if (req.readyState == 4 && req.status == 200)
141 {
142 var res = req.responseText;
143 if (res == "Error")
144 return;
145 res = res.split ("\n");
146 async_global_ref = res[0];
147 var code = res[1];
148 async_upload_push (code);
149 }
150 }
151 req.open ("POST", async_global_url + 'script.php?init_async' , true);
152
153 var form = new FormData();
154 form.append ("filename", async_global_file.name);
155 form.append ("type", async_global_file.type);
156 if (time)
157 form.append ("time", time);
158 if (password)
159 form.append ("key", password);
160 if (one_time)
161 form.append ("one_time_download", '1');
162 req.send (form);
163 }
164
165 function async_upload_progress (e)
166 {
167 if (!e.lengthComputable && async_global_file.size != 0)
168 return;
169 var p = Math.round ((e.loaded + async_global_transfered) * 99 / (async_global_file.size));
170 show_upload_progression (p.toString() + '%');
171 }
172
173 function async_upload_push (code)
174 {
175 if (async_global_transfered == async_global_file.size)
176 {
177 async_upload_end (code);
178 return;
179 }
180 var req = new XMLHttpRequest ();
181 req.upload.addEventListener ("progress", async_upload_progress, false);
182 req.addEventListener ("error", upload_failed, false);
183 req.addEventListener ("abort", upload_failed, false);
184 req.onreadystatechange = function ()
185 {
186 if (req.readyState == 4 && req.status == 200)
187 {
188 var res = req.responseText;
189 if (res == "Error")
190 return;
191 res = res.split ("\n");
192 var code = res[0]
193 async_global_transfered = async_global_transfering;
194 async_upload_push (code);
195 }
196 }
197 req.open ("POST", async_global_url + 'script.php?push_async' , true);
198
199 var chunk_size = parseInt (async_global_max_size * 0.90);
200 var start = async_global_transfered;
201 var end = start + chunk_size;
202 if (end >= async_global_file.size)
203 end = async_global_file.size;
204 var blob = async_global_file.slice (start, end);
205 async_global_transfering = end;
206
207 var form = new FormData();
208 form.append ("ref", async_global_ref);
209 form.append ("data", blob);
210 form.append ("code", code);
211 req.send (form);
212 }
213
214 function async_upload_end (code)
215 {
216 var req = new XMLHttpRequest ();
217 req.addEventListener ("error", upload_failed, false);
218 req.addEventListener ("abort", upload_failed, false);
219 req.onreadystatechange = function ()
220 {
221 if (req.readyState == 4 && req.status == 200)
222 {
223 var res = req.responseText;
224 if (res == "Error")
225 return;
226 res = res.split ("\n");
227 if (async_global_time != 'none')
228 {
229 var d = new Date();
230 if (async_global_time == 'minute')
231 d.setSeconds (d.getSeconds() + 60);
232 else if (async_global_time == 'hour')
233 d.setSeconds (d.getSeconds() + 3600);
234 else if (async_global_time == 'day')
235 d.setSeconds (d.getSeconds() + 86400);
236 else if (async_global_time == 'week')
237 d.setSeconds (d.getSeconds() + 604800);
238 else if (async_global_time == 'month')
239 d.setSeconds (d.getSeconds() + 2419200);
240 else
241 return;
242 show_link (async_global_url, res[0], res[1], d.toString());
243 }
244 else
245 show_link (async_global_url, res[0], res[1]);
246 }
247 }
248 req.open ("POST", async_global_url + 'script.php?end_async' , true);
249
250 var form = new FormData();
251 form.append ("ref", async_global_ref);
252 form.append ("code", code);
253 req.send (form);
254 }
255
256 function upload (url, max_size)
257 {
258 if (check_html5_file_api ()
259 && document.getElementById('file_select').files[0].size >= max_size)
260 {
261 async_upload_start (url,
262 max_size,
263 document.getElementById('file_select').files[0],
264 document.getElementById('select_time').value,
265 document.getElementById('input_key').value,
266 document.getElementById('one_time_download').checked
267 );
268 }
269 else
270 {
271 classic_upload (url,
272 document.getElementById('file_select').files[0],
273 document.getElementById('select_time').value,
274 document.getElementById('input_key').value,
275 document.getElementById('one_time_download').checked
276 );
277 }
278 }

patrick-canterino.de