bozhao:cloudinary

v3.0.5Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Cloudinary Image/File Uploader

This package should be viewed as EXPERIMENTAL.

This is a blaze-only package! Cloudinary provides a simple way for uploading files to Cloudinary, which in turn can be set up to sync with your Amazon S3 service. This is useful for uploading and actively manipulating images and files that you want accesible to the public. Cloudinary is built on Cloudinary (NPM) and Cloudinary (JS). Installing this package will make Cloudinary available server-side and cloudinary available client-side (cloudinary_js feels unstable in certain situations).

Outlook

This is a quick implementation of the uploader, feel free to fork it and improve on it like so many people did for S3 ^_^. I'm always open for pull requests too.

BREAKING CHANGES

{{#cloudinary_upload}} is now {{#c_upload}}

This package does not use Session variables for reactivity anymore. You can identify whether an upload is successful via the new helper c.uploading_images or your own collection.

There is a new global helper that contains all cloudinary helpers, it takes the namespace of "c". This means:

{{c_url}} is now {{c.url}}

NEW FEATURE

You can now upload a stream of data via the c_upload_stream template and view progress via the c.uploading_images helper.

Installation

$ meteor add cramhead:cloudinary

How to upload

Step 1

Configure your Cloudinary Credentials. SERVER SIDE AND CLIENT SIDE.

1//SERVER
2Cloudinary.config({
3	cloud_name: 'cloud_name',
4	api_key: '1237419',
5	api_secret: 'asdf24adsfjk'
6});
7
8//CLIENT
9$.cloudinary.config({
10	cloud_name:"cloud_name"
11});
12

Step 2

Create a Cloudinary input with a callback function. CLIENT SIDE.

{{#c_upload callback="save_url"}}
	<input type="file">
{{/c_upload}}

Or a Cloudinary stream input with a callback function. CLIENT SIDE.

{{#c_upload_stream callback="save_url"}}
	<input type="file">
{{/c_upload_stream}}

{{#each c.uploading_images}}
	<p>{{percent_uploaded}}</p>
{{/each}}

Step 3

Define a callback function that will handle what to do with Cloudinary's response. Usually just save the url to a collection. SERVER SIDE.

1Meteor.methods({
2	save_url:function(response){
3		console.log('Add '+response.upload_data+' to the id of '+response.context);
4	}
5});

How to read and manipulate

All of Cloudinary's manipulation options are available in the c.url helper. You can access an image by passing a cloudinary public_id and format:

<img src="{{c.url public_id format=format}}">

You can manipulate an image by adding parameters to the helper

<img width="250" src="{{c.url public_id format=format effect='blur:300' angle=10}}">

Advanced Usage

You can use the collection-hooks package to hook up to the data stream.

Step 1

Install collection hooks (mrt add collection-hooks)

Step 2

Hook up to cloudinary's client-side collection (_cloudinary).

1_cloudinary.after.update(function(user,file){
2	if(file.percent_uploaded === 100 && !file.uploading){
3		console.log(file);
4	}
5})

Client-side upload

The client-side upload currently uses a preset, configured on Cloudinary such that you can upload with out a secured connection. Parameters are passed to Cloudinary via data attributes. Handlers are automatically attached to the input field of type='file'

{{#c_clientside_upload}}
  <input name="file" type="file" multiple="true" data-meta='role:myRole' data-preset='thePresetName'>
{{/c_clientside_upload}}

On change a copy of the file copied into a client side only collection _cloudinary. The collection has a base64 encoded copy of the file that along with other information such as the progress of upload and a status indicating if the upload is in process or is complete. The file is broken into chunks and sent to Cloudinary and progress is updated with a successfully uploaded chunk.

When the entire file is uploaded the file meta data including the publicId of the file is copied to the uploaded collection, which exists on the server and client. At that point you can safely copy the publicId to another collection and call the uploaded.markLinked() function with the _id uploaded item or call the server with Meteor.call('markUploadDeletedByPublicId', publicId, function(err,result){}

Here are all the transformations you can apply: http://cloudinary.com/documentation/image_transformations#reference

Data of uploading images

Images that are being processed appear under the c.uploading_images helper. This helper only contains percent_uploaded (out of 100) and total_uploaded (in bytes).

{{#each c.uploading_images}}
	<p>{{percent_uploaded}}</p>
	<p>{{total_uploaded}}</p>
{{/each}}

How to delete from Cloudinary

Just pass the public_id of the image or file through this function (security features pending). It will return an object with a list of the images deleted as a result.

1Meteor.call("cloudinary_delete","public_id",function(e,r){
2	if(!e){
3	  	console.log(r);
4	}
5});

Notes

This package is not intrusive on your database. It uses meteor-stream to connect to a clients' local collection and modify it. Because of this it is actually faster and more accurate with data. The local collection uses the _cloudinary namespace (so _cloudinary.find()).

I still need to add more configuration options and better error handling.