-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (109 loc) · 3.64 KB
/
index.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
const express = require('express');
const app = express();
const spicedPg = require('spiced-pg');
const fs = require('fs');
var multer = require('multer');
var uidSafe = require('uid-safe');
var path = require('path');
var db = spicedPg(`postgres:webuser:letmein@localhost:5432/imageboard`);
var url = require('./config.json').s3Url;
var diskStorage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, __dirname + '/uploads');
},
filename: function(req, file, callback) {
uidSafe(24).then(function(uid) {
callback(null, uid + path.extname(file.originalname));
});
}
});
var uploader = multer({
storage: diskStorage,
limits: {
filesize: 2097152
}
});
const knox = require('knox');
let secrets;
if (process.env.NODE_ENV == 'production') {
secrets = process.env; // in prod the secrets are environment variables
} else {
secrets = require('./secrets'); // secrets.json is in .gitignore
}
const client = knox.createClient({
key: secrets.AWS_KEY,
secret: secrets.AWS_SECRET,
bucket: 'reallydavid'
});
app.use(require('body-parser').urlencoded({
extended: false
}));
var favicon = require('serve-favicon');
app.use(favicon(__dirname + '/public/images/favicon.ico'));
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index');
});
app.get('/images', function(req, res) {
db.query('SELECT * FROM images').then(function(result) {
// console.log(result.rows);
for (var i in result.rows) {
result.rows[i].url = url;
}
res.json({
data: result.rows.reverse()
});
});
});
app.get('/image/:id', function(req, res) {
db.query(`SELECT * FROM images WHERE image = $1`, [req.params.id]).then(function(result){
result.rows[0].url = url;
var data = result.rows[0];
db.query(`SELECT * FROM comments WHERE comment_id = $1 ORDER BY created_at DESC NULLS LAST`, [result.rows[0].id]).then(function(result){
data.comments = result.rows;
// console.log(data);
res.json( data );
});
}).catch(function(err) {
console.log(err);
});
});
app.post('/upload', uploader.single('file'), uploadToS3, function(req, res) {
// If nothing went wrong the file is already in the uploads directory
// console.log(req.file);
// console.log(req.body);
db.query(`INSERT INTO images (image, title, username, description) VALUES ($1, $2, $3, $4)`, [req.file.filename, req.body.title, req.body.user, req.body.descr]);
res.json({
success: true
});
});
app.post('/comment/add/:id', function (req, res) {
db.query(`SELECT id FROM images WHERE image = $1`, [req.params.id]).then(function(result){
// console.log(req.body);
// console.log(result);
db.query(`INSERT INTO comments (comment, author, comment_id) VALUES ($1, $2, $3)`, [req.body.comment, req.body.author, result.rows[0].id]);
res.json({
success: true
});
});
});
app.listen(8080, () => console.log(`I'm listening.`));
function uploadToS3(req, res, next) {
// console.log(req);
const s3Request = client.put(req.file.filename, {
'Content-Type': req.file.mimetype,
'Content-Length': req.file.size,
'x-amz-acl': 'public-read'
});
const readStream = fs.createReadStream(req.file.path);
readStream.pipe(s3Request);
s3Request.on('response', function(resp) {
if (resp.statusCode != 200) {
res.json({
success: false
});
} else {
next();
}
});
}