Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added multiple image formats but async await promise not working #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions backend/populate_details.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Go recursively Deep in each directory and create a json file called details.json in each directory
# sample details.json
# {
# "name": "{current subject directory name}",
# "description": "{current subject directory description}",
# "price": "{current subject directory price}",
# }

#!/bin/sh
function find_dirs_without_details_json() {
find . -type d -name "details.json" -prune -o -type d -print
}

function recursive_json_file_creation() {
for dir in $(find_dirs_without_details_json); do
echo "Creating json file in $dir"
echo "{
\"name\": \"$(basename $dir)\",
\"description\": \"$(basename $dir)\",
\"price\": \"$(basename $dir)\"
}" > $dir/details.json
done
}

recursive_json_file_creation
54 changes: 45 additions & 9 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,56 @@ app.get('/', async (req, res, next) => {
});

// Function to add poster url to each subdomain from cloudinary
const addSubdomainPosterUrl = (domain, subdomains) => {
const addSubdomainPosterUrl = async (domain, subdomains) => {

// Create a clone of subdomains
const subdomainsClone = JSON.parse(JSON.stringify(subdomains));

// Correct way to add JSON fields to an object
// // Correct way to add JSON fields to an object
// subdomainsClone.folders.map((subdomain) => {
// cloudinary.search
// // format can be 'jpg', 'png', 'gif', 'webp'
// .expression(`folder:noteups/${domain}/${subdomain.name} AND resource_type:image AND (format:jpg OR format:png OR format:gif OR format:webp)`)
// .execute()
// .then((result) => {
// // Check if result is not undefined
// if (result && result.resources && result.resources.length > 0) {
// subdomain.poster = result.resources[0].secure_url;
// } else {
// subdomain.poster = "https://files.prokerala.com/movies/assets/img/no-poster-available.jpg";
// }
// console.log(`Subdomain ${subdomain.name} poster url: ${subdomain.poster}`);
// }).catch((err) => {
// console.log(`Error: ${err}`);
// });

// });

// Do the above code in Promise.all
await Promise.all(subdomainsClone.folders.map((subdomain) => {
return new Promise((resolve, reject) => {
cloudinary.search
// format can be 'jpg', 'png', 'gif', 'webp'
.expression(`folder:noteups/${domain}/${subdomain.name} AND resource_type:image AND (format:jpg OR format:png OR format:gif OR format:webp)`)
.execute()
.then((result) => {
// Check if result is not undefined
if (result && result.resources && result.resources.length > 0) {
subdomain.poster = result.resources[0].secure_url;
} else {
subdomain.poster = "https://files.prokerala.com/movies/assets/img/no-poster-available.jpg";
}
console.log(`Subdomain has1 ${subdomain.name} poster url: ${subdomain.poster}`);
resolve(subdomain);
}).catch((err) => {
console.log(`Error: ${err}`);
reject(err);
});
});
}));

subdomainsClone.folders.map((subdomain) => {
subdomain.poster = cloudinary.url(`noteups/${domain}/${subdomain.name}/poster.jpg`,
// {
// width: 250,
// height: 350,
// crop: 'fill',
// }
);
console.log(`Subdomain has2 ${subdomain.name} poster url: ${subdomain.poster}`);
});

return subdomainsClone;
Expand Down
1 change: 1 addition & 0 deletions frontend/src/redux/actions/ProductActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const detailsSubjectActionCreator = (domain, subdomain, subject) => async
dispatch({ type: SUBJECT_DETAILS_REQUEST, payload: { domain, subdomain, subject } });

try {
// regex not necessary for cloudinary
const { data } = await axios.get(`/api/${domain}/${subdomain}/${subject}/details.json`);
dispatch({ type: SUBJECT_DETAILS_SUCCESS, payload: data });
} catch (error) {
Expand Down