-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
186 lines (181 loc) · 4.72 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*------------------------------
Globals
------------------------------*/
require("dotenv").config();
const express = require("express");
const app = express();
const http = require("http");
const { Server } = require("socket.io");
const server = http.Server(app);
const io = new Server(server);
const Datastore = require("nedb");
const database = new Datastore("database.db");
const linkDB = new Datastore("link.db");
const Bundler = require("parcel-bundler");
const file = "index.html";
const options = {
outDir: "./dist",
outFile: "index.html",
publicUrl: "/",
watch: true,
};
const bundler = new Bundler(file, options);
const { google } = require("googleapis");
const path = require("path");
const fs = require("fs");
const { resourceLimits } = require("worker_threads");
const { docs } = require("googleapis/build/src/apis/docs");
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REDIRECT_URL = "https://developers.google.com/oauthplayground";
const REFRESH_TOKEN = process.env.REFRESH_TOKEN;
const oauth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URL
);
let arrLinks = [];
/*------------------------------
NeDB
------------------------------*/
database.loadDatabase();
linkDB.loadDatabase();
/*------------------------------
Google API
------------------------------*/
oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });
const drive = google.drive({
version: "v3",
auth: oauth2Client,
});
const filePath = path.join(__dirname, "girl.jpg");
async function createFolder() {
var fileMetadata = {
name: "avenueOfBrokenDreams",
mimeType: "application/vnd.google-apps.folder",
};
drive.files.create(
{
resource: fileMetadata,
fields: "id",
},
function (err, file) {
if (err) {
// Handle error
console.error(err);
} else {
console.log("Folder Id: ", file.id);
}
}
);
}
async function uploadFile() {
const fileMetadata = {
name: "girl.jpg",
parents: ["1Jlz8EVk2TAYb1hi1vBFLkBJVeXAeeawX"],
};
try {
const response = await drive.files.create({
resource: fileMetadata,
requestBody: {
name: "beautifulgirl.jpg",
mimeType: "image/jpg",
parents: ["1Jlz8EVk2TAYb1hi1vBFLkBJVeXAeeawX"],
},
media: {
mimeType: "image/jpg",
body: fs.createReadStream(filePath),
},
});
console.log(response.data);
} catch (error) {
console.log(error.message);
}
}
const generatePublicURL = async (urlID, socket) => {
try {
const fileID = `${urlID}`;
await drive.permissions.create({
fileId: fileID,
requestBody: {
role: "reader",
type: "anyone",
},
});
const result = await drive.files.get({
fileId: fileID,
fields: "webViewLink, webContentLink",
});
return result.data.webViewLink;
// return result;
} catch (error) {
console.log(error);
}
};
function listFiles() {
drive.files.list(
{
q: '"1Jlz8EVk2TAYb1hi1vBFLkBJVeXAeeawX" in parents',
fields: "nextPageToken, files(id, name)",
},
(err, res) => {
if (err) return console.log("The API returned an error: " + err);
const files = res.data.files;
if (files.length) {
console.log("Files:");
files.map((file) => {
database.insert({
file_name: file.name,
file_id: file.id,
});
// console.log(`${file.name} (${file.id})`);
});
} else {
console.log("No files found.");
}
}
);
}
const addToDB = () => {
database.find({}, function (err, docs) {
for (let i = 0; i < docs.length; i++) {
let temp = generatePublicURL(docs[i].file_id);
generatePublicURL(docs[i].file_id);
arrLinks.push(temp);
}
Promise.all(arrLinks).then((values) => {
for (let i = 0; i < values.length; i++) {
linkDB.insert({
webLink: values[i],
});
}
});
});
};
// uploadFile();
// generatePublicURL("17Pv57alJu5sbXtT2U8v-r4xm7IhpxwjA");
// createFolder();
// listFiles();
// let test = addToDB();
/*------------------------------
Initialize
------------------------------*/
server.listen(3000);
io.on("connection", (socket) => {
linkDB.find({}, function (err, docs) {
io.to(socket.id).emit("hereYouGo", docs);
// io.emit();
});
// database.find({}, function (err, docs) {
// for (let i = 0; i < docs.length; i++) {
// let temp = generatePublicURL(docs[i].file_id);
// generatePublicURL(docs[i].file_id);
// arrLinks.push(temp);
// }
// console.log("running?");
// Promise.all(arrLinks).then((values) => {
// io.emit("backToYou", values);
// });
// });
});
app.use(bundler.middleware());