-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
121 lines (110 loc) · 3.25 KB
/
server.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
const express = require('express')
const multer = require("multer")
const crypto = require("node:crypto")
const fs = require("node:fs/promises")
const { connectToDb } = require('./lib/mongo')
const { connectToRabbitMQ, getChannel } = require('./lib/rabbitmq')
const {
getImageInfoById,
saveImageFile,
getImageDownloadStreamByFilename
} = require('./models/image')
const app = express()
const port = process.env.PORT || 8000
const imageTypes = {
"image/jpeg": "jpg",
"image/png": "png",
"image/gif": "gif"
}
const upload = multer({
storage: multer.diskStorage({
destination: `${__dirname}/uploads`,
filename: (req, file, callback) => {
const filename = crypto.pseudoRandomBytes(16).toString("hex")
const extension = imageTypes[file.mimetype]
callback(null, `${filename}.${extension}`)
}
}),
fileFilter: (req, file, callback) => {
callback(null, !!imageTypes[file.mimetype])
}
})
app.post("/images", upload.single("image"), async function (req, res, next) {
if (req.file && req.body && req.body.userId) {
const image = {
contentType: req.file.mimetype,
filename: req.file.filename,
path: req.file.path,
userId: req.body.userId
}
try {
const id = await saveImageFile(image)
const channel = getChannel()
channel.sendToQueue("images", Buffer.from(id.toString()))
await fs.unlink(req.file.path)
res.status(200).send({
id: id
})
} catch(err) {
next(err)
}
} else {
res.status(400).send({
err: "Invalid file"
})
}
})
app.get('/images/:id', async (req, res, next) => {
try {
const image = await getImageInfoById(req.params.id)
if (image) {
const resBody = {
_id: image._id,
contentType: image.metadata.contentType,
userId: image.metadata.userId,
tags: image.metadata.tags,
url: `/media/images/${image.filename}`
}
res.status(200).send(resBody)
} else {
next()
}
} catch (err) {
next(err)
}
})
app.get("/media/images/:filename", function (req, res, next) {
getImageDownloadStreamByFilename(req.params.filename)
.on("error", function (err) {
if (err.code === "ENOENT") {
next()
} else {
next(err)
}
})
.on("file", function (file) {
res.status(200).type(file.metadata.contentType)
})
.pipe(res)
})
app.use('*', (req, res, next) => {
res.status(404).send({
err: "Path " + req.originalUrl + " does not exist"
})
})
/*
* This route will catch any errors thrown from our API endpoints and return
* a response with a 500 status to the client.
*/
app.use('*', (err, req, res, next) => {
console.error("== Error:", err)
res.status(500).send({
err: "Server error. Please try again later."
})
})
connectToDb(async () => {
await connectToRabbitMQ("images")
app.listen(port, () => {
console.log("== Server is running on port", port)
})
})