]> git.p6c8.net - jirafeau_project.git/blob - lib/functions.js
Fixes #7 Jirafeau can now limit file size
[jirafeau_project.git] / lib / functions.js
1 /*
2 * Jirafeau, your web file repository
3 * Copyright (C) 2015 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 // Download page if element exists
22 var download_link = url + 'f.php?h=' + reference;
23 var download_link_href = url + 'f.php?h=' + reference;
24 if (crypt_key.length > 0)
25 {
26 download_link += '&amp;k=' + crypt_key;
27 download_link_href += '&k=' + crypt_key;
28 }
29 if (!!document.getElementById('upload_finished_download_page'))
30 {
31 document.getElementById('upload_link').innerHTML = download_link;
32 document.getElementById('upload_link').href = download_link_href;
33 }
34
35 // Is the preview allowed ?
36 if (!!document.getElementById('preview_link'))
37 {
38 document.getElementById('upload_finished_preview').style.display = 'none';
39 var preview_link = url + 'f.php?h=' + reference + '&amp;p=1';
40 var preview_link_href = url + 'f.php?h=' + reference + '&p=1';
41 if (crypt_key.length > 0)
42 {
43 preview_link += '&amp;k=' + crypt_key;
44 preview_link_href += '&k=' + crypt_key;
45 }
46
47 // Test if content can be previewed
48 type = document.getElementById('file_select').files[0].type;
49 if (type.indexOf("image") > -1 ||
50 type.indexOf("audio") > -1 ||
51 type.indexOf("text") > -1 ||
52 type.indexOf("video") > -1)
53 {
54 document.getElementById('preview_link').innerHTML = preview_link;
55 document.getElementById('preview_link').href = preview_link_href;
56 document.getElementById('upload_finished_preview').style.display = '';
57 }
58 }
59
60 // Only show link to password page if password is set
61 document.getElementById('upload_password_page').style.display = 'none';
62 if (document.getElementById('input_key').value.length > 0)
63 {
64 if (!!document.getElementById('upload_finished_download_page'))
65 document.getElementById('upload_finished_download_page').style.display = 'none';
66 document.getElementById('upload_password_page').style.display = '';
67 if (!!document.getElementById('upload_finished_preview'))
68 document.getElementById('upload_finished_preview').style.display = 'none';
69 document.getElementById('upload_direct_download').style.display = 'none';
70
71 document.getElementById('password_link').innerHTML = download_link;
72 document.getElementById('password_link').href = download_link_href;
73 }
74 // Direct download link
75 else
76 {
77 var direct_download_link = url + 'f.php?h=' + reference + '&amp;d=1';
78 var direct_download_link_href = url + 'f.php?h=' + reference + '&d=1';
79 if (crypt_key.length > 0)
80 {
81 direct_download_link += '&amp;k=' + crypt_key;
82 direct_download_link_href += '&k=' + crypt_key;
83 }
84 document.getElementById('direct_link').innerHTML = direct_download_link;
85 document.getElementById('direct_link').href = direct_download_link_href;
86 }
87
88 // Delete link
89 var delete_link = url + 'f.php?h=' + reference + '&amp;d=' + delete_code;
90 var delete_link_href = url + 'f.php?h=' + reference + '&d=' + delete_code;
91 document.getElementById('delete_link').innerHTML = delete_link;
92 document.getElementById('delete_link').href = delete_link_href;
93
94 if (date)
95 {
96 document.getElementById('date').innerHTML = date;
97 document.getElementById('validity').style.display = '';
98 }
99 else
100 document.getElementById('validity').style.display = 'none';
101
102 document.getElementById('uploading').style.display = 'none';
103 document.getElementById('upload').style.display = 'none';
104 document.getElementById('upload_finished').style.display = '';
105 document.title = 'Jirafeau - 100%';
106 }
107
108 function show_upload_progression (p)
109 {
110 document.getElementById('uploaded_percentage').innerHTML = p;
111 document.title = 'Jirafeau - ' + p;
112 }
113
114 function upload_progress (e)
115 {
116 if (!e.lengthComputable)
117 return;
118 /* Show the user the operation do not reach 100%, the server need time
119 * to give a response before providing the link.
120 */
121 var p = Math.round (e.loaded * 100 / e.total);
122 if (p == 100)
123 show_upload_progression (' ');
124 else
125 show_upload_progression (p.toString() + '%');
126 }
127
128 function control_selected_file_size(max_size, error_str)
129 {
130 f_size = document.getElementById('file_select').files[0].size;
131 if (max_size > 0 && f_size > max_size * 1024 * 1024)
132 {
133 pop_failure(error_str);
134 document.getElementById('send').style.display = 'none';
135 }
136 else
137 {
138 document.getElementById('options').style.display = '';
139 document.getElementById('send').style.display = '';
140 document.getElementById('error_pop').style.display = 'none';
141 }
142 }
143
144 function pop_failure (e)
145 {
146 var text = "An error occured";
147 if (typeof e !== 'undefined')
148 text = e;
149 text = "<p>" + text + "</p>";
150 document.getElementById('error_pop').innerHTML = e;
151
152 document.getElementById('uploading').style.display = 'none';
153 document.getElementById('error_pop').style.display = '';
154 document.getElementById('upload').style.display = '';
155 document.getElementById('send').style.display = '';
156 }
157
158 function classic_upload (url, file, time, password, one_time, upload_password)
159 {
160 var req = new XMLHttpRequest ();
161 req.upload.addEventListener ("progress", upload_progress, false);
162 req.addEventListener ("error", pop_failure, false);
163 req.addEventListener ("abort", pop_failure, false);
164 req.onreadystatechange = function ()
165 {
166 if (req.readyState == 4 && req.status == 200)
167 {
168 var res = req.responseText;
169 if (res == "Error")
170 {
171 pop_failure ();
172 return;
173 }
174 res = res.split ("\n");
175 if (time != 'none')
176 {
177 var d = new Date();
178 if (time == 'minute')
179 d.setSeconds (d.getSeconds() + 60);
180 else if (time == 'hour')
181 d.setSeconds (d.getSeconds() + 3600);
182 else if (time == 'day')
183 d.setSeconds (d.getSeconds() + 86400);
184 else if (time == 'week')
185 d.setSeconds (d.getSeconds() + 604800);
186 else if (time == 'month')
187 d.setSeconds (d.getSeconds() + 2419200);
188 else if (time == 'year')
189 d.setSeconds (d.getSeconds() + 29030400);
190 else
191 return;
192 show_link (url, res[0], res[1], res[2], d.toString());
193 }
194 else
195 show_link (url, res[0], res[1], res[2]);
196 }
197 }
198 req.open ("POST", url + 'script.php' , true);
199
200 var form = new FormData();
201 form.append ("file", file);
202 if (time)
203 form.append ("time", time);
204 if (password)
205 form.append ("key", password);
206 if (one_time)
207 form.append ("one_time_download", '1');
208 if (upload_password.length > 0)
209 form.append ("upload_password", upload_password);
210
211 req.send (form);
212 }
213
214 function check_html5_file_api ()
215 {
216 if (window.File && window.FileReader && window.FileList && window.Blob)
217 return true;
218 return false;
219 }
220
221 var async_global_transfered = 0;
222 var async_global_url = '';
223 var async_global_file;
224 var async_global_ref = '';
225 var async_global_max_size = 0;
226 var async_global_time;
227 var async_global_transfering = 0;
228
229 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
230 {
231 async_global_transfered = 0;
232 async_global_url = url;
233 async_global_file = file;
234 async_global_max_size = max_size;
235 async_global_time = time;
236
237 var req = new XMLHttpRequest ();
238 req.addEventListener ("error", pop_failure, false);
239 req.addEventListener ("abort", pop_failure, false);
240 req.onreadystatechange = function ()
241 {
242 if (req.readyState == 4 && req.status == 200)
243 {
244 var res = req.responseText;
245 if (res == "Error")
246 {
247 pop_failure ();
248 return;
249 }
250 res = res.split ("\n");
251 async_global_ref = res[0];
252 var code = res[1];
253 async_upload_push (code);
254 }
255 }
256 req.open ("POST", async_global_url + 'script.php?init_async' , true);
257
258 var form = new FormData();
259 form.append ("filename", async_global_file.name);
260 form.append ("type", async_global_file.type);
261 if (time)
262 form.append ("time", time);
263 if (password)
264 form.append ("key", password);
265 if (one_time)
266 form.append ("one_time_download", '1');
267 if (upload_password.length > 0)
268 form.append ("upload_password", upload_password);
269
270 req.send (form);
271 }
272
273 function async_upload_progress (e)
274 {
275 if (!e.lengthComputable && async_global_file.size != 0)
276 return;
277 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
278 if (p == 100)
279 show_upload_progression (' ');
280 else
281 show_upload_progression (p.toString() + '%');
282 }
283
284 function async_upload_push (code)
285 {
286 if (async_global_transfered == async_global_file.size)
287 {
288 async_upload_end (code);
289 return;
290 }
291 var req = new XMLHttpRequest ();
292 req.upload.addEventListener ("progress", async_upload_progress, false);
293 req.addEventListener ("error", pop_failure, false);
294 req.addEventListener ("abort", pop_failure, false);
295 req.onreadystatechange = function ()
296 {
297 if (req.readyState == 4 && req.status == 200)
298 {
299 var res = req.responseText;
300 if (res == "Error")
301 {
302 pop_failure ();
303 return;
304 }
305 res = res.split ("\n");
306 var code = res[0]
307 async_global_transfered = async_global_transfering;
308 async_upload_push (code);
309 }
310 }
311 req.open ("POST", async_global_url + 'script.php?push_async' , true);
312
313 var chunk_size = parseInt (async_global_max_size * 0.50);
314 var start = async_global_transfered;
315 var end = start + chunk_size;
316 if (end >= async_global_file.size)
317 end = async_global_file.size;
318 var blob = async_global_file.slice (start, end);
319 async_global_transfering = end;
320
321 var form = new FormData();
322 form.append ("ref", async_global_ref);
323 form.append ("data", blob);
324 form.append ("code", code);
325 req.send (form);
326 }
327
328 function async_upload_end (code)
329 {
330 var req = new XMLHttpRequest ();
331 req.addEventListener ("error", pop_failure, false);
332 req.addEventListener ("abort", pop_failure, false);
333 req.onreadystatechange = function ()
334 {
335 if (req.readyState == 4 && req.status == 200)
336 {
337 var res = req.responseText;
338 if (res == "Error")
339 {
340 pop_failure ();
341 return;
342 }
343 res = res.split ("\n");
344 if (async_global_time != 'none')
345 {
346 var d = new Date();
347 if (async_global_time == 'minute')
348 d.setSeconds (d.getSeconds() + 60);
349 else if (async_global_time == 'hour')
350 d.setSeconds (d.getSeconds() + 3600);
351 else if (async_global_time == 'day')
352 d.setSeconds (d.getSeconds() + 86400);
353 else if (async_global_time == 'week')
354 d.setSeconds (d.getSeconds() + 604800);
355 else if (async_global_time == 'month')
356 d.setSeconds (d.getSeconds() + 2419200);
357 else if (async_global_time == 'year')
358 d.setSeconds (d.getSeconds() + 29030400);
359 else
360 return;
361 show_link (async_global_url, res[0], res[1], res[2], d.toString());
362 }
363 else
364 show_link (async_global_url, res[0], res[1], res[2]);
365 }
366 }
367 req.open ("POST", async_global_url + 'script.php?end_async' , true);
368
369 var form = new FormData();
370 form.append ("ref", async_global_ref);
371 form.append ("code", code);
372 req.send (form);
373 }
374
375 function upload (url, max_size)
376 {
377 if (check_html5_file_api ()
378 && document.getElementById('file_select').files[0].size >= max_size)
379 {
380 async_upload_start (url,
381 max_size,
382 document.getElementById('file_select').files[0],
383 document.getElementById('select_time').value,
384 document.getElementById('input_key').value,
385 document.getElementById('one_time_download').checked,
386 document.getElementById('upload_password').value
387 );
388 }
389 else
390 {
391 classic_upload (url,
392 document.getElementById('file_select').files[0],
393 document.getElementById('select_time').value,
394 document.getElementById('input_key').value,
395 document.getElementById('one_time_download').checked,
396 document.getElementById('upload_password').value
397 );
398 }
399 }

patrick-canterino.de