]>
git.p6c8.net - jirafeau_project.git/blob - lib/functions.js
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
)
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');
124 function check_html5_file_api ()
126 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
131 var async_global_transfered
= 0;
132 var async_global_url
= '';
133 var async_global_file
;
134 var async_global_ref
= '';
135 var async_global_max_size
= 0;
136 var async_global_time
;
137 var async_global_transfering
= 0;
139 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
)
141 async_global_transfered
= 0;
142 async_global_url
= url
;
143 async_global_file
= file
;
144 async_global_max_size
= max_size
;
145 async_global_time
= time
;
147 var req
= new XMLHttpRequest ();
148 req
.addEventListener ("error", upload_failed
, false);
149 req
.addEventListener ("abort", upload_failed
, false);
150 req
.onreadystatechange = function ()
152 if (req
.readyState
== 4 && req
.status
== 200)
154 var res
= req
.responseText
;
157 res
= res
.split ("\n");
158 async_global_ref
= res
[0];
160 async_upload_push (code
);
163 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
165 var form
= new FormData();
166 form
.append ("filename", async_global_file
.name
);
167 form
.append ("type", async_global_file
.type
);
169 form
.append ("time", time
);
171 form
.append ("key", password
);
173 form
.append ("one_time_download", '1');
177 function async_upload_progress (e
)
179 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
181 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 100 / (async_global_file
.size
));
183 show_upload_progression (' ');
185 show_upload_progression (p
.toString() + '%');
188 function async_upload_push (code
)
190 if (async_global_transfered
== async_global_file
.size
)
192 async_upload_end (code
);
195 var req
= new XMLHttpRequest ();
196 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
197 req
.addEventListener ("error", upload_failed
, false);
198 req
.addEventListener ("abort", upload_failed
, false);
199 req
.onreadystatechange = function ()
201 if (req
.readyState
== 4 && req
.status
== 200)
203 var res
= req
.responseText
;
206 res
= res
.split ("\n");
208 async_global_transfered
= async_global_transfering
;
209 async_upload_push (code
);
212 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
214 var chunk_size
= parseInt (async_global_max_size
* 0.50);
215 var start
= async_global_transfered
;
216 var end
= start
+ chunk_size
;
217 if (end
>= async_global_file
.size
)
218 end
= async_global_file
.size
;
219 var blob
= async_global_file
.slice (start
, end
);
220 async_global_transfering
= end
;
222 var form
= new FormData();
223 form
.append ("ref", async_global_ref
);
224 form
.append ("data", blob
);
225 form
.append ("code", code
);
229 function async_upload_end (code
)
231 var req
= new XMLHttpRequest ();
232 req
.addEventListener ("error", upload_failed
, false);
233 req
.addEventListener ("abort", upload_failed
, false);
234 req
.onreadystatechange = function ()
236 if (req
.readyState
== 4 && req
.status
== 200)
238 var res
= req
.responseText
;
241 res
= res
.split ("\n");
242 if (async_global_time
!= 'none')
245 if (async_global_time
== 'minute')
246 d
.setSeconds (d
.getSeconds() + 60);
247 else if (async_global_time
== 'hour')
248 d
.setSeconds (d
.getSeconds() + 3600);
249 else if (async_global_time
== 'day')
250 d
.setSeconds (d
.getSeconds() + 86400);
251 else if (async_global_time
== 'week')
252 d
.setSeconds (d
.getSeconds() + 604800);
253 else if (async_global_time
== 'month')
254 d
.setSeconds (d
.getSeconds() + 2419200);
257 show_link (async_global_url
, res
[0], res
[1], res
[2], d
.toString());
260 show_link (async_global_url
, res
[0], res
[1], res
[2]);
263 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
265 var form
= new FormData();
266 form
.append ("ref", async_global_ref
);
267 form
.append ("code", code
);
271 function upload (url
, max_size
)
273 if (check_html5_file_api ()
274 && document
.getElementById('file_select').files
[0].size
>= max_size
)
276 async_upload_start (url
,
278 document
.getElementById('file_select').files
[0],
279 document
.getElementById('select_time').value
,
280 document
.getElementById('input_key').value
,
281 document
.getElementById('one_time_download').checked
287 document
.getElementById('file_select').files
[0],
288 document
.getElementById('select_time').value
,
289 document
.getElementById('input_key').value
,
290 document
.getElementById('one_time_download').checked
patrick-canterino.de