-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
159 lines (143 loc) · 3.39 KB
/
app.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
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
var AWS = require('aws-sdk');
var Async = require('async');
var fs = require('fs');
// Create an S3 client
var s3 = new AWS.S3();
AWS.config.update({
region: "us-east-1"
});
var docClient = new AWS.DynamoDB.DocumentClient();
var dynamodb = new AWS.DynamoDB();
app.use(express.static('./public'));
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, path.resolve('public/uploads'));
},
filename: function(req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
});
function getDesc(da, cb) {
var desc = [];
for (var i = 0; i < da.Contents.length; i++) {
var pics = {};
pics['picname'] = da.Contents[i].Key;
pics['desc'] = '';
console.log(pics.picname)
var params = {
'TableName': 'pictureslabel',
'Key': {
'year': 2019,
'picname': pics.picname
}
};
desc.push(pics);
}
var query = {
TableName: "hejiewei",
Limit: 1000
};
dynamodb.scan(query, function(err, data) {
console.log("啊" + JSON.stringify(data));
var Items = data.Items;
for (var i = 0; i < desc.length; i++) {
for (var j = 0; j < data.Count; j++) {
console.log(desc[i].picname)
console.log(Items[j].picname)
if (desc[i].picname == Items[j].picname.S) {
desc[i].desc = Items[j].desc.S;
}
}
}
console.log(desc)
cb(null, desc);
}
)
}
const upload = multer({
storage: storage
});
app.post('/getpic', function(req, res, next) {
Async.waterfall([
function(cb) {
let getParams = {
Bucket: 'hejiewei',
};
s3.listObjectsV2(getParams, function(err, da) {
cb(err, da)
});
},
function(da, cb) {
getDesc(da, function(err, picD) {
console.log("picD" + picD);
cb(err, picD);
})
}
], function(err, picD) {
if (err) {
console.log("错误" + err);
res.send(err);
} else {
var datas = [];
console.log(picD.length)
var prefix = 'https://hejiewei.s3.amazonaws.com/'
for (var i = 0; i < picD.length; i++) {
var data = {};
data['pic'] = prefix + picD[i].picname;
data['desc'] = picD[i].desc;
datas.push(data);
}
res.send(datas);
}
});
})
app.post('/profile', upload.single('avatar'), function(req, res, next) {
console.log(req.file.originalname);
var text = req.body.desc;
var params = {
Item: {
"picname": {
S: req.file.originalname
},
"desc": {
S: text
}
},
ReturnConsumedCapacity: "TOTAL",
TableName: "hejiewei"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
}
});
fs.readFile(req.file.path, function(err, data) {
if (err) {
throw err;
}
s3.putObject({
Bucket: 'hejiewei',
Key: req.file.originalname,
Body: data,
ContentType: 'image/jpeg',
ACL: 'public-read'
}, function(err, data) {
if (err) console.log(err, err.stack);
else
console.log('Successfully uploaded package.');
});
});
res.send({
err: null,
filePath: 'uploads/' + path.basename(req.file.path)
});
});
app.listen(3000, function() {
console.log("app is listening");
});