The provided `editSize` function is an asynchronous function that performs...

September 2, 2025 at 04:33 PM

async function editSize(req, res) { const resizeMap = { '300*400': { width: 300, height: 400 }, '700*300': { width: 700, height: 300 }, '500*700': { width: 500, height: 700 }, '700*500': { width: 700, height: 500 }, }; if (!(req.body.size in resizeMap)) { return res.status(400).end('Incorrect data'); } const { width } = resizeMap[req.body.size]; const { height } = resizeMap[req.body.size]; const { userId } = req.session; let avatarInnerFileName; try { avatarInnerFileName = await getAvatarFileName(userId, null); const filename = avatarInnerFileName.avatarFileName; const fileAddress = path.join( __dirname, './../userImages', filename ); const dirPath = imagesFolder; const newFilename = await picFileName(dirPath); const newFilePath = path.join(dirPath, newFilename); await fsPromises.access(fileAddress) .catch((accessError) => { logger.error(accessError); return res.status(404).end('Not found'); }); const resizeRes = await resizeImage(width, height, fileAddress, newFilePath); if (!resizeRes) { return res.status(500).end('Server error'); } await updateUser('avatarFileName', newFilename, userId); const isIntheImageList = await imgExist(userId, null, filename); if (!isIntheImageList) { await deleteFile(path.join(dirPath, filename)); } return res.redirect('/profile'); } catch (error) { logger.error(error); return res.status(500).send('Server error'); } }

The provided editSize function is an asynchronous function that performs several operations to resize and update a user's avatar image based on a specified size. Here's a detailed breakdown of what this code does:

  1. Defines a Size Mapping:

    • A resizeMap object defines specific width and height dimensions corresponding to different size strings (e.g., 300*400, 700*300).
  2. Validates the Requested Size:

    • It checks if the size provided by the client (req.body.size) exists in the resizeMap.
    • If the size is not in the resizeMap, it returns a 400 Bad Request response with a message 'Incorrect data'.
  3. Extracts Dimensions:

    • If the size is valid, the associated width and height values are retrieved from the resizeMap.
  4. Gets the User ID:

    • The user's ID is extracted from the current session (req.session.userId).
  5. Retrieves the Current Avatar:

    • Attempts to get the current avatar file name of the user by calling the getAvatarFileName function.
  6. Locates the File:

    • Constructs the full file path for the user's current avatar image using the avatar file name and the image directory (./../userImages).
  7. Generates a New File Name:

    • Calls the picFileName function to generate a new file name for the resized image and constructs the new file path.
  8. Checks File Existence:

    • Uses fsPromises.access to verify whether the current avatar file exists. If it does not exist, the function logs an error and sends a 404 Not Found response.
  9. Resizes the Image:

    • Calls the resizeImage function, passing the image's dimensions (width, height), the current file path, and the new file path.
    • If the resize operation fails, a 500 Internal Server Error is returned.
  10. Updates the Avatar:

    • Updates the user's avatar file name in the database by calling the updateUser function with the new file name.
  11. Checks Image in the User's Image List:

    • Calls the imgExist function to check whether the user's current avatar image is still in the image list.
    • If it's not in the list, it deletes the old file by calling deleteFile.
  12. Redirects to the Profile:

    • If all the operations succeed, the user is redirected to the /profile page.
  13. Handles Errors:

    • If any error occurs during the process, it is logged and the function responds with a 500 Internal Server Error message.

TL;DR:

This function is an API endpoint for resizing and updating a user's avatar image. It validates the requested size, resizes the existing image, updates the user's profile with the new avatar, and cleans up the old image if necessary.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node