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

patrick-canterino.de