Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix: ignore invalid json files #2405 (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiejaoude authored Dec 15, 2022
1 parent fc3fe87 commit 4c3e1d4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
16 changes: 12 additions & 4 deletions pages/api/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ export default async function handler(req, res) {
.readdirSync(directoryPath)
.filter((item) => item.includes("json"));

const users = files.map((file) => ({
...JSON.parse(fs.readFileSync(path.join(directoryPath, file), "utf8")),
username: file.split(".")[0],
}));
const users = files.flatMap((file) => {
const filePath = path.join(directoryPath, file);
try {
return {
...JSON.parse(fs.readFileSync(filePath, "utf8")),
username: file.split(".")[0],
};
} catch (e) {
console.log(`ERROR loading profile "${filePath}"`);
return [];
}
});

const events = users
.filter((user) => user.events)
Expand Down
16 changes: 12 additions & 4 deletions pages/api/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ export default async function handler(req, res) {
.readdirSync(directoryPath)
.filter((item) => item.includes("json"));

const users = files.map((file) => ({
...JSON.parse(fs.readFileSync(path.join(directoryPath, file), "utf8")),
username: file.split(".")[0],
}));
const users = files.flatMap((file) => {
const filePath = path.join(directoryPath, file);
try {
return {
...JSON.parse(fs.readFileSync(filePath, "utf8")),
username: file.split(".")[0],
};
} catch (e) {
console.log(`ERROR loading profile "${filePath}"`);
return [];
}
});
const getStats = await Profile.find({});

// merge profiles with their profile views if set to public
Expand Down
31 changes: 16 additions & 15 deletions pages/api/users/popular.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ export default async function handler(req, res) {
const directoryPath = path.join(process.cwd(), "data");

// merge profiles with their profile views if set to public
const profiles = getProfiles.map((profile) => {
const user = JSON.parse(
fs.readFileSync(
path.join(directoryPath, `${profile.username}.json`),
"utf8"
)
);

if (user.displayStatsPublic) {
return {
...user,
...profile._doc,
};
const profiles = getProfiles.flatMap((profile) => {
const filePath = path.join(directoryPath, `${profile.username}.json`);
try {
const user = JSON.parse(fs.readFileSync(filePath, "utf8"));

if (user.displayStatsPublic) {
return {
...user,
...profile._doc,
};
}

return { ...user, username: profile.username };
} catch (e) {
console.log(`ERROR loading profile "${filePath}"`);
return [];
}

return { ...user, username: profile.username };
});

res.status(200).json(profiles);
Expand Down

0 comments on commit 4c3e1d4

Please sign in to comment.