]> git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.js
e3800158e51241f291d2cb7d678599e6a4e885f2
[jirafeau_mojo42.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 upload_failed (e)
129 {
130 /* Todo: Considere showing a error div. */
131 alert ('Sorry, upload failed');
132 }
133
134 function classic_upload (url, file, time, password, one_time, upload_password)
135 {
136 var req = new XMLHttpRequest ();
137 req.upload.addEventListener ("progress", upload_progress, false);
138 req.addEventListener ("error", upload_failed, false);
139 req.addEventListener ("abort", upload_failed, false);
140 req.onreadystatechange = function ()
141 {
142 if (req.readyState == 4 && req.status == 200)
143 {
144 var res = req.responseText;
145 if (res == "Error")
146 return;
147 res = res.split ("\n");
148 if (time != 'none')
149 {
150 var d = new Date();
151 if (time == 'minute')
152 d.setSeconds (d.getSeconds() + 60);
153 else if (time == 'hour')
154 d.setSeconds (d.getSeconds() + 3600);
155 else if (time == 'day')
156 d.setSeconds (d.getSeconds() + 86400);
157 else if (time == 'week')
158 d.setSeconds (d.getSeconds() + 604800);
159 else if (time == 'month')
160 d.setSeconds (d.getSeconds() + 2419200);
161 else if (time == 'year')
162 d.setSeconds (d.getSeconds() + 29030400);
163 else
164 return;
165 show_link (url, res[0], res[1], res[2], d.toString());
166 }
167 else
168 show_link (url, res[0], res[1], res[2]);
169 }
170 }
171 req.open ("POST", url + 'script.php' , true);
172
173 var form = new FormData();
174 form.append ("file", file);
175 if (time)
176 form.append ("time", time);
177 if (password)
178 form.append ("key", password);
179 if (one_time)
180 form.append ("one_time_download", '1');
181 if (upload_password.length > 0)
182 form.append ("upload_password", upload_password);
183
184 req.send (form);
185 }
186
187 function check_html5_file_api ()
188 {
189 if (window.File && window.FileReader && window.FileList && window.Blob)
190 return true;
191 return false;
192 }
193
194 var async_global_transfered = 0;
195 var async_global_url = '';
196 var async_global_file;
197 var async_global_ref = '';
198 var async_global_max_size = 0;
199 var async_global_time;
200 var async_global_transfering = 0;
201
202 function async_upload_start (url, max_size, file, time, password, one_time, upload_password)
203 {
204 async_global_transfered = 0;
205 async_global_url = url;
206 async_global_file = file;
207 async_global_max_size = max_size;
208 async_global_time = time;
209
210 var req = new XMLHttpRequest ();
211 req.addEventListener ("error", upload_failed, false);
212 req.addEventListener ("abort", upload_failed, false);
213 req.onreadystatechange = function ()
214 {
215 if (req.readyState == 4 && req.status == 200)
216 {
217 var res = req.responseText;
218 if (res == "Error")
219 return;
220 res = res.split ("\n");
221 async_global_ref = res[0];
222 var code = res[1];
223 async_upload_push (code);
224 }
225 }
226 req.open ("POST", async_global_url + 'script.php?init_async' , true);
227
228 var form = new FormData();
229 form.append ("filename", async_global_file.name);
230 form.append ("type", async_global_file.type);
231 if (time)
232 form.append ("time", time);
233 if (password)
234 form.append ("key", password);
235 if (one_time)
236 form.append ("one_time_download", '1');
237 if (upload_password.length > 0)
238 form.append ("upload_password", upload_password);
239
240 req.send (form);
241 }
242
243 function async_upload_progress (e)
244 {
245 if (!e.lengthComputable && async_global_file.size != 0)
246 return;
247 var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size));
248 if (p == 100)
249 show_upload_progression (' ');
250 else
251 show_upload_progression (p.toString() + '%');
252 }
253
254 function async_upload_push (code)
255 {
256 if (async_global_transfered == async_global_file.size)
257 {
258 async_upload_end (code);
259 return;
260 }
261 var req = new XMLHttpRequest ();
262 req.upload.addEventListener ("progress", async_upload_progress, false);
263 req.addEventListener ("error", upload_failed, false);
264 req.addEventListener ("abort", upload_failed, false);
265 req.onreadystatechange = function ()
266 {
267 if (req.readyState == 4 && req.status == 200)
268 {
269 var res = req.responseText;
270 if (res == "Error")
271 return;
272 res = res.split ("\n");
273 var code = res[0]
274 async_global_transfered = async_global_transfering;
275 async_upload_push (code);
276 }
277 }
278 req.open ("POST", async_global_url + 'script.php?push_async' , true);
279
280 var chunk_size = parseInt (async_global_max_size * 0.50);
281 var start = async_global_transfered;
282 var end = start + chunk_size;
283 if (end >= async_global_file.size)
284 end = async_global_file.size;
285 var blob = async_global_file.slice (start, end);
286 async_global_transfering = end;
287
288 var form = new FormData();
289 form.append ("ref", async_global_ref);
290 form.append ("data", blob);
291 form.append ("code", code);
292 req.send (form);
293 }
294
295 function async_upload_end (code)
296 {
297 var req = new XMLHttpRequest ();
298 req.addEventListener ("error", upload_failed, false);
299 req.addEventListener ("abort", upload_failed, false);
300 req.onreadystatechange = function ()
301 {
302 if (req.readyState == 4 && req.status == 200)
303 {
304 var res = req.responseText;
305 if (res == "Error")
306 return;
307 res = res.split ("\n");
308 if (async_global_time != 'none')
309 {
310 var d = new Date();
311 if (async_global_time == 'minute')
312 d.setSeconds (d.getSeconds() + 60);
313 else if (async_global_time == 'hour')
314 d.setSeconds (d.getSeconds() + 3600);
315 else if (async_global_time == 'day')
316 d.setSeconds (d.getSeconds() + 86400);
317 else if (async_global_time == 'week')
318 d.setSeconds (d.getSeconds() + 604800);
319 else if (async_global_time == 'month')
320 d.setSeconds (d.getSeconds() + 2419200);
321 else if (async_global_time == 'year')
322 d.setSeconds (d.getSeconds() + 29030400);
323 else
324 return;
325 show_link (async_global_url, res[0], res[1], res[2], d.toString());
326 }
327 else
328 show_link (async_global_url, res[0], res[1], res[2]);
329 }
330 }
331 req.open ("POST", async_global_url + 'script.php?end_async' , true);
332
333 var form = new FormData();
334 form.append ("ref", async_global_ref);
335 form.append ("code", code);
336 req.send (form);
337 }
338
339 function upload (url, max_size)
340 {
341 if (check_html5_file_api ()
342 && document.getElementById('file_select').files[0].size >= max_size)
343 {
344 async_upload_start (url,
345 max_size,
346 document.getElementById('file_select').files[0],
347 document.getElementById('select_time').value,
348 document.getElementById('input_key').value,
349 document.getElementById('one_time_download').checked,
350 document.getElementById('upload_password').value
351 );
352 }
353 else
354 {
355 classic_upload (url,
356 document.getElementById('file_select').files[0],
357 document.getElementById('select_time').value,
358 document.getElementById('input_key').value,
359 document.getElementById('one_time_download').checked,
360 document.getElementById('upload_password').value
361 );
362 }
363 }

patrick-canterino.de