Skip to content

Instantly share code, notes, and snippets.

@andresmatasuarez
Last active August 29, 2015 14:25
Show Gist options
  • Save andresmatasuarez/0eef97ce5f98bb2b258e to your computer and use it in GitHub Desktop.
Save andresmatasuarez/0eef97ce5f98bb2b258e to your computer and use it in GitHub Desktop.
AngularJS | Module | CloudinaryFileUploader
###
CloudinaryFileUploader
--------------------------
REQUIREMENTS
ngFileUpload: https://github.com/danialfarid/ng-file-upload
USAGE
1. Include as your AngularJS app dependencies
angular.module('yourApp', [ 'cloudinary-file-uploader' ])
2. Inject it into your code
angular.controller('YourAppController', function(CloudinaryFileUploader){ ... })
3. Upload your files!
CloudinaryFileUploader.upload(yourFile, [ opts ])
Note: CloudinaryFileUploader.upload returns a ngFileUpload object.
More info on how to treat this kind of objects here: https://github.com/danialfarid/ng-file-upload
Configuration:
1. You can globally set default values for all the uploads through CloudinaryFileUploaderProvider.
Example:
CloudinaryFileUploaderProvider.uploadPreset = 'defdefdef';
2. You can configure each upload separately by passing an options object on each call to CloudinaryFileUploader.upload.
Example:
CloudinaryFileUploader.upload(file, { uploadPreset: 'asdasdasd' })
###
'use strict'
angular
.module 'cloudinary-file-uploader', [ 'ngFileUpload' ]
.provider 'CloudinaryFileUploader', ->
this.cloudName = undefined
this.uploadUrl = undefined
this.uploadPreset = undefined
this.defaultTags = undefined
config = this
getUploadUrl = (opts) ->
return opts.uploadUrl if opts and opts.uploadUrl
return "https://api.cloudinary.com/v1_1/#{ opts.cloudName }/upload" if opts and opts.cloudName
return config.uploadUrl if config.uploadUrl
return "https://api.cloudinary.com/v1_1/#{ config.cloudName }/upload" if config.cloudName
getUploadPreset = (opts) -> if opts and opts.uploadPreset then opts.uploadPreset else config.uploadPreset
getDefaultTags = (opts) -> if opts and opts.defaultTags then opts.defaultTags else config.defaultTags
this.$get = (Upload) ->
upload: (file, opts) ->
Upload.upload
url : getUploadUrl opts
file : file
sendFieldsAs : 'form'
fields:
upload_preset : getUploadPreset opts
default_tags : getDefaultTags opts
# Added empty return statement due to
# '[...] must define $get factory method.'
return
/**
* CloudinaryFileUploader
* --------------------------
* REQUIREMENTS
* ngFileUpload: https://github.com/danialfarid/ng-file-upload
*
* USAGE
* 1. Include as your AngularJS app dependencies
* angular.module('yourApp', [ 'cloudinary-file-uploader' ])
*
* 2. Inject it into your code
* angular.controller('YourAppController', function(CloudinaryFileUploader){ ... })
*
* 3. Upload your files!
* CloudinaryFileUploader.upload(yourFile, [ opts ])
*
* Note: CloudinaryFileUploader.upload returns a ngFileUpload object.
* More info on how to treat this kind of objects here: https://github.com/danialfarid/ng-file-upload
*
* CONFIGURATION
* 1. You can globally set default values for all the uploads through CloudinaryFileUploaderProvider.
* Example:
* CloudinaryFileUploaderProvider.uploadPreset = 'defdefdef';
*
* 2. You can configure each upload separately by passing an options object on each call to CloudinaryFileUploader.upload.
* Example:
* CloudinaryFileUploader.upload(file, { uploadPreset: 'asdasdasd' })
*
*/
'use strict';
angular
.module('cloudinary-file-uploader', [ 'ngFileUpload' ])
.provider('CloudinaryFileUploader', function(){
this.cloudName = undefined;
this.uploadUrl = undefined;
this.uploadPreset = undefined;
this.defaultTags = undefined;
var config = this;
var getUploadUrl = function(opts){
if (opts && opts.uploadUrl){
return opts.uploadUrl;
}
if (opts && opts.cloudName){
return "https://api.cloudinary.com/v1_1/#{ opts.cloudName }/upload";
}
if (config.uploadUrl){
return config.uploadUrl;
}
if (config.cloudName){
return "https://api.cloudinary.com/v1_1/#{ config.cloudName }/upload";
}
};
var getUploadPreset = function(opts) {
return opts && opts.uploadPreset ? opts.uploadPreset : config.uploadPreset;
};
var getDefaultTags = function(opts) {
return opts && opts.defaultTags ? opts.defaultTags : config.defaultTags;
};
this.$get = function(Upload){
return {
upload: function(file, opts){
return Upload.upload({
url : getUploadUrl(opts),
file : file,
sendFieldsAs : 'form',
fields: {
upload_preset : getUploadPreset(opts),
default_tags : getDefaultTags(opts)
}
});
}
};
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment