-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
373 lines (310 loc) · 13 KB
/
upload.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const express = require('express');
const router = express.Router();
const i18n = require('i18n');
const path = require('path');
const fs = require('fs');
const sharp = require('sharp');
const fileUpload = require('express-fileupload');
const { Pool } = require("pg");
require("dotenv").config();
const { getLanguageData} = require("./parents");
// Add pool configuration
const pool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
ssl: { rejectUnauthorized: process.env.PG_SSL_REJECT_UNAUTHORIZED === "true" },
});
// Add this near the top of the file, after the requires
router.use(fileUpload());
router.use(express.json());
router.use(express.urlencoded({ extended: true }));
// Handle file upload
router.post("/api/upload", async (req, res) => {
try {
console.log("Request body:", req.body);
console.log("Request files:", req.files);
const lang = req.body.lang || 'en';
if (!req.body.problemName || !req.body.method) {
return res.status(400).json({
success: false,
message: 'Problem name and solution method are required'
});
}
// Check if user is authenticated or provided full name
if (!req.session.userId && !req.body.fullName) {
return res.status(400).json({
success: false,
message: 'Please provide your full name or login to continue'
});
}
const problemName = req.body.problemName;
const solution = req.body.method === 'latex' ? req.body.latexContent : '';
// Get language data for validation
const { chapters } = await getLanguageData(lang);
// Validate problem name format
if (!problemName || !problemName.match(/^\d+\.\d+\.\d+$/)) {
return res.status(400).json({
success: false,
message: "Invalid problem name format. Use chapter.section.problem format."
});
}
// Use different variable names to avoid conflict
const [chapterNum, sectionNum, probNum] = problemName.split('.').map(Number);
// Validate chapter exists
const currentChapter = chapters[chapterNum - 1]; // Adjust for zero-based indexing
if (!currentChapter) {
return res.status(400).json({
success: false,
message: `Chapter ${chapterNum} does not exist.`
});
}
// Validate section exists
const currentSection = currentChapter.sections[sectionNum - 1]; // Adjust for zero-based indexing
if (!currentSection) {
return res.status(400).json({
success: false,
message: `Section ${chapterNum}.${sectionNum} does not exist.`
});
}
// Validate problem number against section maximum
const maxProblems = currentSection.maximum;
if (parseInt(probNum) > parseInt(maxProblems)) {
return res.status(400).json({
success: false,
message: `Problem number (${probNum}) exceeds the maximum allowed (${maxProblems}) for Section ${chapterNum}.${sectionNum}.`
});
}
// Generate markdown content based on language
const content = lang === 'ru' ?
`### Условие
$${probNum}.$ ${solution}
### Решение
[Здесь должно быть ваше решение]
#### Ответ
[Вставьте краткий ответ или результат в рамке]`
:
`### Statement
$${probNum}.$ ${solution}
### Solution
[Your solution should be placed here]
#### Answer
[Insert a concise answer or boxed result]`;
// Create directory if it doesn't exist
const problemsDir = path.join(__dirname, "posts", lang);
fs.mkdirSync(problemsDir, { recursive: true });
const filePath = path.join(problemsDir, `${problemName}.md`);
// Check if file already exists
if (fs.existsSync(filePath)) {
return res.status(400).json({
success: false,
message: "Problem file already exists."
});
}
// Update the files check to properly handle FormData uploads
const uploadedFiles = [];
if (req.files) {
// Handle both 'files' and 'illustrations' arrays if they exist
const fileArrays = ['files', 'illustrations'];
for (const fileType of fileArrays) {
if (req.files[fileType]) {
const files = Array.isArray(req.files[fileType])
? req.files[fileType]
: [req.files[fileType]];
uploadedFiles.push(...files);
}
}
}
console.log("Processed uploaded files:", uploadedFiles);
const imageResults = [];
// Create base img directory if it doesn't exist
const baseImgDir = path.join(__dirname, 'img', problemName);
fs.mkdirSync(baseImgDir, { recursive: true });
console.log(uploadedFiles);
// Process images only if files were uploaded
if (uploadedFiles.length > 0) {
const uploadDir = path.join(__dirname, 'img', problemName);
fs.mkdirSync(uploadDir, { recursive: true });
// Add size validation
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
const MAX_TOTAL_SIZE = 20 * 1024 * 1024; // 20MB
let totalSize = 0;
for (const file of uploadedFiles) {
// Check individual file size
if (file.size > MAX_FILE_SIZE) {
return res.status(400).json({
success: false,
message: `File ${file.name} exceeds maximum size of 5MB`
});
}
// Check total upload size
totalSize += file.size;
if (totalSize > MAX_TOTAL_SIZE) {
return res.status(400).json({
success: false,
message: 'Total upload size exceeds maximum of 20MB'
});
}
// Validate file type
const allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'];
if (!allowedTypes.includes(file.mimetype)) {
return res.status(400).json({
success: false,
message: `File ${file.name} has invalid type. Only JPG, PNG, GIF and SVG are allowed`
});
}
const filePath = path.join(uploadDir, file.name);
// Use Sharp to process and save the image
const imageBuffer = file.data;
await sharp(imageBuffer)
.toFile(filePath);
// Get image dimensions using Sharp
try {
const metadata = await sharp(filePath).metadata();
imageResults.push({
imagePath: `/img/${problemName}/${file.name}`,
width: metadata.width,
height: metadata.height
});
} catch (err) {
console.error("Error getting image metadata:", err);
}
}
}
// Write the markdown file
await fs.promises.writeFile(filePath, content);
// Record the creation in contributions table
const userId = req.session.userId || null;
const fullName = req.body.fullName || null;
const clientIp = req.headers["x-forwarded-for"] || req.ip;
await pool.query(
`INSERT INTO contributions (
user_id,
problem_name,
language,
edited_at,
original_content,
new_content,
ip_address,
content_changed,
full_name
) VALUES ($1, $2, $3, NOW(), $4, $5, $6, $7, $8)`,
[userId, problemName, lang, '', content, clientIp, false, fullName]
);
return res.json({
success: true,
message: lang === 'ru' ?
`Задача ${problemName} успешно создана!` :
`Problem ${problemName} created successfully!`,
redirectUrl: `/${lang}/edit/${problemName}`,
images: imageResults
});
} catch (error) {
console.error('Upload error:', error);
const lang = req.body.lang || 'en';
res.status(500).json({
success: false,
message: lang === 'ru' ?
"Не удалось создать файл задачи." :
"Failed to create problem file."
});
}
});
// Authentication middleware (assuming it's defined elsewhere)
function checkAuthenticated(req, res, next) {
const lang = req.query.lang || req.body.lang || 'en';
i18n.setLocale(res, lang);
if (req.session.userId) {
return next();
}
res.redirect(`/${lang}/login?error=${i18n.__('Please log in to access this page')}`);
}
// Upload page routes
router.get(["/upload", "/:lang([a-z]{2})/upload"], async (req, res) => {
const lang = req.params.lang || req.query.lang || 'en';
i18n.setLocale(res, lang);
// Get current user's profile picture if logged in
let profilePictureCurrent = null;
if (req.session.userId) {
const currentUserResult = await pool.query(
"SELECT profile_picture FROM users WHERE id = $1",
[req.session.userId]
);
profilePictureCurrent = currentUserResult.rows[0]?.profile_picture;
}
res.render("upload_page", {
__: i18n.__,
lang,
usernameCurrent: req.session.username,
profilePictureCurrent
});
});
// Add new route for problem verification
router.get("/api/verify-problem/:id", async (req, res) => {
try {
const problemNumber = req.params.id;
const lang = req.query.language || 'en'; // Get the selected language
const otherLang = lang === 'en' ? 'ru' : 'en';
// Check if problem exists in the current language directory
const problemPath = path.join(__dirname, "posts", lang, `${problemNumber}.md`);
const exists = fs.existsSync(problemPath);
// Check if problem exists in the other language directory
const otherLangPath = path.join(__dirname, "posts", otherLang, `${problemNumber}.md`);
const existsInOtherLang = fs.existsSync(otherLangPath);
res.json({ exists, existsInOtherLang });
} catch (error) {
console.error('Problem verification error:', error);
res.status(500).json({ error: 'Failed to verify problem' });
}
});
router.get("/api/validate-limits/:chapter/:section/:problem", async (req, res) => {
try {
const { chapter, section, problem } = req.params;
const lang = req.query.language || 'en';
// Get language data for validation
const { chapters } = await getLanguageData(lang);
// Validate chapter exists
const currentChapter = chapters[chapter - 1];
if (!currentChapter) {
return res.json({
valid: false,
message: lang === 'ru' ?
`Глава ${chapter} не существует.` :
`Chapter ${chapter} does not exist.`
});
}
// Validate section exists
const currentSection = currentChapter.sections[section - 1];
if (!currentSection) {
return res.json({
valid: false,
message: lang === 'ru' ?
`Раздел ${chapter}.${section} не существует.` :
`Section ${chapter}.${section} does not exist.`
});
}
// Validate problem number against section maximum
const maxProblems = currentSection.maximum;
if (parseInt(problem) > parseInt(maxProblems)) {
return res.json({
valid: false,
message: lang === 'ru' ?
`Номер задачи (${problem}) превышает максимально допустимый (${maxProblems}) для раздела ${chapter}.${section}.` :
`Problem number (${problem}) exceeds the maximum allowed (${maxProblems}) for Section ${chapter}.${section}.`
});
}
res.json({ valid: true });
} catch (error) {
console.error('Limits validation error:', error);
const lang = req.query.language || 'en';
res.status(500).json({
valid: false,
message: lang === 'ru' ?
'Не удалось проверить ограничения задачи' :
'Failed to validate problem limits'
});
}
});
module.exports = router;