-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
170 lines (138 loc) · 5.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
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
import express from "express";
import fileUpload from "express-fileupload";
import path from 'path';
import fs from 'fs';
import gplay from 'google-play-scraper';
const fileExtLimiter = (allowedExtArray) => {
return (req, res, next) => {
const files = req.files;
const fileExtensions = [];
Object.keys(files).forEach(key => {
fileExtensions.push(path.extname(files[key].name));
});
// Are the file extensions allowed?
const allowed = fileExtensions.every(ext => allowedExtArray.includes(ext));
if (!allowed) {
const message = `Upload failed. Only ${allowedExtArray.toString()} files allowed.`.replaceAll(",", ", ");
return res.status(422).json({ status: "error", message });
}
next();
}
};
const app = express();
const PORT = process.env.PORT || 5000;
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
// File Upload
app.post('/upload',
fileUpload({ createParentPath: true }),
async (req, res) => {
try {
const files = req.files;
const apkData = JSON.parse(req.body.apkData); // Assuming you send the ApkData as a stringified JSON in the request body
if (files && files.file) {
// File upload
const uploadedFile = files.file; // Assuming the file input field name is 'file'
const filePath = path.join('./src', 'dumps', uploadedFile.name);
uploadedFile.mv(filePath, (err) => {
if (err) return res.status(500).json({ status: "error", message: err });
const jsonFilePath = path.join('./src', 'dumps', 'uploadedData.json');
// Check if the JSON file already exists
if (fs.existsSync(jsonFilePath)) {
const existingData = JSON.parse(fs.readFileSync(jsonFilePath));
// Update or add the data based on the appId
const updatedData = {
...existingData,
[apkData.appId]: {
filePath,
name: apkData.title,
icon: apkData.icon,
version: apkData.version,
Engine: apkData.Engine
}
};
fs.writeFileSync(jsonFilePath, JSON.stringify(updatedData));
return res.json({ status: 'success', message: 'File uploaded successfully' });
} else {
const jsonData = {
[apkData.appId]: {
filePath,
icon: apkData.icon,
version: apkData.version,
Engine: apkData.Engine
}
};
fs.writeFileSync(jsonFilePath, JSON.stringify(jsonData));
return res.json({ status: 'success', message: 'File uploaded successfully' });
}
});
} else {
// Data without a file
const jsonFilePath = path.join('./src', 'dumps', 'uploadedData.json');
// Check if the JSON file already exists
if (fs.existsSync(jsonFilePath)) {
const existingData = JSON.parse(fs.readFileSync(jsonFilePath));
// Update or add the data based on the appId
const updatedData = {
...existingData,
[apkData.appId]: {
name: apkData.title,
icon: apkData.icon,
version: apkData.version,
Engine: apkData.Engine
}
};
fs.writeFileSync(jsonFilePath, JSON.stringify(updatedData));
return res.json({ status: 'success', message: 'Data uploaded successfully' });
} else {
const jsonData = {
[apkData.appId]: {
name: apkData.title,
icon: apkData.icon,
version: apkData.version,
Engine: apkData.Engine
}
};
fs.writeFileSync(jsonFilePath, JSON.stringify(jsonData));
return res.json({ status: 'success', message: 'Data uploaded successfully' });
}
}
} catch (error) {
return res.status(500).json({ status: "error", message: error.message });
}
}
);
// Web Scraping
app.get('/search', async (req, res) => {
const searchTerm = req.query.q;
try {
const results = await gplay.search({
term: searchTerm.toString(),
num: 5,
fullDetail: true
});
return res.json(results);
} catch (error) {
return res.status(500).json({ status: "error", message: error.message });
}
});
// Fetch data from JSON file
app.get('/data', (req, res) => {
try {
const jsonFilePath = path.join('./src', 'dumps', 'uploadedData.json');
const jsonData = JSON.parse(fs.readFileSync(jsonFilePath));
return res.json(jsonData);
} catch (error) {
return res.status(500).json({ status: "error", message: error.message });
}
});
app.get('/download', (req, res) => {
const filePath = req.query.filePath;
const absoluteFilePath = filePath
res.download(absoluteFilePath);
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));