UNPKG

@saboosanket/code-generator

Version:

This npm package is a versatile setup tool tailored for Node.js projects, enabling users to generate customized project structures and functionalities. It supports integration with Google Cloud Platform (GCP), RabbitMQ, Redis, a Prisma query generator, an

96 lines (86 loc) 3.4 kB
import { createModuleAndFile, writeFileWithoutPrompt } from '../../utils/write_files.js' import { customErrors } from '../../base_scaffolding/custom_error.js' const gcpUtils = ` import 'dotenv/config' import { Storage } from '@google-cloud/storage' import { GOOGLE_APPLICATION_CREDENTIALS } from '../constants.js' import { UnexpectedError } from '../custom_error.js' const keyFilename = GOOGLE_APPLICATION_CREDENTIALS const storage = new Storage({ keyFilename }) /** * Uploads a file stream to cloud storage. * * @param {string} bucketName - The name of the Google Cloud Storage bucket. * @param {stream.Readable} fileStream - The readable stream of the file. * @param {string} path - Path of the file. * @param {string} mimeType - The MIME type of the file (e.g., image/png, application/pdf). * @returns {string} Returns the path where the file is stored. */ async function uploadFileStream(bucketName, fileStream, path, mimeType) { try { const file = storage.bucket(bucketName).file(path) await new Promise((resolve, reject) => { fileStream.pipe(file.createWriteStream({ metadata: { contentType: mimeType } })) .on('error', reject) .on('finish', resolve) }) } catch (error) { console.error(\`Error uploading \${path}:\`, error) throw UnexpectedError('Error in uploadFileStream', 500,{ message: \`Error uploading uploadFileStream \${path}:\`, data: String(error)}) } return path } /** * Uploads a file to cloud storage using a buffer. * * @param {string} bucketName - The name of the Google Cloud Storage bucket. * @param {string} data - The base64 encoded data of the file. * @param {string} path - Path of the file. * @param {string} mimeType - The MIME type of the file (e.g., image/png, application/pdf). * @returns {string} Returns the path where the file is stored. */ async function uploadFileBuffer(bucketName, data, path, mimeType) { try { const buffer = Buffer.from(data, 'base64') await storage.bucket(bucketName).file(path).save(buffer, { metadata: { contentType: mimeType } }) } catch (error) { console.error(\`Error uploading \${path}:\`, error) throw UnexpectedError('Error in uploadFileBuffer', 500,{ message: \`Error uploading uploadFileBuffer \${path}:\`, data: String(error)}) } return path } async function signedUrl (bucketName, path, days) { try { const expirationInMilliSeconds = Date.now() + (days * 24 * 3600000) // days * hours in day * milliseconds in 1 hr const [signedUrl] = await storage.bucket(bucketName) .file(path) .getSignedUrl({action: 'read', expires: expirationInMilliSeconds}) return signedUrl } catch (error) { console.log('Error in signedUrl', error) throw UnexpectedError('Error in generating signed_url', 500,{ data: String(error)}) } } export { uploadFileStream, uploadFileBuffer, signedUrl } ` async function createGCPFiles() { const folderName = 'services/cloud/gcp' const fileName = 'gcp_utils.services.js' await createModuleAndFile(folderName, fileName, gcpUtils) writeFileWithoutPrompt('services', 'custom_error.js', customErrors) } export { createGCPFiles }