]> git.p6c8.net - jirafeau_project.git/blob - lib/functions.js
add some config documentation
[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 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, upload_password)
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 if (upload_password.length > 0)
122 form.append ("upload_password", upload_password);
123
124 req.send (form);
125 }
126
127 function check_html5_file_api ()
128 {
129 if (window.File && window.FileReader && window.FileList && window.Blob)
130 return true;
131 return false;
132 }
133
134 var async_global_transfered = 0;
135 var async_global_url = '';
136 var async_global_file;
137 var async_global_ref = '';
138 var async_global_max_size = 0;
139 var async_global_time;
140 var async_global_transfering = 0;
141
142 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
143 {
144 async_global_transfered = 0;
145 async_global_url = url;
146 async_global_file = file;
147 async_global_max_size = max_size;
148 async_global_time = time;
149
150 var req = new XMLHttpRequest ();
151 req.addEventListener ("error", upload_failed, false);
152 req.addEventListener ("abort", upload_failed, false);
153 req.onreadystatechange = function ()
154 {
155 if (req.readyState == 4 && req.status == 200)
156 {
157 var res = req.responseText;
158 if (res == "Error")
159 return;
160 res = res.split ("\n");
161 async_global_ref = res[0];
162 var code = res[1];
163 async_upload_push (code);
164 }
165 }
166 req.open ("POST", async_global_url + 'script.php?init_async' , true);
167
168 var form = new FormData();
169 form.append ("filename", async_global_file.name);
170 form.append ("type", async_global_file.type);
171 if (time)
172 form.append ("time", time);
173 if (password)
174 form.append ("key", password);
175 if (one_time)
176 form.append ("one_time_download", '1');
177 if (upload_password.length > 0)
178 form.append ("upload_password", upload_password);
179
180 req.send (form);
181 }
182
183 function async_upload_progress (e)
184 {
185 if (!e.lengthComputable && async_global_file.size != 0)
186 return;
187 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
188 if (p == 100)
189 show_upload_progression (' ');
190 else
191 show_upload_progression (p.toString() + '%');
192 }
193
194 function async_upload_push (code)
195 {
196 if (async_global_transfered == async_global_file.size)
197 {
198 async_upload_end (code);
199 return;
200 }
201 var req = new XMLHttpRequest ();
202 req.upload.addEventListener ("progress", async_upload_progress, false);
203 req.addEventListener ("error", upload_failed, false);
204 req.addEventListener ("abort", upload_failed, false);
205 req.onreadystatechange = function ()
206 {
207 if (req.readyState == 4 && req.status == 200)
208 {
209 var res = req.responseText;
210 if (res == "Error")
211 return;
212 res = res.split ("\n");
213 var code = res[0]
214 async_global_transfered = async_global_transfering;
215 async_upload_push (code);
216 }
217 }
218 req.open ("POST", async_global_url + 'script.php?push_async' , true);
219
220 var chunk_size = parseInt (async_global_max_size * 0.50);
221 var start = async_global_transfered;
222 var end = start + chunk_size;
223 if (end >= async_global_file.size)
224 end = async_global_file.size;
225 var blob = async_global_file.slice (start, end);
226 async_global_transfering = end;
227
228 var form = new FormData();
229 form.append ("ref", async_global_ref);
230 form.append ("data", blob);
231 form.append ("code", code);
232 req.send (form);
233 }
234
235 function async_upload_end (code)
236 {
237 var req = new XMLHttpRequest ();
238 req.addEventListener ("error", upload_failed, false);
239 req.addEventListener ("abort", upload_failed, 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 return;
247 res = res.split ("\n");
248 if (async_global_time != 'none')
249 {
250 var d = new Date();
251 if (async_global_time == 'minute')
252 d.setSeconds (d.getSeconds() + 60);
253 else if (async_global_time == 'hour')
254 d.setSeconds (d.getSeconds() + 3600);
255 else if (async_global_time == 'day')
256 d.setSeconds (d.getSeconds() + 86400);
257 else if (async_global_time == 'week')
258 d.setSeconds (d.getSeconds() + 604800);
259 else if (async_global_time == 'month')
260 d.setSeconds (d.getSeconds() + 2419200);
261 else
262 return;
263 show_link (async_global_url, res[0], res[1], res[2], d.toString());
264 }
265 else
266 show_link (async_global_url, res[0], res[1], res[2]);
267 }
268 }
269 req.open ("POST", async_global_url + 'script.php?end_async' , true);
270
271 var form = new FormData();
272 form.append ("ref", async_global_ref);
273 form.append ("code", code);
274 req.send (form);
275 }
276
277 function upload (url, max_size)
278 {
279 if (check_html5_file_api ()
280 && document.getElementById('file_select').files[0].size >= max_size)
281 {
282 async_upload_start (url,
283 max_size,
284 document.getElementById('file_select').files[0],
285 document.getElementById('select_time').value,
286 document.getElementById('input_key').value,
287 document.getElementById('one_time_download').checked,
288 document.getElementById('upload_password').value
289 );
290 }
291 else
292 {
293 classic_upload (url,
294 document.getElementById('file_select').files[0],
295 document.getElementById('select_time').value,
296 document.getElementById('input_key').value,
297 document.getElementById('one_time_download').checked,
298 document.getElementById('upload_password').value
299 );
300 }
301 }

patrick-canterino.de