2015-10-25 06:17:45 +01:00
|
|
|
# Pony.fm - A community for pony fan music.
|
|
|
|
# Copyright (C) 2015 Peter Deltchev
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-08-31 17:42:21 +02:00
|
|
|
angular.module('ponyfm').factory('upload', [
|
2016-02-15 20:05:58 +01:00
|
|
|
'$rootScope', '$http', '$timeout', 'account-tracks'
|
|
|
|
($rootScope, $http, $timeout, accountTracks) ->
|
2015-10-25 03:35:37 +01:00
|
|
|
self =
|
|
|
|
queue: []
|
2015-08-31 17:42:21 +02:00
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
finishUploadWrapper: (upload)->
|
|
|
|
()->
|
|
|
|
self.finishUpload(upload)
|
|
|
|
|
|
|
|
# Polls for the upload's status
|
|
|
|
finishUpload: (upload) ->
|
|
|
|
# TODO: update upload status
|
|
|
|
$http.get("/api/web/tracks/#{upload.trackId}/upload-status").then(
|
|
|
|
# handle success or still-processing
|
|
|
|
(response)->
|
|
|
|
if response.status == 202
|
|
|
|
$timeout(self.finishUploadWrapper(upload), 5000)
|
|
|
|
|
|
|
|
else if response.status == 201
|
|
|
|
upload.isProcessing = false
|
|
|
|
upload.success = true
|
|
|
|
|
|
|
|
# handle error
|
|
|
|
,(response)->
|
|
|
|
upload.isProcessing = false
|
|
|
|
if response.headers['content-type'] == 'application/json'
|
|
|
|
upload.error = response.data.error
|
|
|
|
else
|
|
|
|
upload.error = 'There was an unknown error!'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
upload: (files) ->
|
|
|
|
_.each files, (file) ->
|
|
|
|
upload =
|
|
|
|
name: file.name
|
|
|
|
progress: 0
|
|
|
|
uploadedSize: 0
|
|
|
|
size: file.size
|
|
|
|
index: self.queue.length
|
|
|
|
isUploading: true
|
2015-12-18 09:56:13 +01:00
|
|
|
isProcessing: false
|
|
|
|
trackId: null
|
2015-10-25 03:35:37 +01:00
|
|
|
success: false
|
|
|
|
error: null
|
2015-08-31 17:42:21 +02:00
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
self.queue.push upload
|
|
|
|
$rootScope.$broadcast 'upload-added', upload
|
2015-08-31 17:42:21 +02:00
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
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
|
2015-08-31 17:42:21 +02:00
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
# TODO: Implement polling here
|
|
|
|
# event listener
|
2015-10-25 03:35:37 +01:00
|
|
|
xhr.onload = -> $rootScope.$apply ->
|
|
|
|
upload.isUploading = false
|
2015-12-18 09:56:13 +01:00
|
|
|
upload.isProcessing = true
|
|
|
|
|
|
|
|
if xhr.status == 200
|
|
|
|
# kick off polling
|
|
|
|
upload.trackId = $.parseJSON(xhr.responseText).id
|
|
|
|
self.finishUpload(upload)
|
|
|
|
|
|
|
|
else
|
2015-10-25 03:35:37 +01:00
|
|
|
error =
|
|
|
|
if xhr.getResponseHeader('content-type') == 'application/json'
|
|
|
|
$.parseJSON(xhr.responseText).errors.track.join ', '
|
|
|
|
else
|
|
|
|
'There was an unknown error!'
|
2015-08-31 17:42:21 +02:00
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
upload.isProcessing = false
|
2015-10-25 03:35:37 +01:00
|
|
|
upload.error = error
|
|
|
|
$rootScope.$broadcast 'upload-error', [upload, error]
|
2015-08-31 17:42:21 +02:00
|
|
|
|
2016-02-15 20:05:58 +01:00
|
|
|
accountTracks.refresh(null, true)
|
|
|
|
.done($rootScope.$broadcast('upload-finished', upload))
|
2015-12-18 09:56:13 +01:00
|
|
|
|
|
|
|
# send the track to the server
|
2015-10-25 03:35:37 +01:00
|
|
|
formData = new FormData();
|
|
|
|
formData.append('track', file);
|
2015-08-31 17:42:21 +02:00
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
xhr.open 'POST', '/api/web/tracks/upload', true
|
2015-11-11 22:45:44 +01:00
|
|
|
xhr.setRequestHeader 'X-XSRF-TOKEN', $.cookie('XSRF-TOKEN')
|
2015-10-25 03:35:37 +01:00
|
|
|
xhr.send formData
|
2015-09-13 04:19:45 +02:00
|
|
|
])
|