]>
git.p6c8.net - jirafeau_project.git/blob - lib/functions.js
fee0b50d84e3ff8af9961b0342ad60435f636944
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
;
44 document
.title
= 'Jirafeau - ' + p
;
47 function upload_progress (e
)
49 if (!e
.lengthComputable
)
51 /* Show the user the operation do not reach 100%, the server need time
52 * to give a response before providing the link.
54 var p
= Math
.round (e
.loaded
* 99 / e
.total
);
55 show_upload_progression (p
.toString() + '%');
58 function upload_failed (e
)
60 /* Todo: Considere showing a error div. */
61 alert ('Sorry, upload failed');
64 function classic_upload (url
, file
, time
, password
, one_time
)
66 var req
= new XMLHttpRequest ();
67 req
.upload
.addEventListener ("progress", upload_progress
, false);
68 req
.addEventListener ("error", upload_failed
, false);
69 req
.addEventListener ("abort", upload_failed
, false);
70 req
.onreadystatechange = function ()
72 if (req
.readyState
== 4 && req
.status
== 200)
74 var res
= req
.responseText
;
77 res
= res
.split ("\n");
82 d
.setSeconds (d
.getSeconds() + 60);
83 else if (time
== 'hour')
84 d
.setSeconds (d
.getSeconds() + 3600);
85 else if (time
== 'day')
86 d
.setSeconds (d
.getSeconds() + 86400);
87 else if (time
== 'week')
88 d
.setSeconds (d
.getSeconds() + 604800);
89 else if (time
== 'month')
90 d
.setSeconds (d
.getSeconds() + 2419200);
93 show_link (url
, res
[0], res
[1], d
.toString());
96 show_link (url
, res
[0], res
[1]);
99 req
.open ("POST", url
+ 'script.php' , true);
101 var form
= new FormData();
102 form
.append ("file", file
);
104 form
.append ("time", time
);
106 form
.append ("key", password
);
108 form
.append ("one_time_download", '1');
112 function check_html5_file_api ()
114 if (window
.File
&& window
.FileReader
&& window
.FileList
&& window
.Blob
)
119 var async_global_transfered
= 0;
120 var async_global_url
= '';
121 var async_global_file
;
122 var async_global_ref
= '';
123 var async_global_max_size
= 0;
124 var async_global_time
;
125 var async_global_transfering
= 0;
127 function async_upload_start (url
, max_size
, file
, time
, password
, one_time
)
129 async_global_transfered
= 0;
130 async_global_url
= url
;
131 async_global_file
= file
;
132 async_global_max_size
= max_size
;
133 async_global_time
= time
;
135 var req
= new XMLHttpRequest ();
136 req
.addEventListener ("error", upload_failed
, false);
137 req
.addEventListener ("abort", upload_failed
, false);
138 req
.onreadystatechange = function ()
140 if (req
.readyState
== 4 && req
.status
== 200)
142 var res
= req
.responseText
;
145 res
= res
.split ("\n");
146 async_global_ref
= res
[0];
148 async_upload_push (code
);
151 req
.open ("POST", async_global_url
+ 'script.php?init_async' , true);
153 var form
= new FormData();
154 form
.append ("filename", async_global_file
.name
);
155 form
.append ("type", async_global_file
.type
);
157 form
.append ("time", time
);
159 form
.append ("key", password
);
161 form
.append ("one_time_download", '1');
165 function async_upload_progress (e
)
167 if (!e
.lengthComputable
&& async_global_file
.size
!= 0)
169 var p
= Math
.round ((e
.loaded
+ async_global_transfered
) * 99 / (async_global_file
.size
));
170 show_upload_progression (p
.toString() + '%');
173 function async_upload_push (code
)
175 if (async_global_transfered
== async_global_file
.size
)
177 async_upload_end (code
);
180 var req
= new XMLHttpRequest ();
181 req
.upload
.addEventListener ("progress", async_upload_progress
, false);
182 req
.addEventListener ("error", upload_failed
, false);
183 req
.addEventListener ("abort", upload_failed
, false);
184 req
.onreadystatechange = function ()
186 if (req
.readyState
== 4 && req
.status
== 200)
188 var res
= req
.responseText
;
191 res
= res
.split ("\n");
193 async_global_transfered
= async_global_transfering
;
194 async_upload_push (code
);
197 req
.open ("POST", async_global_url
+ 'script.php?push_async' , true);
199 var chunk_size
= parseInt (async_global_max_size
* 0.90);
200 var start
= async_global_transfered
;
201 var end
= start
+ chunk_size
;
202 if (end
>= async_global_file
.size
)
203 end
= async_global_file
.size
;
204 var blob
= async_global_file
.slice (start
, end
);
205 async_global_transfering
= end
;
207 var form
= new FormData();
208 form
.append ("ref", async_global_ref
);
209 form
.append ("data", blob
);
210 form
.append ("code", code
);
214 function async_upload_end (code
)
216 var req
= new XMLHttpRequest ();
217 req
.addEventListener ("error", upload_failed
, false);
218 req
.addEventListener ("abort", upload_failed
, false);
219 req
.onreadystatechange = function ()
221 if (req
.readyState
== 4 && req
.status
== 200)
223 var res
= req
.responseText
;
226 res
= res
.split ("\n");
227 if (async_global_time
!= 'none')
230 if (async_global_time
== 'minute')
231 d
.setSeconds (d
.getSeconds() + 60);
232 else if (async_global_time
== 'hour')
233 d
.setSeconds (d
.getSeconds() + 3600);
234 else if (async_global_time
== 'day')
235 d
.setSeconds (d
.getSeconds() + 86400);
236 else if (async_global_time
== 'week')
237 d
.setSeconds (d
.getSeconds() + 604800);
238 else if (async_global_time
== 'month')
239 d
.setSeconds (d
.getSeconds() + 2419200);
242 show_link (async_global_url
, res
[0], res
[1], d
.toString());
245 show_link (async_global_url
, res
[0], res
[1]);
248 req
.open ("POST", async_global_url
+ 'script.php?end_async' , true);
250 var form
= new FormData();
251 form
.append ("ref", async_global_ref
);
252 form
.append ("code", code
);
256 function upload (url
, max_size
)
258 if (check_html5_file_api ()
259 && document
.getElementById('file_select').files
[0].size
>= max_size
)
261 async_upload_start (url
,
263 document
.getElementById('file_select').files
[0],
264 document
.getElementById('select_time').value
,
265 document
.getElementById('input_key').value
,
266 document
.getElementById('one_time_download').checked
272 document
.getElementById('file_select').files
[0],
273 document
.getElementById('select_time').value
,
274 document
.getElementById('input_key').value
,
275 document
.getElementById('one_time_download').checked
patrick-canterino.de