-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
238 lines (195 loc) · 6.92 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const fs = require("fs");
const path = require("path");
const express = require("express");
const QRCode = require("qrcode");
const multer = require("multer");
const app = express();
const baseURL = "https://dsidl-transfer-tool.glitch.me";
const baseHTML = `<!DOCTYPE html>
<head>
<title>dsidl transfer tool</title>
<link rel="icon" type="image/jpeg" href="https://danilionn.github.io/danis-bot-website/assets/images/dsidl-transfer-tool-icon.png">
<style>
@font-face {
font-family: 'Pixel';
src: url('https://danilionn.github.io/danis-bot-website/assets/fonts/JSE_Commodore64.woff2') format('woff2'),
url('https://danilionn.github.io/danis-bot-website/assets/fonts/JSE_Commodore64.woff') format('woff');
}
.qr {
display: inline-block;
padding-top: 10px;
padding-right: 10px;
padding-bottom: 10px;
padding-left: 10px;
}
.center {
margin: auto;
width: 80%;
}
body {
background-color: #000000;
}
p, a, form {
color: #FFFFFF;
font-family: Pixel;
;
}
h3 {
color: #FFFFFF;
font-family: Pixel;
display: inline-block;
}
#uploadButton, #clearButton {
display: none;
}
</style>
</head>
<body>
<div class="center">
<h3 style="text-align:center;">
<img src="https://danilionn.github.io/danis-bot-website/assets/images/dsidl-transfer-tool-icon.png" width="32" height="32" style="padding-left: 5px">
QR codes are meant to be scanned with the
<a href="https://github.com/Epicpkmn11/dsidl" target="_blank">
dsidl homebrew app</a> for the DSi.
<img src="https://danilionn.github.io/danis-bot-website/assets/images/dsidl-transfer-tool-icon.png" width="32" height="32" style="padding-left: 5px">
</h3>
<p style="text-align:center;">
Upload a file and it'll be turned into a QR Code that you can scan with dsidl to download to your DSi!
</p>
<p style="text-align:center;">
Uploaded files are deleted after being scanned and sent over, so QR Codes are one use only.
</p>
</div>
<br>`;
const htmlFinalPart = `<br><br><br><div class="center">
<form style="text-align:center;" action="/upload" method="post" enctype="multipart/form-data">
<input type="file" id="fileInput" name="file" onchange="showUploadButton()">
<button type="submit" id="uploadButton" >Upload</button>
<button type="button" id="clearButton" onclick="clearFileInput()">Clear Selection</button>
</form>
<script>
function showUploadButton() {
const fileInput = document.getElementById('fileInput');
const uploadButton = document.getElementById('uploadButton');
const clearButton = document.getElementById('clearButton');
if (fileInput.files.length > 0) {
uploadButton.style.display = 'inline-block';
clearButton.style.display = 'inline-block';
} else {
uploadButton.style.display = 'none';
clearButton.style.display = 'none';
}
}
function clearFileInput() {
const fileInput = document.getElementById('fileInput');
const uploadButton = document.getElementById('uploadButton');
const clearButton = document.getElementById('clearButton');
fileInput.value = '';
uploadButton.style.display = 'none';
clearButton.style.display = 'none';
}
</script>
</div>
<br><br><br><br>
<div class="center">
<p style="text-align:center;">Check out the <a href="https://github.com/DaniLionn/dsidl-transfer-tool">github repo!<img src="https://danilionn.github.io/danis-bot-website/assets/images/github-mark-white.png" width="25" height="25" style="padding-left: 5px"></a></p>
</div>
</body>`;
var finalHTML = baseHTML;
const options = {
root: path.join(__dirname),
};
function createQRCode(file) {
return new Promise((resolve, reject) => {
QRCode.toDataURL(
JSON.stringify([
`QR Code for file ${file} was scanned!`,
`${baseURL}/${file}`,
"Thanks for downloading!",
]),
(err, url) => {
if (err) {
reject("Error generating QR code");
} else {
const qrCodeHtml = `<div id="${file}" class="qr"><p style="text-align:center">Download ${file}:</p><br><img src="${url}" alt="QR Code" width="312px" height="312px"</div>`;
//"<form action="/clearQR?file=${file}&url=${url}" method="get" enctype="multipart/form-data"><button type="submit" id="clearQR" >Clear</button></form>"
finalHTML += qrCodeHtml;
resolve();
}
}
);
});
}
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads/");
},
filename: function (req, file, cb) {
console.log(file);
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });
app.post("/upload", upload.single("file"), (req, res) => {
const uploadedFile = req.file;
if (uploadedFile) {
console.log("uploaded");
const filePath = path.join("./uploads", uploadedFile.filename);
app.use(encodeURI(`/${uploadedFile.filename}`), function (req, res, next) {
console.log("Got request! Sending file...");
res.sendFile(filePath, options, function (err) {
if (err) {
console.log(err);
} else {
console.log("Sent:", filePath);
fs.promises.unlink(filePath);
}
});
});
createQRCode(encodeURI(uploadedFile.filename)).then(() => {
res.redirect(`${baseURL}?resetPage=false`);
});
} else {
res.redirect(baseURL);
}
});
app.get("/test", function (req, res) {
res.send("OK");
});
// app.get("/clearQR", function (req, res) {
// let file = req.query.file
// let url = req.query.url
// console.log(file,url)
// const qrCodeHtml = `<div id="${file}" class="qr"><p style="text-align:center">Download ${file}:</p><br><img src="${url}" alt="QR Code" width="312px" height="312px"><form action="/clearQR?file=${file}" method="post" enctype="multipart/form-data"><button type="submit" id="clearQR" onclick="clearQR()">Clear</button></form></div>`;
// console.log(qrCodeHtml, finalHTML.includes(qrCodeHtml))
// if (finalHTML.includes(qrCodeHtml)) {
// finalHTML = finalHTML.replace(qrCodeHtml, "");
// const filePath = path.join("./uploads", file);
// fs.promises.unlink(filePath);
// }
// res.redirect(`${baseURL}?resetPage=false`);
// });
app.get("/", function (req, res) {
let resetPage = req.query.resetPage;
if (!resetPage || resetPage == "true") {
finalHTML = baseHTML;
res.send(finalHTML + htmlFinalPart);
} else {
res.send(finalHTML + htmlFinalPart);
}
});
app.listen(3000, function (err) {
if (err) console.log(err);
console.log("Server listening on port 3000");
});
//delete any leftover uploaded files on startup
fs.readdir("./uploads", function (err, files) {
if (err) {
console.log(err);
} else {
files.forEach((file) => {
const filePath = path.join("./uploads", file);
fs.promises.unlink(filePath);
});
}
});