/**
 * TagLabelController
 *
 * @description :: Server-side logic for managing Journals
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

const path = require('path');
const fs = require('fs');

module.exports = {
    getProfileImage: async function (req, res, next) {
        const username = req.params.username;
        const user = await User.findOne({username});
        const data = await UserData.findOne({researchEntity: user.researchEntity});

        if (!_.has(data, 'profile')) {
            return res.notFound({
                message: 'Profile not found!'
            });
        }

        const profile = data.profile;

        if (_.has(profile, 'hidden.value') && profile.hidden.value) {
            return res.forbidden({
                message: 'This profile is not public!'
            });
        }

        if (_.has(profile, 'image.privacy') && profile.image.privacy !== 'public') {
            return res.forbidden({
                message: 'This image is not public!'
            });
        }

        const image = await UserData.getProfileImage(user, profile);

        if (image) {
            const imagePath = path.join(sails.config.appPath, image);
            const stream = fs.createReadStream(imagePath);
            stream.pipe(res)
        } else {
            return res.notFound({
                message: 'Image not found!'
            });
        }
    }
};
@@ -392,24 +392,194 @@
 username%7D);%0A
+%0A        if (!user %7C%7C (user && !_.has(user, 'researchEntity'))) %7B%0A            return res.notFound(%7B%0A                message: 'User not found!'%0A            %7D);%0A        %7D%0A%0A
         cons

Return 404 not found if user or research entity doesn't exist when getting the profile image