Commit a917c3af authored by Davide Gagliardi's avatar Davide Gagliardi

file array converted to object, notification handling, filtered file extension

parent cc42ee38
......@@ -127,9 +127,20 @@ io.use(function(socket,next) {
// var participants = 0
var connectedUsers = {};
var uploadedImagePlaceHolder = {};
var uploadedImages = fs.readdirSync(__dirname + "/uploads") //, (err, files) => {}) || [];
uploadedImages = uploadedImages.filter(function(item){return item!="README.md"})
uploadedImages = Object.assign({}, uploadedImages)
function objectSize(obj){
var res = 0
for (var keys in obj) {
res += 1
}
return res
}
function countUser(arr){
let cnt = 0;
for(id in arr){
......@@ -159,11 +170,12 @@ function onConnection(socket){
socket.emit('dump_chat', chatHistory)
io.sockets.emit("connections", connectedUsers);
if(uploadedImages.length > 0){
if(objectSize(uploadedImages) > 0){
for (var i in uploadedImages) {
var filename = uploadedImages[i]
var cnt = parseInt(i)+1
uploadImage(filename, cnt, socket)
socket.emit('notify', 'upload')
}
}
......@@ -172,8 +184,10 @@ function onConnection(socket){
uploader.listen(socket)
// Do something when a file is saved:
uploader.on("saved", function(event){
uploadedImages.push(event.file.name)
uploadImage(event.file.name, uploadedImages.length, io)
var index = objectSize(uploadedImages)
uploadedImages[index] = (event.file.name)
uploadImage(event.file.name, objectSize(uploadedImages), io)
socket.broadcast.emit('notify', 'upload')
});
// Error handler:
uploader.on("error", function(event){
......@@ -193,7 +207,6 @@ function onConnection(socket){
socket.broadcast.emit('message', msg);
socket.broadcast.emit('notify', 'chat')
chatHistory.messages.push(msg);
console.log(chatHistory);
fs.writeFile(
"chat_history/"+today+"_chat.json",
JSON.stringify(chatHistory, null, 2),
......@@ -202,10 +215,9 @@ function onConnection(socket){
});
socket.on('download-chat', () => {socket.emit("chat-history", chatHistory)})
socket.on('delete-image', (img) => {
console.log("Delete img: " + img)
io.emit('received-delete', img)
console.log(uploadedImages)
fs.unlink(__dirname + "/uploads/"+uploadedImages[img-1], (err) => {if(err){console.log(err)}; uploadedImages.splice(img-1)})
fs.unlink(__dirname + "/uploads/"+uploadedImages[img-1], (err) => {if(err){console.log(err)}; delete uploadedImages[img-1]})
})
socket.on('disconnect', (reason) => {
......
......@@ -37,9 +37,37 @@ var socketFile = new SocketIOFileUpload(socket)
// return window.btoa(t)
// }
var _validFileExtensions = ["jpg", "jpeg", "bmp", "gif", "png"];
document.getElementById('uploaded-file').addEventListener("click", socketFile.prompt, false)
socketFile.listenOnInput(document.getElementsByClassName("upload-form")[0]);
socketFile.listenOnDrop(document.getElementsByClassName("upload-form")[0]);
socketFile.addEventListener('choose', (event) => {
var files = event.files;
flag = false;
for(var i = 0; i < files.length; i++){
var file = files[i]
var filename = file.name.split(".")
var extension = filename[1]
if(extension){
_validFileExtensions.some((ext, i) => {
if(extension === ext){
flag = true
}
});
}
}
if(!flag){
UIkit.notification({
message: 'Not a valid file extension. Only images can be uploaded',
pos: 'bottom-center',
status: 'danger',
timeout: 3500
})
}
return flag
})
// Do something on upload progress:
// socketFile.addEventListener("progress", function(event){
// var percent = event.bytesLoaded / event.file.size * 100;
......@@ -48,8 +76,9 @@ socketFile.listenOnDrop(document.getElementsByClassName("upload-form")[0]);
// Do something when a file is uploaded:
socketFile.addEventListener("complete", function(event){
console.log(event.success);
console.log(event.file);
console.log('completed');
// console.log(event.success);
// console.log(event.file);
});
// socket.on('imageConversionByClient', function(data) {
......@@ -57,14 +86,20 @@ socketFile.addEventListener("complete", function(event){
// console.log("qualcosa");
// });
var localFileList = {}
socket.on('imageConversionByServer', function(data) {
figureCnt = data[1]
addImage(data[0])
});
var name = data[2];
var figureCnt = data[1]
if(!localFileList[figureCnt]){
localFileList[figureCnt] = name
addImage(data[0], figureCnt)
}
});
function addImage(src) {
var imgGrid = '<div class="uk-width-1-4 uk-inline"><img src="'+src+'" class="img-element figure-'+figureCnt+'" alt="Image Preview"><button type="button" name="button" onclick="closeImg(this)" uk-close>figure '+figureCnt+' </button></div>'
var img = '<li style="text-align:center"><img src="'+src+'" class="img-element figure-'+figureCnt+'" alt="Image Preview" ><p>( Figure '+figureCnt+' )</p></li>'
function addImage(src, ind) {
var imgGrid = '<div class="uk-width-1-4 uk-inline"><img src="'+src+'" class="img-element figure-'+ind+'" alt="Image Preview"><button type="button" name="button" onclick="closeImg(this)" uk-close>figure '+ind+' </button></div>'
var img = '<li style="text-align:center"><img src="'+src+'" class="img-element figure-'+ind+'" alt="Image Preview" ><p>( Figure '+ind+' )</p></li>'
$("#preview-container-grid").append(imgGrid)
$('#carousel-uploads').append(img)
}
......@@ -88,6 +123,7 @@ socket.on("received-delete", (index) => {
for (var i = 0; i < images.length; i++) {
$(images[i]).parent().remove()
}
delete localFileList[index]
})
/*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment