-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
121 lines (95 loc) · 3.21 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 dotenv = require(`dotenv`);
const express = require(`express`);
const session = require('express-session');
const bodyParser = require(`body-parser`);
const fileUpload = require('express-fileupload');
const multer = require("multer");
const { google } = require('googleapis');
const path = require('path');
const db = require(`./models/db.js`);
const fs = require('fs');
const ejs = require('ejs');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
secret: 'secret-key',
resave: true,
saveUninitialized: true,
secure: true
}));
const routes = require(`./routes/routes.js`);
app.set('view engine', 'ejs');
dotenv.config();
port = process.env.PORT;
hostname = process.env.HOSTNAME;
const CLIENT_ID = '390227754248-tu672qpb5v9fc80u23t2048nro0m57kd.apps.googleusercontent.com';
const CLIENT_SECRET = 'uRPVZow7FtKh3_Ks2yWW4uSF';
const REDIRECT_URI = 'https://developers.google.com/oauthplayground';
const REFRESH_TOKEN = '1//04SD-SlCNWjwlCgYIARAAGAQSNwF-L9IrqdtvtSNHwAGmKqpM-B_LqNBRoVIoE5gTxj_g2ZcV5VVJSRdkFcb4GmtMnGnDuqn-F_o';
const filePath = './public/images/uploadedimg.png';
const uploadsFolder = '1NCAJycgdrSufPKuTvScA589zOd9Kl7Vb';
const profileimgFolder = '1VIJ7-Z5WOs4G7e1B7y5OTpQ8D9fPwrJc';
const backgroundimgFolder = '1YhUG_W5XfZXgru7wt8Ik_Y5RcxwACNrj';
const oauth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });
const drive = google.drive({
version: 'v3',
auth: oauth2Client,
});
app.use(express.static(`public`));
db.connect();
async function uploadFile(filename, folderpath) {
try {
const response = await drive.files.create({
requestBody: {
name: Date.now() + ' -- ' + filename,
mimeType: 'image/png',
parents: [folderpath]
},
media: {
mimeType: 'image/png',
body: fs.createReadStream(filePath),
},
});
//makes picture PUBLIC
await drive.permissions.create({
fileId: response.data.id,
requestBody: {
role: 'reader',
type: 'anyone',
},
});
return response.data.id;
} catch (error) {
console.log(error.message);
}
}
var fileStorageEngine = multer.diskStorage({
destination: function (req,file,cb) {
cb(null,'./public/images');
},
filename: function (req, file, cb) {
cb(null, 'uploadedimg.png');
}
});
const upload = multer({ storage: fileStorageEngine });
const Post = require('./models/PostModel.js');
app.post('/single', upload.single('image'), function(req, res) {
uploadFile(req.file.originalname, uploadsFolder ).then(imgurl => {
res.send(imgurl);
});
});
app.post('/singleprofile', upload.single('image'), function(req, res) {
uploadFile(req.file.originalname, profileimgFolder ).then(imgurl => {
res.send(imgurl);
});
});
app.use(`/`, routes);
app.listen(port, hostname, function () {
console.log(`Server is running at:`);
console.log(`http://` + hostname + `:` + port);
});