-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.html
401 lines (325 loc) · 15.8 KB
/
bucket.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bucket</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- remove this if you use Modernizr -->
<script>(function(e,t,n){var r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2")})(document,window,0);</script>
<link rel="stylesheet" href="index.css">
<!-- <link rel="stylesheet" href="main.css"> -->
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Sansita" rel="stylesheet">
<!-- pusher -->
<script src="//js.pusher.com/3.1/pusher.min.js" type="text/javascript"></script>
<script src="//code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<h1 style="margin-top: 35px;">Welcome to your Bucket!</h1>
</div>
<div class="container" role="main">
<form class="box" method="post" action="" enctype="multipart/form-data" novalidate class="box">
<div class="box__input">
<svg class="box__icon" xmlns="http://www.w3.org/2000/svg" width="50" height="43" viewBox="0 0 50 43"><path d="M48.4 26.5c-.9 0-1.7.7-1.7 1.7v11.6h-43.3v-11.6c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v13.2c0 .9.7 1.7 1.7 1.7h46.7c.9 0 1.7-.7 1.7-1.7v-13.2c0-1-.7-1.7-1.7-1.7zm-24.5 6.1c.3.3.8.5 1.2.5.4 0 .9-.2 1.2-.5l10-11.6c.7-.7.7-1.7 0-2.4s-1.7-.7-2.4 0l-7.1 8.3v-25.3c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v25.3l-7.1-8.3c-.7-.7-1.7-.7-2.4 0s-.7 1.7 0 2.4l10 11.6z"/></svg>
<div class="hidden-file-input">
<input type="file" name="files[]" id="file" class="box__file" data-multiple-caption="You have {count} files in the bucket!" multiple />
</div>
<div class="file-display-section row">
</div>
<label style="margin-top: 10px;" for="file" class="file-caption-labels"><strong>Choose a file</strong><span class="box__dragndrop"> or drag it here</span>.</label>
<button type="submit" style="display:none;" class="box__button">Upload</button>
</div>
<div class="box__uploading file-caption-labels">Uploading…</div>
<div class="box__success file-caption-labels">Done! <a href="https://css-tricks.com/examples/DragAndDropFileUploading//?submit-on-demand" class="box__restart" role="button">Upload more?</a></div>
<div class="box__error file-caption-labels">Error! <span></span>. <a href="https://css-tricks.com/examples/DragAndDropFileUploading//?submit-on-demand" class="box__restart" role="button">Try again!</a></div>
</form>
</div>
</body>
<script>
// Global host name
console.log(document.cookie);
var host_name = "http://9c1a3ec5.ngrok.io/";
// Initialize pusher
var pusher = new Pusher('31932a4df70b512824e3', {
cluster: 'eu',
encrypted: true
});
var raw_cookie = document.cookie;
console.log(raw_cookie);
var index = raw_cookie.lastIndexOf("bucket_key=");
index += 11;
var bucket_key = raw_cookie.substring(index, index + 36);
var global_files = [];
var channel = pusher.subscribe('ICStrangers');
$( document ).ready(function() {
$.ajax({
url: host_name + 'bucket/' + bucket_key + "/list-contents",
type: 'GET',
dataType: 'json',
// cache: false,
// contentType: false,
// processData: false,
success: function( data ){
}
});
});
// channel.bind("on_connection" , function(data){
// console.log("Connection Notification");
// window.location.replace("bucket.html");
// });
channel.bind("on_disconnect" , function(data){
//destroys bucket key and redirects back to index
console.log("disconnect");
//get bucket_id from cookies
var raw_cookie = document.cookie;
console.log(raw_cookie);
var index = raw_cookie.lastIndexOf("user_key=");
index += 9;
var user_key = raw_cookie.substring(index, index + 36);
console.log("user key is" + user_key);
if (user_key == data.destination_key) {
console.log('disconnect');
document.cookie = "bucket_key=; expires=Thu, 01 Jan 1970 00:00:00 UTC;"
window.location.replace("./");
}
});
channel.bind("bucket_updated" , function(data){
console.log("Bucket Updated");
//get bucket_key from cookie
var raw_cookie = document.cookie;
console.log(raw_cookie);
var index = raw_cookie.lastIndexOf("bucket_key=");
index += 11;
var bucket_key = raw_cookie.substring(index, index + 36);
//compare bucket_key to cookie
// if (bucket_key == data.bucket_key) {
//send file data to function to show code
global_files = data.files;
renderGlobalFiles();
console.log(data.files);
// }
//ajax request to get files
});
channel.bind("bucket_destroyed" , function(data){
//destroys bucket key and redirects back to index
console.log("Destroy Bucket Notified");
//get bucket_id from cookies
var raw_cookie = document.cookie;
console.log(raw_cookie);
var index = raw_cookie.lastIndexOf("bucket_key=");
index += 11;
var bucket_key = raw_cookie.substring(index, index + 36);
console.log(bucket_key);
if (bucket_key == data.bucket_key) {
document.cookie = "bucket_key=; expires=Thu, 01 Jan 1970 00:00:00 UTC;"
window.location.replace("./");
}
});
function renderGlobalFiles() {
$('.file-display-section').html("");
for (var i = 0; i<global_files.length; i++) {
var fileName = global_files[i].name;
var extension = global_files[i].name.split('.').pop().toLowerCase();
if(extension == "pdf"){
$('.file-display-section').append("<div class='col-md-4'>" + '<img class="file-thumbnail" src="img/pdf_logo.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + global_files[i].id + ">" + fileName + '</a></div>');
}
else if(extension == 'pptx'){
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/ppt_logo.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + global_files[i].id + ">" + fileName + '</a></div>')
}
else if(extension == 'xls' || extension == 'xlsx' || extension == 'xlsb'){
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/xls_logo.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + global_files[i].id + ">" + fileName + '</a></div>')
}
else if(extension == 'doc' || extension == 'docx'){
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/word_logo.jpg">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + global_files[i].id + ">" + fileName + '</a></div>');
}
else if (extension == 'png' || extension == 'jpg' || extension == 'jpeg') {
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="' + host_name + "user_files/" + bucket_key + "_" + global_files[i].name + '"</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + global_files[i].id + ">" + fileName + '</a></div>');
}
else{//edge case get from server see if path works
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/question_mark.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + global_files[i].id + ">" + fileName + '</a></div>');
}
}
}
// Function to get files from api
function getData(){
$.get( "", function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
});
}
'use strict';
;( function( $, window, document, undefined )
{
// feature detection for drag&drop upload
var isAdvancedUpload = function(){
var div = document.createElement( 'div' );
return ( ( 'draggable' in div ) || ( 'ondragstart' in div && 'ondrop' in div ) ) && 'FormData' in window && 'FileReader' in window;
}();
// applying the effect for every form
$( '.box' ).each( function()
{
var $form = $( this ),
$input = $form.find( 'input[type="file"]' ),
$label = $form.find( 'label' ),
$errorMsg = $form.find( '.box__error span' ),
$restart = $form.find( '.box__restart' ),
file_key = "uploaded_file",
showFiles = function( files )
{
// Display file name along with thumbnail.
for(var i = 0; i < files.length; i++){
var reader = new FileReader();
var curr_file = files[i];
reader.onload = (function(theFile){
var fileName = theFile.name;
var extension = theFile.name.split('.').pop().toLowerCase();
console.log(global_files);
// Handle different filetypes
if(extension == "pdf"){
$('.file-display-section').append("<div class='col-md-4'>" + '<img class="file-thumbnail" src="img/pdf_logo.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + curr_file.id + ">" + fileName + '</a></div>');
}
else if(extension == 'pptx'){
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/ppt_logo.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + curr_file.id + ">" + fileName + '</a></div>')
}
else if(extension == 'xls' || extension == 'xlsx' || extension == 'xlsb'){
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/xls_logo.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + curr_file.id + ">" + fileName + '</a></div>')
}
else if(extension == 'doc' || extension == 'docx'){
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/word_logo.jpg">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + curr_file.id + ">" + fileName + '</a></div>');
}
else if (extension == 'png' || extension == 'jpg' || extension == 'jpeg') {
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="' + host_name + "user_files/" + bucket_key + "_" + curr_file.name + '"</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + curr_file.id + ">" + fileName + '</a></div>');
}
else{//edge case get from server see if path works
$('.file-display-section').append("<div style='margin-bottom:10px;' class='col-md-4'>" + '<img class="file-thumbnail" src="img/question_mark.png">'+ '</img><br>' + "<a style='cursor:pointer;' href=" + host_name + "bucket/" + bucket_key + "/get?file_id=" + curr_file.id + ">" + fileName + '</a></div>');
}
})(curr_file);
reader.readAsDataURL(curr_file);
}
$label.text( files.length > 1 ? ( $input.attr( 'data-multiple-caption' ) || '' ).replace( '{count}', files.length ) : "You added "+ files[ 0 ].name + " to the bucket!" );
}
// letting the server side to know we are going to make an Ajax request
$form.append( '<input type="hidden" name="ajax" value="1" />' );
// drag&drop files if the feature is available
if( isAdvancedUpload )
{
$form
.addClass( 'has-advanced-upload' ) // letting the CSS part to know drag&drop is supported by the browser
.on( 'drag dragstart dragend dragover dragenter dragleave drop', function( e )
{
// preventing the unwanted behaviours
e.preventDefault();
e.stopPropagation();
})
.on( 'dragover dragenter', function() //
{
$form.addClass( 'is-dragover' );
})
.on( 'dragleave dragend drop', function()
{
$form.removeClass( 'is-dragover' );
})
.on( 'drop', function( e )
{
droppedFiles = e.originalEvent.dataTransfer.files; // the files that were dropped
// $.each( droppedFiles, function( i, file ){
// arrayFiles.push(file);
// });
console.log(droppedFiles);
renderGlobalFiles();
showFiles( droppedFiles );
$form.submit();
});
}
// automatically submit the form on file select
$input.on( 'change', function( e )
{
showFiles( e.target.files );
});
// if the form was submitted
$form.on( 'submit', function( e )
{
// preventing the duplicate submissions if the current one is in progress
if( $form.hasClass( 'is-uploading' ) ) return false;
$form.addClass( 'is-uploading' ).removeClass( 'is-error' );
if( isAdvancedUpload ) // ajax file upload for modern browsers
{
e.preventDefault();
// gathering the form data
var ajaxData = new FormData();
if( droppedFiles ){
// $.each( droppedFiles, function( i, file )
// {
// ajaxData.append( file_key, file );
// });
$.each( droppedFiles, function( i, file ){
ajaxData.append(file_key, file);
console.log(file.name + " Uploaded");
});
}
// ajax request to submit form data
$.ajax(
{
url: host_name + "bucket/" + bucket_key + "/add",
type: 'POST',
data: ajaxData,
//dataType: 'json',
cache: false,
contentType: false,
processData: false,
complete: function()
{
for (var pair of ajaxData.entries()) {
console.log("File Name: "+pair[0]);
console.log(pair[1]);
}
$form.removeClass( 'is-uploading' );
},
success: function( data )
{
//$form.addClass( data.success == true ? 'is-success' : 'is-error' );
//console.log(data.success);
//if( !data.success ) $errorMsg.text( data.error );
//alert("File Uploaded!");
//$form.addClass('is-success');
},
error: function()
{
alert( 'Error. Please, contact the webmaster!' );
}
});
}
else // fallback Ajax solution upload for older browsers
{
var iframeName = 'uploadiframe' + new Date().getTime(),
$iframe = $( '<iframe name="' + iframeName + '" style="display: none;"></iframe>' );
$( 'body' ).append( $iframe );
$form.attr( 'target', iframeName );
$iframe.one( 'load', function()
{
var data = $.parseJSON( $iframe.contents().find( 'body' ).text() );
$form.removeClass( 'is-uploading' ).addClass( data.success == true ? 'is-success' : 'is-error' ).removeAttr( 'target' );
if( !data.success ) $errorMsg.text( data.error );
$iframe.remove();
});
}
});
// restart the form if has a state of error/success
$restart.on( 'click', function( e )
{
e.preventDefault();
$form.removeClass( 'is-error is-success' );
$input.trigger( 'click' );
});
// Firefox focus bug fix for file input
$input
.on( 'focus', function(){ $input.addClass( 'has-focus' ); })
.on( 'blur', function(){ $input.removeClass( 'has-focus' ); });
});
})( jQuery, window, document );
</script>
</html>