]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.js
2dcde55f39528ced6dfc9e1b02a1cc085de0f4e2
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
= '';
39 document
.title
= 'Jirafeau - 100%';
42 function show_upload_progression (p
)
44 document
.getElementById('uploaded_percentage').innerHTML
= p
;
45 document
.title
= 'Jirafeau - ' + p
;
48 function upload_progress (e
)
50 if (!e
.lengthComputable
)
52 /* Show the user the operation do not reach 100%, the server need time
53 * to give a response before providing the link.
55 var p
= Math
.round (e
.loaded
* 99 / e
.total
);
56 show_upload_progression (p
.toString() + '%');
59 function upload_failed (e
)
61 /* Todo: Considere showing a error div. */
62 alert ('Sorry, upload failed');
65 function classic_upload (url
, file
, time
, password
, one_time
)
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 ()
73 if (req
.readyState
== 4 && req
.status
== 200)
75 var res
= req
.responseText
;
78 res
= res
.split ("\n");
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);
94 show_link (url
, res
[0], res
[1], d
.toString());
97 show_link (url
, res
[0], res
[1]);
100 req
.open ("POST", url
+ 'script.php' , true);
102 var form
= new FormData();
103 form
.append ("file", file
);
105 form
.append ("time", time
);
107 form
.append ("key", password
);
109 form
.append ("one_time_download", '1');
113 function check_html5_file_api ()
115 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
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;
128 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
)
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
;
136 var req
= new XMLHttpRequest ();
137 req
.addEventListener ("error", upload_failed
, false);
138 req
.addEventListener ("abort", upload_failed
, false);
139 req
.onreadystatechange = function ()
141 if (req
.readyState
== 4 && req
.status
== 200)
143 var res
= req
.responseText
;
146 res
= res
.split ("\n");
147 async_global_ref
= res
[0];
149 async_upload_push (code
);
152 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
154 var form
= new FormData();
155 form
.append ("filename", async_global_file
.name
);
156 form
.append ("type", async_global_file
.type
);
158 form
.append ("time", time
);
160 form
.append ("key", password
);
162 form
.append ("one_time_download", '1');
166 function async_upload_progress (e
)
168 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
170 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 99 / (async_global_file
.size
));
171 show_upload_progression (p
.toString() + '%');
174 function async_upload_push (code
)
176 if (async_global_transfered
== async_global_file
.size
)
178 async_upload_end (code
);
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 ()
187 if (req
.readyState
== 4 && req
.status
== 200)
189 var res
= req
.responseText
;
192 res
= res
.split ("\n");
194 async_global_transfered
= async_global_transfering
;
195 async_upload_push (code
);
198 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
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
;
208 var form
= new FormData();
209 form
.append ("ref", async_global_ref
);
210 form
.append ("data", blob
);
211 form
.append ("code", code
);
215 function async_upload_end (code
)
217 var req
= new XMLHttpRequest ();
218 req
.addEventListener ("error", upload_failed
, false);
219 req
.addEventListener ("abort", upload_failed
, false);
220 req
.onreadystatechange = function ()
222 if (req
.readyState
== 4 && req
.status
== 200)
224 var res
= req
.responseText
;
227 res
= res
.split ("\n");
228 if (async_global_time
!= 'none')
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);
243 show_link (async_global_url
, res
[0], res
[1], d
.toString());
246 show_link (async_global_url
, res
[0], res
[1]);
249 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
251 var form
= new FormData();
252 form
.append ("ref", async_global_ref
);
253 form
.append ("code", code
);
257 function upload (url
, max_size
)
259 if (check_html5_file_api ()
260 && document
.getElementById('file_select').files
[0].size
>= max_size
)
262 async_upload_start (url
,
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
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
patrick-canterino.de