Skip to content

Instantly share code, notes, and snippets.

@zo0m
Last active November 13, 2015 21:54
Show Gist options
  • Save zo0m/1fb47628560054ac398d to your computer and use it in GitHub Desktop.
Save zo0m/1fb47628560054ac398d to your computer and use it in GitHub Desktop.
Upload Photo to ACS (Appcelerator Cloud Services) Angular Service ( POST file via $resource)
class PhotoService
constructor : ($resource, ACS_API_KEY, Session, $q)->
@instance = $resource 'https://api.cloud.appcelerator.com/v1/photos/:methodName'
,
key: ACS_API_KEY
_session_id: ->
Session.sessionId
,
create:
method: 'POST'
headers :
"Content-Type" : undefined
transformRequest: (data)->
formData = new FormData();
for key, value of data
formData.append key, value
formData
params:
methodName: 'create.json'
count:
method: 'GET'
params:
methodName: 'count.json'
query:
method: 'GET'
params:
methodName: 'query.json'
isArray: true
show:
method: 'GET'
params:
methodName: 'show.json'
isArray: true
update:
method: 'PUT'
params:
methodName: 'update.json'
delete:
method: 'DELETE'
params:
methodName: 'delete.json'
createPhoto : (photo, options) =>
console.log "create Photo #{photo}"
options or=
"photo_sizes[preview]" : "100x100#"
"photo_sizes[afisha_small]" : "150x400"
"photo_sizes[afisha_big]" : "300x800"
"photo_sync_sizes[]" : "afisha_small"
"photo_sync_sizes[]" : "preview"
"photo_sync_sizes[]" : "afisha_small"
"photo_sync_sizes[]" : "preview"
requestObject =
photo: photo
user_id: Session.userId
for key, value of options
requestObject[key] = value
@instance.create(requestObject).$promise
.then (response) ->
console.log 'created Photo ' + JSON.stringify(response?.response?.photos?[0])
response?.response?.photos?[0] or null
.catch (error)->
console.log "Create Photo ERROR : #{JSON.stringify error}"
angular.module('ngapp').service 'PhotoService', PhotoService
PhotoService.$inject = [
'$resource'
'ACS_API_KEY'
'Session'
'$q'
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment