Skip to content

Commit

Permalink
Fix watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Paśnik committed Dec 10, 2023
1 parent 56a8df3 commit bed6d0b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
64 changes: 31 additions & 33 deletions src/arenas.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,35 @@ function loadArenas() {
const arenas = [];
directories.forEach(d => {
const arenaPath = `${dirPath}/${d.name}/${d.name}.json`;
const fileContent = fs.readFileSync(arenaPath, 'utf8');
const obj = JSON.parse(fileContent);

const format = 'YYYY-MM-DD HH:mm:ss.SSSSSS Z';
const dateObject = moment.utc(obj.meta.version_timestamp, format);
const timeAgo = dateObject.fromNow();
const size = getFolderSize(`${dirPath}/${d.name}`);

let short_description = 'N/A';
if (obj.about.short_description) {
short_description = obj.about.short_description;
if (fs.existsSync(arenaPath)) {
const fileContent = fs.readFileSync(arenaPath, 'utf8');
const obj = JSON.parse(fileContent);

const format = 'YYYY-MM-DD HH:mm:ss.SSSSSS Z';
const dateObject = moment.utc(obj.meta.version_timestamp, format);
const timeAgo = dateObject.fromNow();
const size = getFolderSize(`${dirPath}/${d.name}`);

let short_description = 'N/A';
if (obj.about.short_description) {
short_description = obj.about.short_description;
}

let full_description = 'N/A';
if (obj.about.full_description) {
full_description = obj.about.full_description;
}

arenas.push({
name: obj.meta.name,
author: obj.about.author,
short_description: short_description,
full_description: full_description,
version_timestamp: obj.meta.version_timestamp,
updated: timeAgo,
size: size
});
}

let full_description = 'N/A';
if (obj.about.full_description) {
full_description = obj.about.full_description;
}

arenas.push({
name: obj.meta.name,
author: obj.about.author,
short_description: short_description,
full_description: full_description,
version_timestamp: obj.meta.version_timestamp,
updated: timeAgo,
size: size
});
});
console.log(`Loaded ${arenas.length} arenas successfully`);
return arenas;
Expand All @@ -72,18 +74,14 @@ module.exports = {
arenas = loadArenas();

const watcher = chokidar.watch(`${dirPath}/**/*.json`, {
ignoreInitial: true,
persistent: true,
recursive: true,
ignored: '**/editor_view.json'
});

watcher.on('change', (filePath) => {
console.log(`File ${filePath} has been changed`);
arenas = loadArenas();
});

watcher.on('unlink', (filePath) => {
console.log(`File ${filePath} has been removed`);
watcher.on('all', (event, path) => {
console.log(event, path);
arenas = loadArenas();
});

Expand Down
22 changes: 8 additions & 14 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ function ensureAuthenticated(req, res, next) {
res.redirect('/auth/steam');
}

function ensurePathExists(filePath) {
const directories = filePath.split(path.sep);
let currentPath = '';
for (const directory of directories) {
currentPath = path.join(currentPath, directory);
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
function writeFileWithDirectory(filePath, content) {
const directory = path.dirname(filePath);
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
fs.writeFileSync(filePath, content);
}

module.exports = function (app, passport) {
Expand Down Expand Up @@ -210,12 +207,9 @@ module.exports = function (app, passport) {
}
}

const fileName = req.file.originalname;
const fileContent = req.file.buffer;
const filePath = __dirname + `/../public/arenas/${arena}/${filename}`;
ensurePathExists(filePath);
fs.writeFileSync(filePath, fileContent);
console.log('File saved to:', filePath);
const filePath = `public/arenas/${arena}/${sanitizedFilename}`;
writeFileWithDirectory(filePath, req.file.buffer);
console.log(`File saved to ${filePath}`);

return res.json({
success: 'The file has been uploaded'
Expand Down

0 comments on commit bed6d0b

Please sign in to comment.