Skip to content

Commit 5ac08e7

Browse files
drag&drop file upload plus inline-images/inline download are now working
1 parent be354b3 commit 5ac08e7

File tree

12 files changed

+115
-18
lines changed

12 files changed

+115
-18
lines changed

application/chatserver/controller/connectionController.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Room = require("../../../domain/room.js")["Room"];
22
User = require("../../../domain/user.js")["User"];
33
UserCommunication = require("../../../domain/communication.js")["UserCommunication"];
44
SystemCommunication = require("../../../domain/communication.js")["SystemCommunication"];
5+
DownloadCommunication = require("../../../domain/communication.js")["DownloadCommunication"];
56

67
var room = new Room("default");
78
var userId = 0;
@@ -55,5 +56,12 @@ var deliver = function() {
5556
}
5657
};
5758

59+
var handleUpload = function(filename, type) {
60+
var communication = new DownloadCommunication(filename, type, '/download/' + encodeURIComponent(filename));
61+
room.addCommunication(communication);
62+
deliver();
63+
};
64+
5865
exports.attachRenderer = attachRenderer;
5966
exports.attachServer = attachServer;
67+
exports.handleUpload = handleUpload;

application/chatserver/renderer/objectRenderer.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
var render = function(communication) {
2+
var renderedCommunication;
23
if (communication instanceof UserCommunication) {
3-
var renderedCommunication = { type: 'user', username: communication.user.name, message: communication.message };
4-
}
5-
else if (communication instanceof SystemCommunication) {
6-
var renderedCommunication = { type: 'system', message: communication.message };
4+
renderedCommunication = { type: 'user', username: communication.user.name, message: communication.message };
5+
} else if (communication instanceof SystemCommunication) {
6+
renderedCommunication = { type: 'system', message: communication.message };
7+
} else if (communication instanceof DownloadCommunication) {
8+
renderedCommunication = { type: 'download', filename: communication.filename, filetype: communication.filetype, url: communication.url };
79
}
810

911
return renderedCommunication;

application/client/app/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ $(document).ready(function() {
3131
},
3232

3333
uploadStartedCallback: function(i, file, len) {
34-
window.alert('go!');
34+
$('#messages').removeClass('filehover');
3535
},
3636

3737
progressUpdated: function(i, file, progress) {
@@ -43,6 +43,7 @@ $(document).ready(function() {
4343
},
4444

4545
errorCallback: function(message) {
46+
$('#messages').removeClass('filehover');
4647
window.alert(message);
4748
}
4849
});

application/client/css/index.css

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,25 @@
1919
}
2020

2121
#messages.filehover {
22-
background-color: #ff9;
22+
background-color: #ffe;
23+
}
24+
25+
#messages div.message.file {
26+
background-color: #ffe;
27+
text-align: center;
28+
border: 1px solid #eee;
29+
border-radius: 8px;
30+
padding: 8px;
31+
margin: 8px;
32+
}
33+
34+
#messages div.message.file.inline img {
35+
background-color: #fff;
36+
height: 50px;
37+
border: 1px solid #eee;
38+
border-radius: 4px;
39+
padding: 8px;
40+
margin: 6px;
2341
}
2442

2543
.input-xlarge {

application/client/lib/chat.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ bivouac.chat = {
2121
handleOutput: function (socket, messagesElement) {
2222
socket.on('hear', function (communication) {
2323
if (communication.type === 'user') {
24-
messagesElement.append('<div class="message"><span class="says"><span class="username">' + $('<div/>').text(communication.username).html() + '</span> says:</span> ' + $('<div/>').text(communication.message).html() + '</div>')
24+
messagesElement.append('<div class="message user"><span class="says"><span class="username">' + $('<div/>').text(communication.username).html() + '</span> says:</span> ' + $('<div/>').text(communication.message).html() + '</div>');
2525
}
2626
if (communication.type === 'system') {
27-
messagesElement.append('<div class="message"><i>' + $('<div/>').text(communication.message).html() + '</i></div>')
27+
messagesElement.append('<div class="message system"><i>' + $('<div/>').text(communication.message).html() + '</i></div>');
28+
}
29+
if (communication.type === 'download') {
30+
if (communication.filetype.match(/image\//)) {
31+
messagesElement.append('<div class="message file inline"><a target="_blank" href="' + $('<div/>').text(communication.url).html() + '"><img src="' + $('<div/>').text(communication.url).html() + '"/><br /><span class="subtitle">' + $('<div/>').text(communication.filename).html() + '</span></a></div>');
32+
} else {
33+
messagesElement.append('<div class="message file download">Uploaded file: <a target="_blank" href="' + $('<div/>').text(communication.url).html() + '">' + $('<div/>').text(communication.filename).html() + '</a></div>');
34+
}
2835
}
2936
messagesElement.scrollTop(99999);
3037
});

application/client/lib/fileupload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bivouac.fileupload = {
1111

1212
options.dropElement.filedrop({
1313
paramname: 'file',
14-
maxfiles: 1,
14+
maxfiles: 5,
1515
maxfilesize: 5,
1616
url: options.targetUrl,
1717
dragEnter: options.dragEnterCallback,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var util = require('util');
2+
var fs = require('fs');
3+
var mime = require('mime');
4+
var formidable = require("formidable");
5+
6+
var handleUpload = function(request, response, inform) {
7+
var form = new formidable.IncomingForm();
8+
form.parse(request, function(error, fields, files) {
9+
fs.rename(files.file.path, "/tmp/bivouac_upload_" + files.file.name, function(err) {
10+
inform(files.file.name, files.file.type);
11+
response.writeHead(200, {'content-type': 'text/plain'});
12+
response.end('ok');
13+
});
14+
});
15+
}
16+
17+
var handleDownload = function(filename, response) {
18+
var path = "/tmp/bivouac_upload_" + filename;
19+
fs.readFile(path, "binary", function(error, file) {
20+
if(error) {
21+
response.writeHead(500, {'content-type': 'text/plain'});
22+
response.end(error + "\n");
23+
} else {
24+
response.writeHead(200, {
25+
'content-type': mime.lookup(path)
26+
});
27+
response.end(file, "binary");
28+
}
29+
});
30+
}
31+
32+
exports.handleUpload = handleUpload;
33+
exports.handleDownload = handleDownload;

application/webserver/webserver.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
var http = require('http');
2-
var url = require('url');
2+
var url = require('url');
33
var path = require('path');
4-
var fs = require('fs');
4+
var fs = require('fs');
55

6-
var start = function(documentRoot) {
6+
var start = function(documentRoot, handleUpload, uploadCallback, handleDownload) {
77
var server = http.createServer(function(request, response) {
8-
var uri = url.parse(request.url).pathname;
9-
var filename = path.join(documentRoot, uri);
10-
console.log(filename);
8+
var uriPath = url.parse(request.url).pathname;
9+
var filename;
10+
11+
if (uriPath == '/upload' && request.method.toLowerCase() == 'post') {
12+
console.log("Handling upload...");
13+
handleUpload(request, response, uploadCallback);
14+
return;
15+
}
16+
17+
if (uriPath.match(/\/download/) && request.method.toLowerCase() == 'get') {
18+
var parts = uriPath.match(/\/download\/(.*)/);
19+
filename = parts[1];
20+
console.log("Handling download of '" + filename + "'...");
21+
handleDownload(filename, response);
22+
return;
23+
}
24+
25+
filename = path.join(documentRoot, uriPath);
26+
console.log(uriPath + " -> " + filename);
1127

1228
path.exists(filename, function(exists) {
1329
if (!exists) {

bivouac.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
(function () {
2+
var filesharing = require("./application/filesharing/filesharing.js");
23
var webserver = require("./application/webserver/webserver.js");
34
var chatserver = require("./application/chatserver/server/socketioserver.js");
45
var renderer = require("./application/chatserver/renderer/objectRenderer.js");
56
var connectionController = require("./application/chatserver/controller/connectionController.js");
67

78
connectionController.attachRenderer(renderer);
89
connectionController.attachServer(chatserver);
9-
chatserver.start(webserver.start(process.cwd() + "/application/client"));
10+
chatserver.start(webserver.start(process.cwd() + "/application/client", filesharing.handleUpload, connectionController.handleUpload, filesharing.handleDownload));
1011
})();

domain/communication.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@ var SystemCommunication = function(message) {
77
this.message = message;
88
}
99

10+
var DownloadCommunication = function(filename, filetype, url) {
11+
this.filename = filename;
12+
this.filetype = filetype;
13+
this.url = url;
14+
}
15+
1016
exports.UserCommunication = UserCommunication;
1117
exports.SystemCommunication = SystemCommunication;
18+
exports.DownloadCommunication = DownloadCommunication;

0 commit comments

Comments
 (0)