]>
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
, date
)
21 var download_link
= url
+ 'file.php?h=' + reference
;
22 var delete_link
= download_link
+ '&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
;
30 document
.getElementById('date').innerHTML
= date
;
31 document
.getElementById('validity').style
.display
= '';
34 document
.getElementById('validity').style
.display
= 'none';
36 document
.getElementById('uploading').style
.display
= 'none';
37 document
.getElementById('upload').style
.display
= 'none';
38 document
.getElementById('upload_finished').style
.display
= '';
41 function show_upload_progression (p
)
43 document
.getElementById('uploaded_percentage').innerHTML
= p
;
46 function upload_progress (e
)
48 if (!e
.lengthComputable
)
50 /* Show the user the operation do not reach 100%, the server need time
51 * to give a response before providing the link.
53 var p
= Math
.round (e
.loaded
* 99 / e
.total
);
54 show_upload_progression (p
.toString() + '%');
57 function upload_failed (e
)
59 /* Todo: Considere showing a error div. */
60 alert ('Sorry, upload failed');
63 function classic_upload (url
, file
, time
, password
, one_time
)
65 var req
= new XMLHttpRequest ();
66 req
.upload
.addEventListener ("progress", upload_progress
, false);
67 req
.addEventListener ("error", upload_failed
, false);
68 req
.addEventListener ("abort", upload_failed
, false);
69 req
.onreadystatechange = function ()
71 if (req
.readyState
== 4 && req
.status
== 200)
73 var res
= req
.responseText
;
76 res
= res
.split ("\n");
81 d
.setSeconds (d
.getSeconds() + 60);
82 else if (time
== 'hour')
83 d
.setSeconds (d
.getSeconds() + 3600);
84 else if (time
== 'day')
85 d
.setSeconds (d
.getSeconds() + 86400);
86 else if (time
== 'week')
87 d
.setSeconds (d
.getSeconds() + 604800);
88 else if (time
== 'month')
89 d
.setSeconds (d
.getSeconds() + 2419200);
92 show_link (url
, res
[0], res
[1], d
.toString());
95 show_link (url
, res
[0], res
[1]);
98 req
.open ("POST", url
+ 'script.php' , true);
100 var form
= new FormData();
101 form
.append ("file", file
);
103 form
.append ("time", time
);
105 form
.append ("key", password
);
107 form
.append ("one_time_download", '1');
111 function check_html5_file_api ()
113 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
118 var async_global_transfered
= 0;
119 var async_global_url
= '';
120 var async_global_file
;
121 var async_global_ref
= '';
122 var async_global_max_size
= 0;
123 var async_global_time
;
124 var async_global_transfering
= 0;
126 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
)
128 async_global_transfered
= 0;
129 async_global_url
= url
;
130 async_global_file
= file
;
131 async_global_max_size
= max_size
;
132 async_global_time
= time
;
134 var req
= new XMLHttpRequest ();
135 req
.addEventListener ("error", upload_failed
, false);
136 req
.addEventListener ("abort", upload_failed
, false);
137 req
.onreadystatechange = function ()
139 if (req
.readyState
== 4 && req
.status
== 200)
141 var res
= req
.responseText
;
144 res
= res
.split ("\n");
145 async_global_ref
= res
[0];
147 async_upload_push (code
);
150 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
152 var form
= new FormData();
153 form
.append ("filename", async_global_file
.name
);
154 form
.append ("type", async_global_file
.type
);
156 form
.append ("time", time
);
158 form
.append ("key", password
);
160 form
.append ("one_time_download", '1');
164 function async_upload_progress (e
)
166 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
168 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 99 / (async_global_file
.size
));
169 show_upload_progression (p
.toString() + '%');
172 function async_upload_push (code
)
174 if (async_global_transfered
== async_global_file
.size
)
176 async_upload_end (code
);
179 var req
= new XMLHttpRequest ();
180 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
181 req
.addEventListener ("error", upload_failed
, false);
182 req
.addEventListener ("abort", upload_failed
, false);
183 req
.onreadystatechange = function ()
185 if (req
.readyState
== 4 && req
.status
== 200)
187 var res
= req
.responseText
;
190 res
= res
.split ("\n");
192 async_global_transfered
= async_global_transfering
;
193 async_upload_push (code
);
196 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
198 var chunk_size
= parseInt (async_global_max_size
* 0.90);
199 var start
= async_global_transfered
;
200 var end
= start
+ chunk_size
;
201 if (end
>= async_global_file
.size
)
202 end
= async_global_file
.size
;
203 var blob
= async_global_file
.slice (start
, end
);
204 async_global_transfering
= end
;
206 var form
= new FormData();
207 form
.append ("ref", async_global_ref
);
208 form
.append ("data", blob
);
209 form
.append ("code", code
);
213 function async_upload_end (code
)
215 var req
= new XMLHttpRequest ();
216 req
.addEventListener ("error", upload_failed
, false);
217 req
.addEventListener ("abort", upload_failed
, false);
218 req
.onreadystatechange = function ()
220 if (req
.readyState
== 4 && req
.status
== 200)
222 var res
= req
.responseText
;
225 res
= res
.split ("\n");
226 if (async_global_time
!= 'none')
229 if (async_global_time
== 'minute')
230 d
.setSeconds (d
.getSeconds() + 60);
231 else if (async_global_time
== 'hour')
232 d
.setSeconds (d
.getSeconds() + 3600);
233 else if (async_global_time
== 'day')
234 d
.setSeconds (d
.getSeconds() + 86400);
235 else if (async_global_time
== 'week')
236 d
.setSeconds (d
.getSeconds() + 604800);
237 else if (async_global_time
== 'month')
238 d
.setSeconds (d
.getSeconds() + 2419200);
241 show_link (async_global_url
, res
[0], res
[1], d
.toString());
244 show_link (async_global_url
, res
[0], res
[1]);
247 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
249 var form
= new FormData();
250 form
.append ("ref", async_global_ref
);
251 form
.append ("code", code
);
255 function upload (url
, max_size
)
257 if (check_html5_file_api ()
258 && document
.getElementById('file_select').files
[0].size
>= max_size
)
260 async_upload_start (url
,
262 document
.getElementById('file_select').files
[0],
263 document
.getElementById('select_time').value
,
264 document
.getElementById('input_key').value
,
265 document
.getElementById('one_time_download').checked
271 document
.getElementById('file_select').files
[0],
272 document
.getElementById('select_time').value
,
273 document
.getElementById('input_key').value
,
274 document
.getElementById('one_time_download').checked
patrick-canterino.de