]> git.p6c8.net - jirafeau.git/blob - lib/functions.js
24c7df36f876e29deb920665655232ba1ded7aad
[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, crypt_key, date)
20 {
21 var download_link = url + 'f.php?h=' + reference;
22 var download_link_href = url + 'f.php?h=' + reference;
23 if (crypt_key.length > 0)
24 {
25 download_link += '&amp;k=' + crypt_key;
26 download_link_href += '&k=' + crypt_key;
27 }
28
29 var delete_link = url + 'f.php?h=' + reference + '&amp;d=' + delete_code;
30 var delete_link_href = url + 'f.php?h=' + reference + '&d=' + delete_code;
31
32 document.getElementById('upload_link').innerHTML = download_link;
33 document.getElementById('upload_link').href = download_link_href;
34 document.getElementById('delete_link').innerHTML = delete_link;
35 document.getElementById('delete_link').href = delete_link_href;
36 if (date)
37 {
38 document.getElementById('date').innerHTML = date;
39 document.getElementById('validity').style.display = '';
40 }
41 else
42 document.getElementById('validity').style.display = 'none';
43
44 document.getElementById('uploading').style.display = 'none';
45 document.getElementById('upload').style.display = 'none';
46 document.getElementById('upload_finished').style.display = '';
47 document.title = 'Jirafeau - 100%';
48 }
49
50 function show_upload_progression (p)
51 {
52 document.getElementById('uploaded_percentage').innerHTML = p;
53 document.title = 'Jirafeau - ' + p;
54 }
55
56 function upload_progress (e)
57 {
58 if (!e.lengthComputable)
59 return;
60 /* Show the user the operation do not reach 100%, the server need time
61 * to give a response before providing the link.
62 */
63 var p = Math.round (e.loaded * 100 / e.total);
64 if (p == 100)
65 show_upload_progression (' ');
66 else
67 show_upload_progression (p.toString() + '%');
68 }
69
70 function upload_failed (e)
71 {
72 /* Todo: Considere showing a error div. */
73 alert ('Sorry, upload failed');
74 }
75
76 function classic_upload (url, file, time, password, one_time)
77 {
78 var req = new XMLHttpRequest ();
79 req.upload.addEventListener ("progress", upload_progress, false);
80 req.addEventListener ("error", upload_failed, false);
81 req.addEventListener ("abort", upload_failed, false);
82 req.onreadystatechange = function ()
83 {
84 if (req.readyState == 4 && req.status == 200)
85 {
86 var res = req.responseText;
87 if (res == "Error")
88 return;
89 res = res.split ("\n");
90 if (time != 'none')
91 {
92 var d = new Date();
93 if (time == 'minute')
94 d.setSeconds (d.getSeconds() + 60);
95 else if (time == 'hour')
96 d.setSeconds (d.getSeconds() + 3600);
97 else if (time == 'day')
98 d.setSeconds (d.getSeconds() + 86400);
99 else if (time == 'week')
100 d.setSeconds (d.getSeconds() + 604800);
101 else if (time == 'month')
102 d.setSeconds (d.getSeconds() + 2419200);
103 else
104 return;
105 show_link (url, res[0], res[1], res[2], d.toString());
106 }
107 else
108 show_link (url, res[0], res[1], res[2]);
109 }
110 }
111 req.open ("POST", url + 'script.php' , true);
112
113 var form = new FormData();
114 form.append ("file", file);
115 if (time)
116 form.append ("time", time);
117 if (password)
118 form.append ("key", password);
119 if (one_time)
120 form.append ("one_time_download", '1');
121 req.send (form);
122 }
123
124 function check_html5_file_api ()
125 {
126 if (window.File && window.FileReader && window.FileList && window.Blob)
127 return true;
128 return false;
129 }
130
131 var async_global_transfered = 0;
132 var async_global_url = '';
133 var async_global_file;
134 var async_global_ref = '';
135 var async_global_max_size = 0;
136 var async_global_time;
137 var async_global_transfering = 0;
138
139 function async_upload_start (url, max_size, file, time, password, one_time)
140 {
141 async_global_transfered = 0;
142 async_global_url = url;
143 async_global_file = file;
144 async_global_max_size = max_size;
145 async_global_time = time;
146
147 var req = new XMLHttpRequest ();
148 req.addEventListener ("error", upload_failed, false);
149 req.addEventListener ("abort", upload_failed, false);
150 req.onreadystatechange = function ()
151 {
152 if (req.readyState == 4 && req.status == 200)
153 {
154 var res = req.responseText;
155 if (res == "Error")
156 return;
157 res = res.split ("\n");
158 async_global_ref = res[0];
159 var code = res[1];
160 async_upload_push (code);
161 }
162 }
163 req.open ("POST", async_global_url + 'script.php?init_async' , true);
164
165 var form = new FormData();
166 form.append ("filename", async_global_file.name);
167 form.append ("type", async_global_file.type);
168 if (time)
169 form.append ("time", time);
170 if (password)
171 form.append ("key", password);
172 if (one_time)
173 form.append ("one_time_download", '1');
174 req.send (form);
175 }
176
177 function async_upload_progress (e)
178 {
179 if (!e.lengthComputable && async_global_file.size != 0)
180 return;
181 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
182 if (p == 100)
183 show_upload_progression (' ');
184 else
185 show_upload_progression (p.toString() + '%');
186 }
187
188 function async_upload_push (code)
189 {
190 if (async_global_transfered == async_global_file.size)
191 {
192 async_upload_end (code);
193 return;
194 }
195 var req = new XMLHttpRequest ();
196 req.upload.addEventListener ("progress", async_upload_progress, false);
197 req.addEventListener ("error", upload_failed, false);
198 req.addEventListener ("abort", upload_failed, false);
199 req.onreadystatechange = function ()
200 {
201 if (req.readyState == 4 && req.status == 200)
202 {
203 var res = req.responseText;
204 if (res == "Error")
205 return;
206 res = res.split ("\n");
207 var code = res[0]
208 async_global_transfered = async_global_transfering;
209 async_upload_push (code);
210 }
211 }
212 req.open ("POST", async_global_url + 'script.php?push_async' , true);
213
214 var chunk_size = parseInt (async_global_max_size * 0.50);
215 var start = async_global_transfered;
216 var end = start + chunk_size;
217 if (end >= async_global_file.size)
218 end = async_global_file.size;
219 var blob = async_global_file.slice (start, end);
220 async_global_transfering = end;
221
222 var form = new FormData();
223 form.append ("ref", async_global_ref);
224 form.append ("data", blob);
225 form.append ("code", code);
226 req.send (form);
227 }
228
229 function async_upload_end (code)
230 {
231 var req = new XMLHttpRequest ();
232 req.addEventListener ("error", upload_failed, false);
233 req.addEventListener ("abort", upload_failed, false);
234 req.onreadystatechange = function ()
235 {
236 if (req.readyState == 4 && req.status == 200)
237 {
238 var res = req.responseText;
239 if (res == "Error")
240 return;
241 res = res.split ("\n");
242 if (async_global_time != 'none')
243 {
244 var d = new Date();
245 if (async_global_time == 'minute')
246 d.setSeconds (d.getSeconds() + 60);
247 else if (async_global_time == 'hour')
248 d.setSeconds (d.getSeconds() + 3600);
249 else if (async_global_time == 'day')
250 d.setSeconds (d.getSeconds() + 86400);
251 else if (async_global_time == 'week')
252 d.setSeconds (d.getSeconds() + 604800);
253 else if (async_global_time == 'month')
254 d.setSeconds (d.getSeconds() + 2419200);
255 else
256 return;
257 show_link (async_global_url, res[0], res[1], res[2], d.toString());
258 }
259 else
260 show_link (async_global_url, res[0], res[1], res[2]);
261 }
262 }
263 req.open ("POST", async_global_url + 'script.php?end_async' , true);
264
265 var form = new FormData();
266 form.append ("ref", async_global_ref);
267 form.append ("code", code);
268 req.send (form);
269 }
270
271 function upload (url, max_size)
272 {
273 if (check_html5_file_api ()
274 && document.getElementById('file_select').files[0].size >= max_size)
275 {
276 async_upload_start (url,
277 max_size,
278 document.getElementById('file_select').files[0],
279 document.getElementById('select_time').value,
280 document.getElementById('input_key').value,
281 document.getElementById('one_time_download').checked
282 );
283 }
284 else
285 {
286 classic_upload (url,
287 document.getElementById('file_select').files[0],
288 document.getElementById('select_time').value,
289 document.getElementById('input_key').value,
290 document.getElementById('one_time_download').checked
291 );
292 }
293 }

patrick-canterino.de