-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
222 lines (177 loc) · 7.14 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require('dotenv').config();
const cors = require('cors')
const express = require("express");
const millennium = express()
// if (process.env.NODE_ENV !== 'production') {
// millennium.listen(3000)
// }
/* Setup express posting and CORS */
millennium.use(express.json())
millennium.use(express.urlencoded({ extended: true }))
millennium.use(cors());
var admin = require("firebase-admin");
const functions = require("firebase-functions")
const { cache_handler, reset } = require("./middleware/cache.js")
const { rate_limit } = require("./middleware/rate-limiter.js")
admin.initializeApp({
credential: admin.credential.cert(require('./credentials/cert.json')),
storageBucket: "millennium-d9ce0.appspot.com"
});
const bucket = admin.storage().bucket();
const db = admin.firestore();
const { check_updates } = require("./v2/check-updates.js")
const { get_details } = require('./v2/get-details.js')
const { get_featured } = require('./v2/featured.js')
const { get_update_v2 } = require("./v2/get-update.js")
const { get_update } = require("./v2/get-update.js")
const { download } = require("./v2/download.js")
const { RetrievePluginList } = require("./plugin/GetPluginList.js")
const { GetPluginData } = require("./plugin/GetPluginData.js")
const { GetDownloadInfo } = require("./plugin/GetDownloadInfo.js")
const { GetPluginMetadata } = require("./plugin/GetPluginMetadata.js")
millennium.get("/api/updater", cache_handler, (_, res) => {
res.json({
up: true,
message: "200 - good to go.",
url: "https://api.github.com/repos/ShadowMonster99/millennium-steam-binaries/releases/latest"
})
})
millennium.get("/api/v2/details/:id", cache_handler, async (req, res) => {
get_details(req)
.then(details => res.json(details))
.catch(error => res.status(500).json({success: false, message: error.toString()}));
})
millennium.get("/api/v2", cache_handler, (_, res) => {
get_featured()
.then(details => res.json(details))
.catch(error => res.status(500).json({success: false, message: error.toString()}));
})
millennium.post("/api/v2/update", (req, res) => {
get_update_v2(req)
.then(result => res.json(result))
.catch(error => res.status(500).json({success: false, message: error.toString()}))
})
/* Deprecated */
millennium.post("/api/v2/get-update", cache_handler, (req, res) => {
get_update(req)
.then(result => res.json(result))
.catch(error => res.status(500).json({success: false, message: error.toString()}))
})
millennium.post("/api/v2/download", rate_limit, (req, res) => {
download(req)
.then(result => res.json(result))
.catch(error => res.status(500).json({success: false, message: error.toString()}))
})
millennium.post("/api/v2/checkupdates", async (req, res) => {
check_updates(req)
.then(result => res.json(result))
.catch(error => res.status(500).json({success: false, message: error.toString()}))
})
millennium.get("/api/cache/reset", cache_handler, (_, res) => {
reset()
res.json({
success: true
})
})
/**
* Support for plugins
*/
const FetchPlugins = async () => {
return new Promise(async (resolve, reject) => {
try {
const pluginList = await RetrievePluginList();
// Fetch all plugin metadata, data, and download counts in parallel
const [metadata, pluginData] = await Promise.all([
GetPluginMetadata(),
GetPluginData(pluginList)
]);
// Fetch all download counts in one Firestore call
const downloadDocs = await db.collection('downloads').get();
const downloadCounts = {};
downloadDocs.forEach(doc => {
downloadCounts[doc.id] = doc.data().downloadCount || 0;
});
// Process the plugin data
for (const key in pluginData) {
const data = metadata.find(meta => meta.commitId === pluginData[key].id);
if (data) {
const pluginId = data.id.substring(0, 12); // Shortened ID
const initCommitId = data.id; // Full ID
pluginData[key].downloadCount = downloadCounts[initCommitId] ?? 0;
pluginData[key].id = pluginId;
pluginData[key].initCommitId = initCommitId;
}
}
resolve(pluginData);
}
catch (error) {
console.error("An error occurred while processing plugins:", error);
reject(error);
}
});
};
millennium.get("/api/v1/plugins", cache_handler, async (req, res) => {
res.json(await FetchPlugins())
})
millennium.get("/api/v1/plugin/:id", cache_handler, async (req, res) => {
const plugin = (await FetchPlugins()).find(plugin => plugin.id === req.params.id)
if (!plugin) {
res.status(404).json({ error: 1 });
return;
}
try {
const pluginBuild = bucket.file(`plugins/${plugin.initCommitId}.zip`);
const [ exists ] = await pluginBuild.exists()
if (exists) {
const [ metadata ] = await pluginBuild.getMetadata()
plugin.commitDate = new Date(metadata.updated) > new Date(metadata.timeCreated) ? metadata.updated : metadata.timeCreated;
plugin.fileSize = Number(metadata.size);
plugin.hasValidBuild = true;
}
else {
console.warn(`Plugin ${plugin.id} does not have a build available.`)
plugin.hasValidBuild = false;
}
}
catch (error) {
console.error("An error occurred while checking plugin build:", error);
plugin.hasValidBuild = false;
}
plugin.downloadUrl = `/api/v1/plugins/download/?id=${plugin?.initCommitId}&n=${plugin?.pluginJson?.name}.zip`;
res.json(plugin)
})
millennium.get('/api/v1/plugins/download', async (req, res) => {
console.log("Getting download info for", req.query.id);
const fileName = req.query.id;
console.log(req.query)
const downloadFileName = req.query.n;
console.log("using filename", downloadFileName);
const file = bucket.file(`plugins/${fileName}.zip`);
// Increment the download count in Firestore
try {
const docRef = db.collection('downloads').doc(fileName);
await docRef.set(
{ downloadCount: admin?.firestore?.FieldValue?.increment(1) ?? 1 },
{ merge: true }
);
console.log(`Download count incremented for ${fileName}`);
}
catch (err) {
console.error('Error updating download count:', err);
// Continue processing the request even if updating count fails
}
file.createReadStream()
.on('error', (err) => {
console.error('Error streaming file:', err);
res.status(500).send('Error streaming file');
})
.on('response', (response) => {
res.setHeader('Content-Type', response.headers['content-type']);
res.setHeader('Content-Disposition', `attachment; filename="${downloadFileName}"`);
})
.pipe(res)
.on('finish', () => {
console.log(`File ${fileName} streamed successfully!`);
});
});
exports.api = functions.https.onRequest(millennium)