-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
171 lines (134 loc) · 4.03 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
160
161
162
163
164
165
166
167
168
169
170
171
const nodemailer = require('nodemailer');
const { google } = require('googleapis');
//////////////////////////////////////////
const express = require('express');
const ejs = require('ejs');
const path = require('path');
const File = require("./model/uploader");
const multer = require("multer");
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// join path
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.render('formData');
});
//////////////////////////////////////////
const mongoose = require("mongoose");
process.on("uncaughtException", (err) => {
console.log("UNCAUGHT EXCEPTION, APP SHUTTING NOW!!");
console.log(err.message, err.name);
process.exit(1);
});
const DB = "mongodb://localhost:27017/file-system";
mongoose
.connect(DB, {
useCreateIndex: true,
useFindAndModify: true,
useNewUrlParser: true,
useUnifiedTopology: true,
autoIndex: true,
})
.then(() => {
console.log("DB connected successfully");
});
// multer start here
let dataFileName = "";
const multerStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "public");
},
filename: (req, file, cb) => {
const ext = file.mimetype.split("/")[1];
cb(null, `files/${file.originalname}`);
dataFileName = file.originalname;
console.log(dataFileName);
},
});
// Multer Filter
const multerFilter = (req, file, cb) => {
if (file.mimetype.split("/")[1] === "pdf") {
cb(null, true);
} else {
cb(new Error("Not a PDF File!!"), false);
}
};
//Calling the "multer" Function
const upload = multer({
storage: multerStorage,
fileFilter: multerFilter,
});
////////////////////////////////////////////////////////
// getting user mailelist
// var obj = [];
// async function mailer() {
// let result = await fetch('http://localhost:5000/user/');
// // let data = await result;
// let data = JSON.stringify(result);
// let useData = JSON.parse(data);
// for (let i in useData) {
// obj.push(useData[i].email)
// }
// console.log(obj.toString());
// // if (!err) {
// // }
// }
// mailer();
//////////////////////////////////////////
app.post("/send", upload.single("myFile"), (req, res) => {
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REDIRECT_URI = process.env.REDIRECT_URI;
// client token are the refresh toke
const REFRESH_TOKEN = process.env.REFRESH_TOKEN;
// authorize the client
const oAuth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
// create Credential
oAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN })
// mail function
async function mailSend() {
try {
const accessToken = await oAuth2Client.getAccessToken();
// transport object
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type:'oAuth2',
user: process.env.from,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
refreshToken: REFRESH_TOKEN,
accessToken: accessToken,
},
});
const mailSender = {
from: process.env.FROM,
to: 'xyz@gmail.com', /* or if we have a user list than the list */
subject: 'Hello world',
html: req.body.Body,
attachments: [
{
filename: dataFileName, path: `./public/files/${dataFileName}`
}
]
};
await transporter.sendMail(mailSender, (err, result) => {
if (err) {
console.log(err.message);
} else {
console.log('email sent', result.response)
}
});
} catch(err) {
return err;
}
}
mailSend();
});
app.listen(5000, () => {
console.log('Server is listening at 5000');
})