Skip to content

Commit

Permalink
Merge pull request #50 from shreyajaiswal17/main
Browse files Browse the repository at this point in the history
added the cloudinary util and updated the readme too
  • Loading branch information
PranavBarthwal authored Oct 28, 2024
2 parents bb4b53f + 6012b84 commit 042e8da
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,40 @@ This command will create a new file `multer-file-upload.js` in the current worki
console.log('Email sent: ' + info.response);
})
```

8. **`cloudinary-util`**
Sets up Cloudinary functionality in a Node.js project

**Code Snippet**:
```js
import { v2 as cloudinary } from 'cloudinary';
import fs from 'fs';
// These values need to be defined in your environment variables (usually in a .env file)
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});

const uploadOnCloudinary = async (localFilePath) => {
try {
if (!localFilePath) return null;

const response = await cloudinary.uploader.upload(localFilePath, {
resource_type: "auto"
});

fs.unlinkSync(localFilePath);
return response;
} catch (error) {
fs.unlinkSync(localFilePath);
return null;
}
};
export { uploadOnCloudinary };

```

### 3. `run generate-ai-snippet <snippetName>`
With the new AI-powered code generation feature, you can generate customized code snippets. For instance, to generate a code snippet for a specific backend functionality, you can run:

Expand Down
29 changes: 29 additions & 0 deletions snippets/cloudinary-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { v2 as cloudinary } from 'cloudinary';
import fs from 'fs';

// Configure Cloudinary
// These values need to be defined in your environment variables (usually in a .env file)
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
});


const uploadOnCloudinary = async (localFilePath) => {
try {
if (!localFilePath) return null;

const response = await cloudinary.uploader.upload(localFilePath, {
resource_type: "auto"
});

fs.unlinkSync(localFilePath); // Remove local file
return response;
} catch (error) {
fs.unlinkSync(localFilePath); // Remove local file if upload fails
return null;
}
};

export { uploadOnCloudinary };

0 comments on commit 042e8da

Please sign in to comment.