-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsClient.js
381 lines (309 loc) · 9.76 KB
/
awsClient.js
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
var AWS = require('aws-sdk'),
fs = require('fs'),
dataUriToBuffer = require('data-uri-to-buffer'),
Files = {};
// Set the region for the AWS services - required for DynamoDB
AWS.config.update({region: "us-east-1"});
// Create clients for each service
var s3 = new AWS.S3();
var dynamodb = new AWS.DynamoDB();
var ses = new AWS.SES();
var sqs = new AWS.SQS();
var sns = new AWS.SNS();
var aws = this;
var tableName = "HouseParties";
// Creates a new bucket for a room
exports.createBucket = function(roomID, callback) {
var name = 'tcnj-csc470-nodejs-' + roomID;
var params = {
Bucket: name,
ACL: 'public-read'
};
s3.createBucket(params, function(err, data) {
if (err) {
console.log("Bucket creation failed");
console.log(err, err.stack); // an error occurred
}
else console.log(data); // successful response
callback(err, data);
});
return name;
}
// Deletes an AWS bucket and all objects it contains
exports.deleteBucket = function(roomID, callback) {
var name = 'tcnj-csc470-nodejs-' + roomID;
var params = {
Bucket: 'STRING_VALUE' /* required */
};
s3.deleteBucket(params, function(err, data) {
if (err) {
console.log("Bucket creation failed");
console.log(err, err.stack); // an error occurred
}
else console.log(data); // successful response
callback(err, data);
});
}
/*
* Queries DynamoDB to see whether the room ID given in the parameter has been used already
* If the room ID is unique, then a true value is passed into the parameter callback
* If the room ID is not unique, then a false value is passed into the parameter callback
*/
exports.testRoomID = function(host, callback) {
var result;
var params = {
Key: {
RoomHost: { S: host }
},
TableName: tableName
};
dynamodb.getItem(params, function(err, data) {
if (err){
console.log(err, err.stack);
callback(false);
}
else {
callback(typeof data.Item === "undefined");
}
});
}
exports.addRoomToDB = function(host, hostIP, callback) {
var params = {
Item: {
RoomHost: { S: host }
},
TableName: tableName
};
dynamodb.putItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
callback(err, data);
});
}
exports.deleteRoomFromDB = function(host, hostIP, callback) {
var params = {
Key: {
RoomHost: { S: host }
},
TableName: tableName
};
dynamodb.deleteItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
callback(err, data);
});
}
// Generates a random room ID string
exports.randID = function(sendTo, instructor)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 10; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
exports.sendEmail = function(sendTo, instructor, roomID, callback, externalIP) {
var charset = "utf-8";
console.log(sendTo);
var params = {
Destination: {
ToAddresses: ['[email protected]'],
BccAddresses: sendTo
},
Message: {
Body: {
Html: {
Data: instructor + ' invited you to a workshop. Your workshop ID number is ' + roomID + '. Click this link to join the room http://' + externalIP + ':3000/room/a/' + roomID,
Charset: charset
},
Text: {
Data: instructor + ' invited you to a workshop. Your workshop ID number is ' + roomID + '. Click this link to join the room http://' + externalIP + ':3000/room/a/' + roomID,
Charset: charset
}
},
Subject: {
Data: 'You\'ve been invited to join a workshop!',
Charset: charset
}
},
Source: '[email protected]',
ReturnPath: '[email protected]'
};
ses.sendEmail(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
callback(err, data, "complete-emails");
});
}
// Save a file into s3.
exports.uploadFileToS3Bucket = function(roomID, file, isMain, callback)
{
debugger;
var bucketName = 'tcnj-csc470-nodejs-' + roomID;
var fileName = file['name'] + '.' + file['extension'];
var file = dataUriToBuffer(file['data']);
isMain = typeof isMain !== 'undefined' ? isMain : false;
var params = {
Bucket: bucketName,
Key: fileName,
ACL: 'public-read',
Body: file,
ContentType: file.type
};
if (isMain) {
params.Metadata = {isMain: 'True', name: fileName};
} else {
params.Metadata = {isMain: 'False', name: fileName};
}
s3.putObject(params, function(err, data) {
if (err) {
console.log("File upload failed");
console.log(err, err.stack); // an error occurred
}
else {
console.log("File uploaded into bucket.")
console.log(data); // successful response
}
if (typeof callback !== 'undefined') {
data.roomID = roomID;
callback(err, data);
}
});
}
exports.listObjects = function(roomID, callback)
{
var bucketName = 'tcnj-csc470-nodejs-' + roomID;
var params = {
Bucket: bucketName /* required */
};
s3.listObjects(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else callback(err, data["Contents"], roomID); // successful response
});
}
exports.headObject = function(roomID, key, callback)
{
var bucketName = 'tcnj-csc470-nodejs-' + roomID;
var params = {
Bucket: bucketName, /* required */
Key: key /* required */
};
s3.headObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else callback(err, data["Metadata"], roomID); // successful response
});
}
// Decode a dataURL format for Blob files to array with file type and Base64 encode data.
exports.decodeDataURL = function(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
}
//Sends a message to the Admin to alert to the creation of the room
exports.publish = function(numRooms){
var params = {
Message: 'A new room has been created. The number of current rooms is ' + numRooms, /* required */
TopicArn: 'arn:aws:sns:us-east-1:191927237267:Node_js_project'
};
sns.publish(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
// Create a queue on SQS service.
exports.createQueueSQS = function(roomID, callback) {
var name = 'tcnj-csc470-nodejs-' + roomID;
var params = {
QueueName: name, /* required */
Attributes: {
ReceiveMessageWaitTimeSeconds: '20',
},
};
sqs.createQueue(params, function(err, data) {
if (err) {
console.log("Queue creation failed.");
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
callback(err, data);
});
}
// log the chat history on a SQS queue of a specific room.
exports.logChatHistory = function(roomID, message) {
var queueURL;
// callback for getQueueURLSQS.
function getQueueURLSQSCallback(err, data) {
if (!err) {
queueURL = data.QueueUrl;
aws.sendMessageSQS(queueURL, message);
}
}
// recovering the queue URL.
this.getQueueURLSQS(roomID, getQueueURLSQSCallback);
}
// recover the queue URL on SQS for a specific queue name.
exports.getQueueURLSQS = function(roomID, callback) {
var name = 'tcnj-csc470-nodejs-' + roomID;
var params = {
QueueName: name, /* required */
};
var queueURL;
sqs.getQueueUrl(params, function(err, data) {
if (err) {
console.log("Queue do not exist");
}
else {
console.log(data); // successful response
}
callback(err, data);
});
}
// send a new message to a queue on SQS.
exports.sendMessageSQS = function(queueURL, message) {
var params = {
MessageBody: JSON.stringify(message), /* required */
QueueUrl: queueURL, /* required */
DelaySeconds: 0,
};
sqs.sendMessage(params, function(err, data) {
if (err) {
console.log("Message cannot be sent to queue.")
console.log(err, err.stack); // an error occurred
}
else console.log(data); // successful response
});
}
// this function recover the room chat history from SQS.
exports.recoverChatHistorySQS = function(roomID, callback) {
var queueURL;
// callback for getQueueURLSQS.
function getQueueURLSQSCallback(err, data) {
if (!err) {
queueURL = data.QueueUrl;
aws.receiveMessagesSQS(queueURL, callback);
}
}
// recovering the queue URL.
this.getQueueURLSQS(roomID, getQueueURLSQSCallback);
}
// recover all messages from a queue on SQS.
exports.receiveMessagesSQS = function(queueURL, callback) {
var params = {
QueueUrl: queueURL, /* required */
MaxNumberOfMessages: 10,
VisibilityTimeout: 0,
WaitTimeSeconds: 0,
};
sqs.receiveMessage(params, function(err, data) {
if (err) {
console.log("Messages cannot be recovered.")
console.log(err, err.stack); // an error occurred
}
else console.log(data); // successful response
callback(err, data);
});
}