2013-07-25 23:33:04 +02:00
|
|
|
angular.module('ponyfm').factory('upload', [
|
|
|
|
'$rootScope'
|
|
|
|
($rootScope) ->
|
|
|
|
self =
|
|
|
|
queue: []
|
|
|
|
|
|
|
|
upload: (files) ->
|
|
|
|
_.each files, (file) ->
|
|
|
|
upload =
|
|
|
|
name: file.name
|
|
|
|
progress: 0
|
|
|
|
uploadedSize: 0
|
|
|
|
size: file.size
|
|
|
|
index: self.queue.length
|
|
|
|
isUploading: true
|
|
|
|
success: false
|
|
|
|
error: null
|
|
|
|
|
|
|
|
self.queue.push upload
|
|
|
|
$rootScope.$broadcast 'upload-added', upload
|
|
|
|
|
|
|
|
xhr = new XMLHttpRequest()
|
|
|
|
xhr.upload.onprogress = (e) ->
|
|
|
|
$rootScope.$apply ->
|
|
|
|
upload.uploadedSize = e.loaded
|
|
|
|
upload.progress = e.loaded / upload.size * 100
|
|
|
|
$rootScope.$broadcast 'upload-progress', upload
|
|
|
|
|
|
|
|
xhr.onload = -> $rootScope.$apply ->
|
|
|
|
upload.isUploading = false
|
|
|
|
if xhr.status != 200
|
|
|
|
error =
|
|
|
|
if xhr.getResponseHeader('content-type') == 'application/json'
|
2013-09-01 11:17:53 +02:00
|
|
|
$.parseJSON(xhr.responseText).errors.track.join ', '
|
2013-07-25 23:33:04 +02:00
|
|
|
else
|
|
|
|
'There was an unknown error!'
|
|
|
|
|
|
|
|
upload.error = error
|
|
|
|
$rootScope.$broadcast 'upload-error', [upload, error]
|
|
|
|
else
|
|
|
|
upload.success = true
|
2013-08-31 03:46:35 +02:00
|
|
|
upload.trackId = $.parseJSON(xhr.responseText).id
|
2013-07-25 23:33:04 +02:00
|
|
|
|
|
|
|
$rootScope.$broadcast 'upload-finished', upload
|
|
|
|
formData = new FormData();
|
|
|
|
formData.append('track', file);
|
|
|
|
|
|
|
|
xhr.open 'POST', '/api/web/tracks/upload', true
|
|
|
|
xhr.setRequestHeader 'X-Token', pfm.token
|
|
|
|
xhr.send formData
|
|
|
|
])
|