-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocketServer.js
91 lines (79 loc) · 2.29 KB
/
pocketServer.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
var express = require('express'),
mongoskin = require('mongoskin'),
bodyParser = require('body-parser'),
busboy = require('connect-busboy'),
AWS = require('aws-sdk')
var app = express()
AWS.config.loadFromPath('./awsConfig.json')
app.use(bodyParser({limit: '50mb'}))
app.use(busboy())
var db = mongoskin.db('mongodb://@localhost:27017/test', {safe:true})
var s3 = new AWS.S3()
app.get('/', function(req, res) {
res.send('I am awake...')
})
app.get('/objects/', function(req, res) {
s3.listObjects({Bucket: 'pocketserver-development'}, function(er, data) {
if (er) {
res.send(400, er)
} else {
res.send(200, data)
}
})
})
app.post('/object/', function(req, res) {
console.log('processing POST')
var orderLabel
var error = null
req.busboy.on('field', function(fieldName, val, fieldNameTruncated, valTruncated) {
if (fieldName === 'deviceToken') {
console.log('field: ' + fieldName + ' value: ' + val)
db.collection('orders').insert({deviceToken : val}, {}, function(e, results) {
if (e) {
error = e
} else {
console.log(results)
var label = results[0]._id
orderLabel = results[0]._id
console.log(orderLabel)
console.log("label: " + label)
}
})
}
})
req.busboy.on('file', function(fieldName, file, fileName) {
//console.log(file.length)
// Create the initial array containing the stream's chunks
file.fileRead = []
file.on('data', function(chunk) {
// Push chunks into the fileRead array
this.fileRead.push(chunk)
});
file.on('end', function() {
// Concat the chunks into a Buffer
var finalBuffer = Buffer.concat(this.fileRead)
s3.putObject({Bucket: 'pocketserver-development', Body: finalBuffer, Key: orderLabel + '/' + fileName, ContentLength: finalBuffer.length}, function(err, data) {
if (err) {
console.log(err)
error = err
//didCompleteSuccess = false
//res.send(400)
}
else {
//res.send(201)
console.log('success!')
}
})
})
})
req.pipe(req.busboy)
req.busboy.on('end', function() {
if (error) {
res.send(400, error)
}
else {
res.send(201)
}
})
})
app.listen(3000)