-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (125 loc) · 4.4 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
require('dotenv').config();
const express = require('express');
const app = express();
app.use(express.json({ limit: "100mb" }));
app.use(express.urlencoded({ limit: "100mb", extended: true }));
const WAHelper = require("./whatsapp.helper");
const path = require("path");
const multer = require("multer");
// CONFIGURATION
const fileSize = 250; // 250MB
const fileTypes = /jpeg|jpg|png|mp4|pdf/;
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: (1024 * 1024 * fileSize) // 250 MB
},
fileFilter: async (req, file, cb) => {
// VALIDATE FILE EXT
const extname = fileTypes.test(path.extname(file.originalname).toLowerCase());
if (extname && file.mimetype) {
cb(null, true);
} else {
cb(new Error(`Only ${fileTypes.toString()} extentions are allowed!`), false);
}
}
});
// UPLOAD MEDIA ROUTE
app.use('/uploadMedia', async (req, res) => {
// UPLOAD
upload.single('file')(req, res, async (error) => {
// PROBABLY FILE SIZE ERROR
if (error instanceof multer.MulterError) {
// LOG ERROR, AND RESPONSE
console.error(error);
res.status(400).send({
message: `Max file size ${fileSize} allowed!`
});
}
// SOMETHING OTHER ERROR
else if (error) {
// LOG ERROR, AND RESPONSE
console.error(error);
res.status(400).send({
message: `Something went wrong please try again!`
});
}
// FILE IS REQUIRED
else if (!req.file) {
// LOG ERROR, AND RESPONSE
res.status(400).send({
message: `File is required!`
});
}
else {
// CREATE SESSION
let session = await WAHelper.RUCreateSession({
file_length: req.file.size,
file_name: req.file.originalname,
file_type: req.file.mimetype
});
if (session.body.error) {
// LOG ERROR, AND RESPONSE
console.error(session.body.error);
return res.status(400).send({
message: session.body.error.error_user_title ? session.body.error.error_user_title + ` (${session.body.error.error_user_msg})` : session.body.error.message
});
}
//INITIATE UPLOAD
let iupload = await WAHelper.RUInitiateUpload(session.body.id, req.file.buffer);
if (iupload.body.h) {
// SUCCESS RESPONSE
console.error(iupload.body);
return res.status(200).send({
message: "Uploaded!",
body: iupload.body
});
}
// ERROR
else if (iupload.body.error) {
// LOG ERROR, AND RESPONSE
console.error(iupload.body.error);
return res.status(400).send({
message: iupload.body.error.error_user_title ? iupload.body.error.error_user_title + ` (${iupload.body.error.error_user_msg})` : iupload.body.error.message
});
}
else {
// LOG ERROR, AND RESPONSE
console.error(iupload);
return res.status(400).send({
message: "Something went wrong please try again!"
});
}
}
});
});
// CREATE TEMPLATE
app.use('/createTemplate', async (req, res) => {
let template = await WAHelper.createWABANOTemplates(req.body);
if (template.body.id) {
return res.status(200).send({
message: "Template Created!",
body: template.body
});
}
// ERROR
else if (template.body.error) {
// LOG ERROR, AND RESPONSE
console.error(template.body.error);
return res.status(400).send({
message: template.body.error.error_user_title ? template.body.error.error_user_title + ` (${template.body.error.error_user_msg})` : template.body.error.message
});
}
// LOG ERROR, AND RESPONSE
console.error(template);
return res.status(400).send({
message: "Something went wrong please try again!"
});
});
// SERVER LISTEN
const port = process.env.PORT;
app.listen(port, () => {
console.info(`Listening on port ${port}...`)
}).on("error", (err) => {
console.error(err.message);
});