forked from shubhamns/nodejs-with-firebase-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (68 loc) · 1.78 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
const express = require("express");
const Multer = require("multer");
const { Storage } = require("@google-cloud/storage");
const app = express();
// firebase init
const storage = new Storage({
projectId: "<firebase project id>",
keyFilename: "<filename.json>",
});
const bucket = storage.bucket("<firebase storage bucket name>");
// multer
const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
},
});
// upload image to storage function
const uploadImageToStorage = (file) => {
return new Promise((resolve, reject) => {
if (!file) {
reject("No image file");
}
let newFileName = `${file.originalname}_${Date.now()}`;
let fileUpload = bucket.file(newFileName);
const blobStream = fileUpload.createWriteStream({
metadata: {
contentType: file.mimetype,
},
});
blobStream.on("error", (error) => {
reject(error);
});
blobStream.on("finish", () => {
// The public URL can be used to directly access the file via HTTP.
const url = `https://storage.googleapis.com/${bucket.name}/${fileUpload.name}`;
resolve(url);
});
blobStream.end(file.buffer);
});
};
// Routes
app.post("/upload", multer.single("file"), (req, res) => {
let file = req.file;
if (file) {
uploadImageToStorage(file)
.then((url) => {
return res.status(200).send({
image: url,
});
})
.catch((error) => {
return res.status(500).send({
error: error,
});
});
} else {
return res.status(422).send({
error: "file is required",
});
}
});
// PORT
const port = 3000;
// Starting a server
app.listen(port, () => {
console.log(`app is running at ${port}`);
});