]> git.p6c8.net - jirafeau_project.git/blob - lib/functions.js
use scripting interface to send files
[jirafeau_project.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
37 document.getElementById('uploading').style.display = 'none';
38 document.getElementById('upload').style.display = 'none';
39 document.getElementById('upload_finished').style.display = '';
40 }
41
42 function upload_progress (e)
43 {
44 if (!e.lengthComputable)
45 return;
46 /* Show the user the operation do not reach 100%, the server need time
47 * to give a response before providing the link.
48 */
49 var p = Math.round (e.loaded * 99 / e.total);
50 document.getElementById('uploaded_percentage').innerHTML = p.toString() + '%';
51 }
52
53 function upload_failed (e)
54 {
55 /* Todo: Considere showing a error div. */
56 alert ('Sorry, upload failed');
57 }
58
59 function upload (url, file, time, password, one_time)
60 {
61 var req = new XMLHttpRequest ();
62 req.upload.addEventListener("progress", upload_progress, false);
63 req.addEventListener("error", upload_failed, false);
64 req.addEventListener("abort", upload_failed, false);
65 req.onreadystatechange = function ()
66 {
67 if (req.readyState == 4 && req.status == 200)
68 {
69 var res = req.responseText;
70 if (res == "Error")
71 return;
72 res = res.split ("\n");
73 if (time != 'none')
74 {
75 var d = new Date();
76 if (time == 'minute')
77 d.setSeconds (d.getSeconds() + 60);
78 else if (time == 'hour')
79 d.setSeconds (d.getSeconds() + 3600);
80 else if (time == 'day')
81 d.setSeconds (d.getSeconds() + 86400);
82 else if (time == 'week')
83 d.setSeconds (d.getSeconds() + 604800);
84 else if (time == 'month')
85 d.setSeconds (d.getSeconds() + 2419200);
86 show_link (url, res[0], res[1], d.toString());
87 }
88 else
89 show_link (url, res[0], res[1]);
90 }
91 }
92 req.open ("POST", url + 'script.php' , true);
93
94 var form = new FormData();
95 form.append ("file", file);
96 if (time)
97 form.append ("time", time);
98 if (password)
99 form.append ("key", password);
100 if (one_time)
101 form.append ("one_time_download", '1');
102 req.send (form);
103 }
104
105 function start_upload (url)
106 {
107 upload (url,
108 document.getElementById('file_select').files[0],
109 document.getElementById('select_time').value,
110 document.getElementById('input_key').value,
111 document.getElementById('one_time_download').checked
112 );
113 }

patrick-canterino.de