Skip to content

Commit

Permalink
chore: Fix broken profile images and set default image for users with…
Browse files Browse the repository at this point in the history
…out profile picture
  • Loading branch information
Codycody31 committed Jun 7, 2024
1 parent c4c74e9 commit 1cbc174
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions backend/scripts/fix-broken-profile-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Packages
const User = require("../models/User");
const chalk = require("chalk");

// Load environment variables
require("dotenv").config();

// TODO: Fix hardcoded URL

// Fix broken profile images
const fixBrokenProfileImages = async () => {
// Find all users
const users = await User.find();

// Loop through all users
for (const user of users) {
let needsUpdate = false;

// If the profile image is not found, set it to the default image
if (!user.profilePicture) {
user.profilePicture =
"https://camphouse.vmgware.dev/images/profiles/ProfilePicture.png";
console.log(
chalk.yellow(
"User profile image has been set to the default image for: " +
user.username
)
);
needsUpdate = true;
}

// If it doesn't contain storage.vmgware.dev, set it to the default image
if (
!user.profilePicture.includes("storage.vmgware.dev") &&
!user.profilePicture.includes("camphouse.vmgware.dev")
) {
user.profilePicture =
"https://camphouse.vmgware.dev/images/profiles/ProfilePicture.png";
console.log(
chalk.yellow(
"User profile image has been set to the default image for: " +
user.username
)
);
needsUpdate = true;
}

if (needsUpdate) {
await User.findByIdAndUpdate(user._id, { profilePicture: user.profilePicture });
}
}

console.log(chalk.green("All broken profile images have been fixed"));
};

// Begin the server
(async () => {
console.log(chalk.green("Fixing broken profile images..."));

// Connect to MongoDB
require("../db");

await fixBrokenProfileImages();
process.exit();
})();

0 comments on commit 1cbc174

Please sign in to comment.