gatsby-source-cloudinary
Advanced tools
Comparing version
@@ -1,57 +0,45 @@ | ||
const cloudinary = require('cloudinary') | ||
const {newCloudinary, getResourceOptions} = require('./utils'); | ||
const type = `CloudinaryMedia`; | ||
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => { | ||
const { createNode } = actions | ||
delete configOptions.plugins | ||
// Configure Cloudinary | ||
cloudinary.config({ | ||
cloud_name: configOptions.cloudName, | ||
api_key: configOptions.apiKey, | ||
api_secret: configOptions.apiSecret | ||
}) | ||
const getNodeData = (gatsby, media) => { | ||
return { | ||
...media, | ||
id: gatsby.createNodeId(`cloudinary-media-${media.public_id}`), | ||
parent: null, | ||
internal: { | ||
type, | ||
content: JSON.stringify(media), | ||
contentDigest: gatsby.createContentDigest(media) | ||
} | ||
}; | ||
}; | ||
const processMedia = media => { | ||
const nodeId = createNodeId(`cloudinary-media-${media.public_id}`) | ||
const nodeContent = JSON.stringify(media) | ||
const createCloudinaryNodes = (gatsby, cloudinary, options) => { | ||
return cloudinary.api.resources(options, (error, result) => { | ||
const hasResources = (result && result.resources && result.resources.length); | ||
const nodeData = Object.assign({}, media, { | ||
id: nodeId, | ||
parent: null, | ||
children: [], | ||
internal: { | ||
type: `CloudinaryMedia`, | ||
content: nodeContent, | ||
contentDigest: createContentDigest(media), | ||
}, | ||
}) | ||
if (error) { | ||
console.error(error); | ||
return; | ||
} | ||
return nodeData | ||
if (!hasResources) { | ||
console.warn('\n ~Yikes! No nodes created because no Cloudinary resources found. Try a different query?'); | ||
return; | ||
} | ||
const {resourceType, prefix, tags, maxResults, type} = configOptions | ||
const queryParams = new Object; | ||
result.resources.forEach(resource => { | ||
const nodeData = getNodeData(gatsby, resource); | ||
gatsby.actions.createNode(nodeData); | ||
}); | ||
if(!!resourceType){queryParams.resource_type = resourceType} | ||
if(!!tags){queryParams.tags = tags} | ||
if(!!maxResults){queryParams.max_results = maxResults} | ||
if(!!type){queryParams.type = type} | ||
if(!!prefix && !!type){queryParams.prefix = prefix} | ||
console.info(`Added ${hasResources} CloudinaryMedia ${hasResources > 1 ? 'nodes' : 'node'}`); | ||
}); | ||
}; | ||
return ( | ||
cloudinary.v2.api.resources(queryParams,(error, result)=>{ | ||
if(result.resources.length > 0){ | ||
result.resources.forEach((mediaItem)=>{ | ||
const nodeData = processMedia(mediaItem) | ||
createNode(nodeData) | ||
console.log("Created Cloudinary file node") | ||
}) | ||
}else{ | ||
console.log('\n ~Yikes! No Cloudinary files found and nodes not created. Try a better query.') | ||
} | ||
if(error){console.log(error)} | ||
}) | ||
) | ||
} | ||
exports.sourceNodes = (gatsby, options) => { | ||
const cloudinary = newCloudinary(options); | ||
const resourceOptions = getResourceOptions(options); | ||
return createCloudinaryNodes(gatsby, cloudinary, resourceOptions); | ||
}; |
{ | ||
"name": "gatsby-source-cloudinary", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "Gatsby source plugin to fetch files from Cloudinary into Gatsby.", | ||
@@ -19,4 +19,5 @@ "main": "gatsby-node.js", | ||
"dependencies": { | ||
"cloudinary": "^1.11.0" | ||
"cloudinary": "^1.11.0", | ||
"lodash.snakecase": "^4.1.1" | ||
} | ||
} |
# Gatsby-Source-Cloudinary | ||
This plugin is still in development, however you can try it out to query media files (videos) from your cloudinary account. | ||
This plugin is used to query media files from your cloudinary account into file nodes in your Gatsby project. | ||
## Cloudinary Credentials | ||
Obtain your cloudname, key and secret from your cloudinary console when you signup at [Cloudinary.com](https://cloudinary.com) | ||
Store your `cloudName`, `apiKey` and `apiSecret` as environment variables for security. | ||
To do this, create a file in the root of the project named `.env`. Add your environment variables in it with: | ||
``` | ||
CLOUDINARY_API_KEY=INSERT API KEY HERE | ||
CLOUDINARY_API_SECRET=INSERT API SECRET HERE | ||
CLOUDINARY_CLOUD_NAME=INSERT CLOUDNAME HERE | ||
``` | ||
Install `dotenv` in your project with: | ||
``` | ||
yarn add dotenv | ||
``` | ||
In your `gatsby-config.js` file, require and configure `dotenv` with: | ||
``` | ||
require('dotenv').config(); | ||
``` | ||
There are several options to configuring `dotenv` to use different env files either in development or production. You can find that [here](https://www.npmjs.com/package/dotenv). | ||
Add the `.env` file to `.gitignore` so it's not committed. | ||
Ensure to configure the environment variables on deployment as well. | ||
## Installation | ||
To use, in your Gatsby project run: | ||
@@ -17,6 +48,6 @@ | ||
options:{ | ||
cloudName: 'xxxx', | ||
apiKey: 'xxxxxxxxxxxxxx', | ||
apiSecret: 'xxxxxxxxxxxxx', | ||
resourceType: 'image', | ||
cloudName: process.env.CLOUDINARY_CLOUD_NAME, | ||
apiKey: process.env.CLOUDINARY_API_KEY, | ||
apiSecret: process.env.CLOUDINARY_API_SECRET, | ||
resourceType: `image`, | ||
type: `type Value`, | ||
@@ -32,4 +63,6 @@ maxResults: `Max result`, | ||
Here are details of each query parameter as culled from cloudinary.com. | ||
## Query Parameters | ||
Here are details of each query parameter as culled from [Cloudinary.com](https://cloudinary.com). | ||
* `resourceType` - Optional (String, default: image). The type of file. Possible values: image, raw, video. Relevant as a parameter only when using the SDKs (the resource type is included in the endpoint URL for direct calls to the HTTP API). Note: Use the video resource type for all video resources as well as for audio files, such as .mp3. | ||
@@ -41,8 +74,6 @@ * `type` - Optional (String, default: all). The storage type: upload, private, authenticated, facebook, twitter, gplus, instagram_name, gravatar, youtube, hulu, vimeo, animoto, worldstarhiphop or dailymotion. Relevant as a parameter only when using the SDKs (the type is included in the endpoint URL for direct calls to the HTTP API). | ||
> With `prefix`, you can source only media files from a specific folder. However, you will need to specify `type` and `resourceType` in the config options. | ||
> With `prefix`, source only media files from a specific folder. However, you will need to specify `type` and `resourceType` in the config options. | ||
An example `prefix` value will is `gatsby-anime-videos/`. This will fetch only media files with public ids beginning with `gatsby-anime-videos/*`. Example: `gatsby-anime-videos/naruto.mp4` | ||
An example `prefix` value is `gatsby-anime-videos/`. This will fetch only media files with public ids beginning with `gatsby-anime-videos/*`. Example: `gatsby-anime-videos/naruto.mp4` | ||
Obtain your cloudname, key and secret from your cloudinary console when you signup at [Cloudinary.com](https://cloudinary.com) | ||
Feel free to create feature requests.... and PRs :) | ||
Feel free to create feature requests.... and PRs :) |
5916
24.65%4
33.33%63
36.96%77
71.11%2
100%+ Added
+ Added