|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Firestore = require('@google-cloud/firestore'); |
| 4 | +const firestore = new Firestore(); |
| 5 | +const uuidv4 = require('uuid/v4'); |
| 6 | + |
| 7 | +const { uploadImage } = require('../image/uploadImage'); |
| 8 | +const { invokeFunction } = require('../../helpers/invokeFunction'); |
| 9 | +const { REGION, PROJECT_ID, BUCKET_NAME, COLLECTION_NAME } = require('../../configuration'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Creates artwork in Firestore |
| 13 | + * |
| 14 | + * @async |
| 15 | + * @function |
| 16 | + * @param {string} artist - The artist's name |
| 17 | + * @param {string} imageUrl - URL to the image |
| 18 | + * @param {string} title - Title of the artwork |
| 19 | + * @param {number} year - Year of the artwork's creation |
| 20 | + * @param {string} createdByUser - The name of the user who created this artwork listing |
| 21 | + * @returns {object} - Returns artwork object |
| 22 | + * @throws {error} - Throws error if it fails to create in Firestore |
| 23 | + */ |
| 24 | +exports.createArtwork = async ({ artist, imageUrl, title, year, createdByUser }) => { |
| 25 | +if (artist && imageUrl && title && year && createdByUser) { |
| 26 | +const UUID = uuidv4(); |
| 27 | + |
| 28 | +const FILE_FORMAT_SPLIT_POINT = imageUrl.lastIndexOf('.'); |
| 29 | +const FILE_FORMAT = imageUrl.slice(FILE_FORMAT_SPLIT_POINT, imageUrl.length); |
| 30 | +const IMAGE_NAME = `${UUID}${FILE_FORMAT}`; |
| 31 | + |
| 32 | +const DOCUMENT = { |
| 33 | +artist, |
| 34 | +originalImageUrl: imageUrl, |
| 35 | +imageUrl: `https://storage.cloud.google.com/${BUCKET_NAME}/${IMAGE_NAME}`, |
| 36 | +title, |
| 37 | +year, |
| 38 | +uuid: UUID, |
| 39 | +labels: [], |
| 40 | +createdByUser |
| 41 | +}; |
| 42 | + |
| 43 | +const upload = await new Promise(async (resolve, reject) => { |
| 44 | +try { |
| 45 | +await uploadImage(imageUrl, IMAGE_NAME, BUCKET_NAME); |
| 46 | +resolve(); |
| 47 | +} catch (error) { |
| 48 | +reject(error); |
| 49 | +} |
| 50 | +}); |
| 51 | + |
| 52 | +const getLabels = await new Promise(async (resolve, reject) => { |
| 53 | +try { |
| 54 | +// Make sure that this exactly matches your own function name from the image labeling service that you should have deployed (https://github.com/mikaelvesavuori/gcp-ml-image-labeling-service) |
| 55 | +const ENDPOINT = `https://${REGION}-${PROJECT_ID}.cloudfunctions.net/getLabels`; |
| 56 | +const labels = await invokeFunction(ENDPOINT, { imageUrl: imageUrl }) |
| 57 | +.then(labels => { |
| 58 | +DOCUMENT.labels = labels; |
| 59 | +console.log('createArtwork ||| Document that will be set in database:', DOCUMENT); |
| 60 | +}) |
| 61 | +.then(res => resolve(res)); |
| 62 | +} catch (error) { |
| 63 | +console.error('createArtwork ||| Failed to get labels from image!', error); |
| 64 | +reject(error); |
| 65 | +} |
| 66 | +}); |
| 67 | + |
| 68 | +const addEntry = await new Promise(async (resolve, reject) => { |
| 69 | +try { |
| 70 | +await firestore |
| 71 | +.doc(`${COLLECTION_NAME}/${UUID}`) |
| 72 | +.set(DOCUMENT) |
| 73 | +.then(() => { |
| 74 | +resolve(); |
| 75 | +}) |
| 76 | +.catch(error => { |
| 77 | +console.error('createArtwork ||| Error creating entry in Firestore!'); |
| 78 | +reject(error); |
| 79 | +}); |
| 80 | +} catch (error) { |
| 81 | +console.error('createArtwork ||| Failed adding entry to Firestore!', error); |
| 82 | +reject(error); |
| 83 | +} |
| 84 | +}); |
| 85 | + |
| 86 | +return await Promise.all([upload, getLabels, addEntry]) |
| 87 | +.then(() => { |
| 88 | +return DOCUMENT; |
| 89 | +}) |
| 90 | +.catch(error => { |
| 91 | +return error; |
| 92 | +}); |
| 93 | +} else { |
| 94 | +const ERROR_MESSAGE = |
| 95 | +'Missing one or more required fields: "artist", "imageUrl", "title", "year", "createdByUser"!'; |
| 96 | +throw new Error(ERROR_MESSAGE); |
| 97 | +} |
| 98 | +}; |
0 commit comments