mirror of
https://github.com/Ravinou/borgwarehouse
synced 2024-05-05 07:13:15 +02:00
101 lines
3.7 KiB
JavaScript
101 lines
3.7 KiB
JavaScript
// This API is design to be used by a cron (of your choice). Call it with curl for example
|
|
//(e.g : curl --request POST --url 'http://localhost:3000/api/cronjob/checkStatus' --header 'Authorization: Bearer 5173f388c0f4a0df92d1412c3036ddc897c22e4448')
|
|
|
|
//Lib
|
|
import { promises as fs } from 'fs';
|
|
import path from 'path';
|
|
const util = require('node:util');
|
|
const exec = util.promisify(require('node:child_process').exec);
|
|
|
|
export default async function handler(req, res) {
|
|
if (req.headers.authorization == null) {
|
|
res.status(401).json({
|
|
status: 401,
|
|
message: 'Unauthorized',
|
|
});
|
|
return;
|
|
}
|
|
|
|
const CRONJOB_KEY = process.env.CRONJOB_KEY;
|
|
const ACTION_KEY = req.headers.authorization.split(' ')[1];
|
|
|
|
try {
|
|
if (req.method == 'POST' && ACTION_KEY === CRONJOB_KEY) {
|
|
////Call the shell : getStorageUsed.sh
|
|
//Find the absolute path of the shells directory
|
|
const shellsDirectory = path.join(process.cwd(), '/helpers');
|
|
//Exec the shell
|
|
const { stdout, stderr } = await exec(
|
|
`${shellsDirectory}/shells/getLastSave.sh`
|
|
);
|
|
if (stderr) {
|
|
console.log('stderr:', stderr);
|
|
res.status(500).json({
|
|
status: 500,
|
|
message:
|
|
'Error on getting the date for last save, contact the administrator.',
|
|
});
|
|
return;
|
|
}
|
|
//Parse the JSON output of getLastSave.sh to use it
|
|
const lastSave = JSON.parse(stdout);
|
|
|
|
//Find the absolute path of the json directory
|
|
const jsonDirectory = path.join(process.cwd(), '/config');
|
|
let repoList = await fs.readFile(
|
|
jsonDirectory + '/repo.json',
|
|
'utf8'
|
|
);
|
|
//Parse the repoList
|
|
repoList = JSON.parse(repoList);
|
|
|
|
//Rebuild a newRepoList with the lasSave timestamp updated
|
|
const date = Math.round(Date.now() / 1000);
|
|
let newRepoList = repoList;
|
|
for (let index in newRepoList) {
|
|
const repoFiltered = lastSave.filter(
|
|
(x) => x.user === newRepoList[index].unixUser
|
|
);
|
|
if (repoFiltered.length === 1) {
|
|
//Write the timestamp of the last save
|
|
newRepoList[index].lastSave = repoFiltered[0].lastSave;
|
|
//Trigger the status if the last save is older than alert setting.
|
|
if (
|
|
date - newRepoList[index].lastSave >
|
|
newRepoList[index].alert
|
|
) {
|
|
newRepoList[index].status = false;
|
|
} else if (
|
|
date - newRepoList[index].lastSave <
|
|
newRepoList[index].alert
|
|
) {
|
|
newRepoList[index].status = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
//Stringify the repoList to write it into the json file.
|
|
newRepoList = JSON.stringify(newRepoList);
|
|
//Write the new json
|
|
fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => {
|
|
if (err) console.log(err);
|
|
});
|
|
|
|
res.status(200).json({
|
|
success: 'Status cron has been executed.',
|
|
});
|
|
} else {
|
|
res.status(401).json({
|
|
status: 401,
|
|
message: 'Unauthorized',
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
res.status(500).json({
|
|
status: 500,
|
|
message: 'API error, contact the administrator.',
|
|
});
|
|
}
|
|
}
|