Skip to content

Instantly share code, notes, and snippets.

@rubenCodeforges
Created March 4, 2016 15:11
Show Gist options
  • Save rubenCodeforges/9cafcad13d69c92dca2d to your computer and use it in GitHub Desktop.
Save rubenCodeforges/9cafcad13d69c92dca2d to your computer and use it in GitHub Desktop.
@App.factory 'MediaHelper', ['mytvConfig','$q','$upload', (mytvConfig,$q,$upload)->
helper =
settings:
maxFileSize: 1300
uploadVideo: (files)->
deferred = $q.defer()
files.forEach (file)=>
if file.size/1000000 < @settings.maxFileSize
data =
api_password: mytvConfig.wistia.id
project_id: mytvConfig.me.remoteProjectId
file: file
$upload.upload(
url: 'https://upload.wistia.com',
method: 'POST',
data: data
)
.progress (obj) ->
progress = Math.round( obj.loaded / obj.totalSize * 100 )
deferred.notify progress
.success (response) ->
deferred.resolve response
else
deferred.reject 'File: "'+file.name+'", is too big.'
deferred.promise
uploadMedia: (position, files)->
deferred = $q.defer()
notification =
inProgress: {}
files.forEach (file) ->
notification.inProgress[file.name] = true
deferred.notify notification
data =
'media_item[file]': file
'media_item[media_name]': file.name
'media_item[position]': position
'media_item[url]': ''
'media_item[visible]': true
$upload.upload({
url: '/api/v2/media_items/',
method: 'POST',
data: data }
)
.success (response) ->
notification.inProgress[file.name] = false
deferred.notify notification
if response.success == false
deferred.reject response.messages[0].text
else
data:
id :response.id
position:files.length + 1
thumb:response.thumb
message: 'Media Items has been successfully uploaded!'
deferred.resolve data
deferred.promise
helper
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment