From 2f9a66b5d4966d4265f9685cb165ee91edcb6e20 Mon Sep 17 00:00:00 2001 From: Kaustav Ghosh Date: Sun, 1 May 2022 16:01:18 +0530 Subject: [PATCH] added multiple image formats but async await promise not working --- backend/populate_details.sh | 25 +++++++++ backend/server.js | 54 ++++++++++++++++---- frontend/src/redux/actions/ProductActions.js | 1 + 3 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 backend/populate_details.sh diff --git a/backend/populate_details.sh b/backend/populate_details.sh new file mode 100644 index 0000000..fde6498 --- /dev/null +++ b/backend/populate_details.sh @@ -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 \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index f106ddd..d8d5a1f 100644 --- a/backend/server.js +++ b/backend/server.js @@ -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; diff --git a/frontend/src/redux/actions/ProductActions.js b/frontend/src/redux/actions/ProductActions.js index d34f984..7b35a9e 100644 --- a/frontend/src/redux/actions/ProductActions.js +++ b/frontend/src/redux/actions/ProductActions.js @@ -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) {