]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.js
8922e0e820ff99bb2192c6b65349e27296be518c
2 * Jirafeau, your web file repository
3 * Copyright (C) 2012 Jerome Jutteau <j.jutteau@gmail.com>
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.
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.
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/>.
19 function show_link (url
, reference
, delete_code
, crypt_key
, date
)
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)
25 download_link
+= '&k=' + crypt_key
;
26 download_link_href
+= '&k=' + crypt_key
;
29 var delete_link
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
30 var delete_link_href
= url
+ 'f.php?h=' + reference
+ '&d=' + delete_code
;
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
;
38 document
.getElementById('date').innerHTML
= date
;
39 document
.getElementById('validity').style
.display
= '';
42 document
.getElementById('validity').style
.display
= 'none';
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%';
50 function show_upload_progression (p
)
52 document
.getElementById('uploaded_percentage').innerHTML
= p
;
53 document
.title
= 'Jirafeau - ' + p
;
56 function upload_progress (e
)
58 if (!e
.lengthComputable
)
60 /* Show the user the operation do not reach 100%, the server need time
61 * to give a response before providing the link.
63 var p
= Math
.round (e
.loaded
* 100 / e
.total
);
65 show_upload_progression (' ');
67 show_upload_progression (p
.toString() + '%');
70 function upload_failed (e
)
72 /* Todo: Considere showing a error div. */
73 alert ('Sorry, upload failed');
76 function classic_upload (url
, file
, time
, password
, one_time
, upload_password
)
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 ()
84 if (req
.readyState
== 4 && req
.status
== 200)
86 var res
= req
.responseText
;
89 res
= res
.split ("\n");
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);
105 show_link (url
, res
[0], res
[1], res
[2], d
.toString());
108 show_link (url
, res
[0], res
[1], res
[2]);
111 req
.open ("POST", url
+ 'script.php' , true);
113 var form
= new FormData();
114 form
.append ("file", file
);
116 form
.append ("time", time
);
118 form
.append ("key", password
);
120 form
.append ("one_time_download", '1');
121 if (upload_password
.length
> 0)
122 form
.append ("upload_password", upload_password
);
127 function check_html5_file_api ()
129 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
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;
142 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
, upload_password
)
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
;
150 var req
= new XMLHttpRequest ();
151 req
.addEventListener ("error", upload_failed
, false);
152 req
.addEventListener ("abort", upload_failed
, false);
153 req
.onreadystatechange = function ()
155 if (req
.readyState
== 4 && req
.status
== 200)
157 var res
= req
.responseText
;
160 res
= res
.split ("\n");
161 async_global_ref
= res
[0];
163 async_upload_push (code
);
166 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
168 var form
= new FormData();
169 form
.append ("filename", async_global_file
.name
);
170 form
.append ("type", async_global_file
.type
);
172 form
.append ("time", time
);
174 form
.append ("key", password
);
176 form
.append ("one_time_download", '1');
177 if (upload_password
.length
> 0)
178 form
.append ("upload_password", upload_password
);
183 function async_upload_progress (e
)
185 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
187 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 100 / (async_global_file
.size
));
189 show_upload_progression (' ');
191 show_upload_progression (p
.toString() + '%');
194 function async_upload_push (code
)
196 if (async_global_transfered
== async_global_file
.size
)
198 async_upload_end (code
);
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 ()
207 if (req
.readyState
== 4 && req
.status
== 200)
209 var res
= req
.responseText
;
212 res
= res
.split ("\n");
214 async_global_transfered
= async_global_transfering
;
215 async_upload_push (code
);
218 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
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
;
228 var form
= new FormData();
229 form
.append ("ref", async_global_ref
);
230 form
.append ("data", blob
);
231 form
.append ("code", code
);
235 function async_upload_end (code
)
237 var req
= new XMLHttpRequest ();
238 req
.addEventListener ("error", upload_failed
, false);
239 req
.addEventListener ("abort", upload_failed
, false);
240 req
.onreadystatechange = function ()
242 if (req
.readyState
== 4 && req
.status
== 200)
244 var res
= req
.responseText
;
247 res
= res
.split ("\n");
248 if (async_global_time
!= 'none')
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);
263 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
266 show_link (async_global_url
, res
[0], res
[1], res
[2]);
269 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
271 var form
= new FormData();
272 form
.append ("ref", async_global_ref
);
273 form
.append ("code", code
);
277 function upload (url
, max_size
)
279 if (check_html5_file_api ()
280 && document
.getElementById('file_select').files
[0].size
>= max_size
)
282 async_upload_start (url
,
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
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
patrick-canterino.de