generated from cotes2020/chirpy-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-no-push.js
108 lines (86 loc) · 3.58 KB
/
server-no-push.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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const multer = require('multer'); // For handling file uploads
const app = express();
const port = 3000;
// Middleware to parse form data
app.use(bodyParser.urlencoded({ extended: true }));
// Serve static files
app.use(express.static('public'));
// Configure Multer for image uploads
const uploadFolder = path.join(__dirname, '_uploads');
if (!fs.existsSync(uploadFolder)) {
fs.mkdirSync(uploadFolder);
}
const upload = multer({ dest: uploadFolder });
// Handle form submission
app.post('/submit', upload.single('image'), (req, res) => {
const { title, layout, categories, comments, message, altText } = req.body;
const image = req.file;
// Get the current date and time
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
// Join multiple selected categories into a comma-separated string
const categoryList = Array.isArray(categories) ? categories.join(', ') : categories;
// Generate image reference if an image is uploaded
let imageReference = '';
if (image) {
const imageExtension = path.extname(image.originalname);
const newImageName = `${Date.now()}${imageExtension}`;
const imagePath = path.join(uploadFolder, newImageName);
// Rename and move the uploaded file
fs.renameSync(image.path, imagePath);
// Add image reference
imageReference = `<center><a href="https://raw.githubusercontent.com/Nox13last/nox13last.github.io/refs/heads/main/_uploads/${newImageName}"><img src="https://raw.githubusercontent.com/Nox13last/nox13last.github.io/refs/heads/main/_uploads/${newImageName}" width="600"></a></center>\n`;
}
// Format the content for the text file
const content = `---
title: "${title}"
layout: ${layout}
date: ${formattedDate}
categories: ${categoryList}
comments: ${comments}
---
${message}
${imageReference}
`;
// Sanitize the title to create a safe filename
const sanitizedTitle = title.replace(/[^a-zA-Z0-9_-]/g, '_');
const filename = `./_posts/${year}-${month}-${day}-${sanitizedTitle}.markdown`;
// Write content to the dynamically named file
const { exec } = require('child_process');
fs.writeFile(path.join(__dirname, filename), content, (err) => {
if (err) {
console.error('Error writing to file', err);
return res.status(500).send('Internal Server Error');
}
res.send(`File "${filename}" generated successfully!`);
// Wait for 3 seconds before running Git commands
setTimeout(() => {
const command = `cd ${__dirname} && git add . && git commit -m "Posted via form: ${filename}"`;
exec(command, (error, stdout, stderr) => {
console.log("Command executed: ", command); // Log the exact command being run
if (error) {
console.error('Error executing command:', error.message);
console.error('Error details:', error);
return;
}
console.log('Standard output:', stdout);
if (stderr) {
console.error('Standard error:', stderr);
}
});
}, 3000);
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});