+ Test
Added a test programm to upload images for characters
This commit is contained in:
+38
-4
@@ -4,6 +4,9 @@ const bcrypt = require('bcrypt');
|
||||
const cors = require('cors');
|
||||
const bodyParser = require('body-parser');
|
||||
const crypto = require('crypto');
|
||||
const multer = require('multer'); // Add this to your dependencies
|
||||
const storage = multer.memoryStorage();
|
||||
const upload = multer({ storage: storage });
|
||||
|
||||
const app = express();
|
||||
const port = 5000;
|
||||
@@ -195,9 +198,19 @@ app.get('/games/:userId/characters', (req, res) => {
|
||||
const userId = req.params.userId;
|
||||
db.all('SELECT * FROM PlayerCharacter WHERE PlayerId = ?', [userId], (err, rows) => {
|
||||
if (err) {
|
||||
console.error('Database error:', err);
|
||||
return res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
res.json(rows);
|
||||
|
||||
// Process each row and convert BLOB to base64 if image exists
|
||||
const processedRows = rows.map(row => {
|
||||
if (row.Img) {
|
||||
row.Img = `data:image/jpeg;base64,${row.Img.toString('base64')}`;
|
||||
}
|
||||
return row;
|
||||
});
|
||||
|
||||
res.json(processedRows);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -208,18 +221,39 @@ app.get('/games/:gameId/:userId/character', (req, res) => {
|
||||
console.log(`Fetching character for gameId: ${gameId}, userId: ${userId}`); // Debug output
|
||||
db.get('SELECT * FROM PlayerCharacter WHERE GameID = ? AND PlayerID = ?', [gameId, userId], (err, row) => {
|
||||
if (err) {
|
||||
console.error('Database error:', err); // Debug output
|
||||
console.error('Database error:', err);
|
||||
return res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
if (!row) {
|
||||
console.log('No character found'); // Debug output
|
||||
return res.status(404).json({ error: 'No character found' });
|
||||
}
|
||||
console.log('Character found:', row); // Debug output
|
||||
|
||||
// Convert BLOB to base64 string if image exists
|
||||
if (row.Img) {
|
||||
row.Img = `data:image/jpeg;base64,${row.Img.toString('base64')}`;
|
||||
}
|
||||
|
||||
res.json(row);
|
||||
});
|
||||
});
|
||||
|
||||
// Update character image
|
||||
app.put('/games/:gameId/:userId/character/image', upload.single('image'), (req, res) => {
|
||||
const gameId = req.params.gameId;
|
||||
const userId = req.params.userId;
|
||||
const imageBuffer = req.file.buffer;
|
||||
|
||||
const stmt = db.prepare('UPDATE PlayerCharacter SET Img = ? WHERE GameID = ? AND PlayerID = ?');
|
||||
stmt.run([imageBuffer, gameId, userId], function(err) {
|
||||
if (err) {
|
||||
console.error('Database error:', err);
|
||||
return res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
res.json({ message: 'Image updated successfully!' });
|
||||
});
|
||||
stmt.finalize();
|
||||
});
|
||||
|
||||
// Update Player Character Description
|
||||
app.put('/games/:gameId/:userId/character', (req, res) => {
|
||||
const gameId = req.params.gameId;
|
||||
|
||||
Reference in New Issue
Block a user