1
0
mirror of https://dev.s-ul.net/Galexion/MaiMaiDXNet.git synced 2024-11-27 22:00:49 +01:00

moved sqlite functions to dbhandler.js in preparation for artemis support

This commit is contained in:
Galexion 2023-07-16 04:06:49 -04:00
parent 70812e0da2
commit 46bd7af3e1
5 changed files with 337 additions and 183 deletions

190
dbhandler.js Normal file
View File

@ -0,0 +1,190 @@
const { serverType, ArtConnSettings,DXMemorialImageDirectory } = require('./config.json');
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database(DXMemorialImageDirectory + '\\db.sqlite');
// index.js route '/'
async function getUserCount() {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM sega_card', (err, rows) => {
if (err) {
console.error(err);
res.render('error', { error: err });
reject(err)
} else {
resolve(rows.length)
}
});
});
} else {
}
}
// index.js & api.js /getUserData/
async function getUserData(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
const cookies = req.cookies
db.all('SELECT * FROM maimai2_user_detail', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// Make the Request easier to get to.
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of rows) { // For Each User in Rows
if (cookies.aime_card_id === user.aime_card_id.toString()) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break;
}
}
if (mUser) {
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
resolve(mUser);
} else {
reject(new Error('User not found'));
}
}
});
}); // function is required to obtain the User Data, and create the user profile. I'm going to bed.
} else { // 07-23 oh god im going to have to recreate this function again aren't i
}
}
// api.js /getExtId/
async function getExtId(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM sega_card', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of rows) { // For Each User in Rows
if (request.input === user.luid) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break
}
}
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
resolve(mUser)
}
});
});
} else {
}
}
// api /getUserScores/
async function getUserScores(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM maimai2_user_playlog', (err, rows) => {
if (err) {
console.error(err);
reject(err)
// Return a Failed Message.
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Scores undefined until the user is found.
for (score of rows) { // For Each Score in Rows
if (request.input == score.user_id) { // If the inputed User ID and Score's User ID Matches,
mUser.push(score) // add that score into the array.
}
}
// Return a Response with all the Scores listed under that user.
resolve(mUser)
}
});
});
} else {
}
}
// api /getUserArea/
async function getUserArea(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM maimai2_user_map', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Areas undefined until the user is found.
for (map of rows) { // For Each Area in Rows
if (request.input == map.user_id) { // If the inputed User ID and Score's User ID Matches,
mUser.push(map) // add that Area into the array.
}
}
// Return a Response with all the Areas listed under that user.
resolve(mUser)
}
});
});
} else {
}
}
/* side note: User Banned & User Unbanned should be tied into 1 commnand that toggles it but maybe not, i dont really know */
// api /getUserBanned/
async function getUserBanned(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
var request = req.body;
if (request.input === undefined) {
return reject('Failed to update user ban state, insufficent paramaters')
return
}
db.run('UPDATE maimai2_user_detail SET ban_state = ? WHERE id = ?', [2, request.input], function (err) {
if (err) {
console.error(err.message);
reject({"status":"failure","message":"Unable to ban user, see reason","reason":err});
} else {
resolve({ "status": "Success", "message": `User ${request.input} banned.` })
}
});
});
} else {
}
}
// api /getUserUnbanned/
async function getUserUnbanned(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
var request = req.body;
if (request.input === undefined) {
return reject('Failed to update user ban state, insufficent paramaters')
return
}
db.run('UPDATE maimai2_user_detail SET ban_state = ? WHERE id = ?', [0, request.input], function (err) {
if (err) {
console.error(err.message);
reject({"status":"failure","message":"Unable to ban user, see reason","reason":err});
} else {
resolve({ "status": "Success", "message": `User ${request.input} banned.` })
}
});
});
} else {
}
}
module.exports = {getUserCount,getUserData,getUserScores,getUserArea,getUserBanned,getUserUnbanned,getExtId};

View File

@ -9,11 +9,12 @@ var indexRouter = require('./routes/index');
var apiRouter = require('./routes/api');
var config = require("./config.json");
const watcher = chokidar.watch(config.DXMemorialImageDirectory, {
if (config.serverType === 0) { // Only run the image Importer when Aqua is active
const watcher = chokidar.watch(config.DXMemorialImageDirectory, {
persistent: true
});
watcher
watcher
.on('add', (filePath) => {
const isImage = /\.(jpe?g|png|gif)$/i.test(filePath);
if (isImage) {
@ -33,16 +34,15 @@ watcher
.on('error', (error) => {
console.error(`Watcher error: ${error}`);
});
}
// readFileSync function must use __dirname get current directory
// readFileSync function must use __dirname get current directory
// require use ./ refer to current directory.
const options = {
};
const options = {};
var app = express()
// Create HTTPs server.
// Create HTTPs server.
//var httpsServer = https.createServer(options, app);
@ -55,19 +55,19 @@ app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.json({ extended: false }));
// define routes
app.use('/', indexRouter);
app.use('/api/', apiRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
app.use(function (req, res, next) {
next(createError(404));
});
var PORT = process.env.PORT || 8000
// error handler
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
@ -77,6 +77,6 @@ app.use(function(err, req, res, next) {
res.render('error');
});
app.listen(PORT, () => console.log(`Server is running in port ${PORT}`));
app.listen(PORT, () => console.log(`Server is running in port ${PORT}, Server type ${config.serverType}`));
module.exports = app;

View File

@ -34,11 +34,53 @@ Create a `config.json` file and paste this in, with paths to your instance.
```json
{
"DXMemorialImageDirectory": "A:\\path\\to\\aqua\\data",
"serverType": 0,
"aquaSrvDir": "A:\\path\\to\\aqua\\data",
"ArtConnSettings": {
"host":"192.168.smt.hng",
"user":"MaiDXNet",
"pass":"aSecurePassword"
},
"imageFolder": "N:\\MaiMaiDXNet\\public\\images"
}
```
`serverType` selects between Aqua (0), and Artemis (1).
Note: you want to enter into the `data` folder in `aquaSrvDir`.
Note: `imageFolder` leads to the `/public/images` folder of the directory.
## Artemis
### WARNING: Artemis currently does not have support for Memorial Photos, and none will be imported when attempting to use Artemis.
Create a `config.json` file and paste this in, with paths to your instance.
```json
{
"serverType": 1,
"aquaSrvDir": "A:\\path\\to\\aqua\\data",
"ArtConnSettings": {
"host":"192.168.smt.hng",
"user":"MaiDXNet",
"pass":"aSecurePassword"
},
"imageFolder": "N:\\MaiMaiDXNet\\public\\images"
}
```
`serverType` selects between Aqua (0), and Artemis (1).
Edit `ArtConnSettings` to be configured for your server.
Modify and use this command string in your terminal to create and set up permissions for The MaiDXNet account.
```sql
CREATE USER 'MaiDXNet'@'%' IDENTIFIED BY 'aSecurePassword';
GRANT Alter,Create,Delete,Drop,Index,Insert,References,Select,Update ON aime.* TO 'MaiDXNet'@'%';
```
# Project Progress
Goal for Milestone 1:

View File

@ -1,126 +1,80 @@
var express = require('express');
var router = express.Router();
var fetch = require('cross-fetch');
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs')
var config = require('../config.json');
let db = new sqlite3.Database(config.DXMemorialImageDirectory + '\\db.sqlite');
var {getExtId,getUserArea,getUserScores,getUserUnbanned,getUserBanned, getUserData} = require("../dbhandler.js");
router.post('/getExtId/', function (req, res, next) {
db.all('SELECT * FROM sega_card', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
res.status(500).json({ error: err.message, status: "Failed" });
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of rows) { // For Each User in Rows
if (request.input === user.luid) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break
}
}
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
getExtId(req).then(
(mUser) => {
res.status(200).json({ data: mUser, status: "Complete" });
}
});
},
(reason) => {
res.status(500).json({ error: err.message, status: "Failed" });
console.error(reason); // Error!
},
);
});
router.post('/getUserData/', function (req,res,next) {
db.all('SELECT * FROM maimai2_user_detail', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
res.status(500).json({ error: err.message, status: "Failed" });
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of rows) { // For Each User in Rows
if (request.input === user.aime_card_id) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break
}
}
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
getUserData(req).then(
(mUser) => {
res.status(200).json({ data: mUser, status: "Complete" });
}
});
},
(reason) => {
res.status(500).json({ error: err.message, status: "Failed" });
console.error(reason); // Error!
},
);
})
router.post('/getUserScores/', function (req,res,next) {
db.all('SELECT * FROM maimai2_user_playlog', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
res.status(500).json({ error: err.message, status: "Failed" });
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Scores undefined until the user is found.
for (score of rows) { // For Each Score in Rows
if (request.input == score.user_id) { // If the inputed User ID and Score's User ID Matches,
mUser.push(score) // add that score into the array.
}
}
// Return a Response with all the Scores listed under that user.
getUserScores(req).then(
(mUser) => {
res.status(200).json({ data: mUser, status: "Complete" });
}
});
},
(reason) => {
res.status(500).json({ error: err.message, status: "Failed" });
console.error(reason); // Error!
},
);
});
router.post('/getUserArea/', function (req,res,next) {
db.all('SELECT * FROM maimai2_user_map', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
res.status(500).json({ error: err.message, status: "Failed" });
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Areas undefined until the user is found.
for (map of rows) { // For Each Area in Rows
if (request.input == map.user_id) { // If the inputed User ID and Score's User ID Matches,
mUser.push(map) // add that Area into the array.
}
}
// Return a Response with all the Areas listed under that user.
getUserArea(req).then(
(mUser) => {
res.status(200).json({ data: mUser, status: "Complete" });
}
});
},
(reason) => {
res.status(500).json({ error: err.message, status: "Failed" });
console.error(reason); // Error!
},
);
});
router.post('/getuserbanned/', function (req,res,next) {
var request = req.body;
if (request.input === undefined) {
return res.status(500).send('Failed to update user ban state, insufficent paramaters');
}
db.run('UPDATE maimai2_user_detail SET ban_state = ? WHERE id = ?', [2, request.input], function(err) {
if (err) {
console.error(err.message);
res.status(500).send('Failed to update user ban state');
} else {
res.send({"status": "Success", "message": `User ${request.input} banned.`});
}
});
getUserBanned(req).then(
(result) => {
res.send(result);
},
(reason) => {
res.status(500).send(reason);
console.error(reason); // Error!
},
);
});
router.post('/getuserunbanned/', function (req,res,next) {
var request = req.body;
if (request.input === undefined) {
return res.status(500).send('Failed to update user ban state, insufficent paramaters');
}
db.run('UPDATE maimai2_user_detail SET ban_state = ? WHERE id = ?', [0, request.input], function(err) {
if (err) {
console.error(err.message);
res.status(500).send('Failed to update user ban state');
} else {
res.send({"status": "Success", "message": `User ${request.input} unbanned.`});
}
});
getUserUnbanned(req).then(
(result) => {
res.send(result);
},
(reason) => {
res.status(500).send(reason);
console.error(reason); // Error!
},
);
});
router.get('/getmemorialimagelist/', function (req,res,next) {

View File

@ -1,29 +1,29 @@
var express = require('express');
var router = express.Router();
var fetch = require('cross-fetch');
const sqlite3 = require('sqlite3').verbose();
var config = require('../config.json');
let db = new sqlite3.Database(config.DXMemorialImageDirectory + '\\db.sqlite');
var { getUserCount, getUserData } = require("../dbhandler.js");
/* GET home page. */
var title = 'MaiDXNet'
router.get('/', function (req, res, next) {
db.all('SELECT * FROM sega_card', (err, rows) => {
if (err) {
console.error(err);
res.render('error', { error: err });
} else {
console.log(rows);
console.log(`There are Currently ${rows.length} Users Registered.`); // this check could probably be changed into an API
var title = 'MaiDXNet';
router.get('/', async function (req, res, next) {
var usersRegistered = await getUserCount().then(
(userCount) => {
var params = {
totalUsers: rows.length
}
totalUsers: userCount // ToDo: Change this to use the internal dbhandler api
};
console.log(`Recived User Count:`+userCount)
res.render('index', { title: title, params: params });
}
});
},
(reason) => {
res.render('error', { title: title, error: reason });
console.error(reason); // Error!
},
);
});
router.get('/user', async function (req, res, next) {
try{
try {
const cookies = req.cookies
console.log(cookies)
if (cookies.aime_card_id === undefined) {
@ -35,36 +35,4 @@ router.get('/user', async function (req, res, next) {
}
});
router.get('/error/test/db', function (req,res,next) {
res.render('error', { error: "Error: SQLITE_BUSY: database is locked"})
})
async function getUserData(req) {
return new Promise((resolve, reject) => {
const cookies = req.cookies
db.all('SELECT * FROM maimai2_user_detail', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// Make the Request easier to get to.
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of rows) { // For Each User in Rows
if (cookies.aime_card_id === user.aime_card_id.toString()) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break;
}
}
if (mUser) {
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
resolve(mUser);
} else {
reject(new Error('User not found'));
}
}
});
});
} // function is required to obtain the User Data, and create the user profile. I'm going to bed.
module.exports = router;