From 78501007ceade513b2f549a8b389cfe6f0c8f655 Mon Sep 17 00:00:00 2001 From: Josef Citrine Date: Mon, 28 Nov 2016 00:19:16 +0000 Subject: [PATCH] Uploader for track versions, admin only for now --- app/Models/Track.php | 4 +- public/templates/directives/track-editor.html | 13 ++ .../app/directives/track-editor.coffee | 11 +- .../assets/scripts/app/services/upload.coffee | 118 ++++++++++++------ 4 files changed, 102 insertions(+), 44 deletions(-) diff --git a/app/Models/Track.php b/app/Models/Track.php index ca798fe4..a78dba19 100644 --- a/app/Models/Track.php +++ b/app/Models/Track.php @@ -534,7 +534,9 @@ class Track extends Model implements Searchable, Commentable, Favouritable $returnValue['username'] = User::whereId($track->user_id)->first()->username; // Seasonal - $returnValue['hwc_submit'] = Playlist::where('user_id', 22549)->first()->tracks()->get()->contains($track); + if (Playlist::where('user_id', 22549)->first()) { + $returnValue['hwc_submit'] = Playlist::where('user_id', 22549)->first()->tracks()->get()->contains($track); + } return $returnValue; } diff --git a/public/templates/directives/track-editor.html b/public/templates/directives/track-editor.html index 061fb2d5..e052aa6e 100644 --- a/public/templates/directives/track-editor.html +++ b/public/templates/directives/track-editor.html @@ -118,6 +118,19 @@
{{errors.username}}
+ +
+ + +
+ + + Uploading… + Processing… + Error + {{::upload.name}} - + {{upload.error}} +
diff --git a/resources/assets/scripts/app/directives/track-editor.coffee b/resources/assets/scripts/app/directives/track-editor.coffee index d71f0c82..118f42a7 100644 --- a/resources/assets/scripts/app/directives/track-editor.coffee +++ b/resources/assets/scripts/app/directives/track-editor.coffee @@ -21,8 +21,8 @@ module.exports = angular.module('ponyfm').directive 'pfmTrackEditor', () -> trackId: '=trackId' controller: [ - '$scope', '$modal', 'auth', 'account-tracks', 'account-albums', 'taxonomies', 'images' - ($scope, $modal, auth, tracks, albums, taxonomies, images) -> + '$scope', '$modal', 'auth', 'account-tracks', 'account-albums', 'taxonomies', 'images', '$state', 'upload' + ($scope, $modal, auth, tracks, albums, taxonomies, images, $state, upload) -> $scope.isDirty = false $scope.isSaving = false $scope.taxonomies = taxonomies @@ -33,6 +33,13 @@ module.exports = angular.module('ponyfm').directive 'pfmTrackEditor', () -> $scope.isAdmin = auth.data.isAdmin albumsDb = {} + $scope.data = upload + $scope.userSlug = $state.params.slug + + $scope.fileChanged = (e) -> + files = e.files + $scope.$apply -> upload.uploadNewVersion(files[0], $scope.userSlug, $scope.trackId) + $scope.$watch 'trackId', (newValue, oldValue) -> $scope.updateUI() diff --git a/resources/assets/scripts/app/services/upload.coffee b/resources/assets/scripts/app/services/upload.coffee index 916c8f6c..12415075 100644 --- a/resources/assets/scripts/app/services/upload.coffee +++ b/resources/assets/scripts/app/services/upload.coffee @@ -20,18 +20,23 @@ module.exports = angular.module('ponyfm').factory('upload', [ self = queue: [] - finishUploadWrapper: (upload)-> + finishUploadWrapper: (upload, versionUpdate)-> ()-> - self.finishUpload(upload) + self.finishUpload(upload, versionUpdate) # Polls for the upload's status - finishUpload: (upload) -> + finishUpload: (upload, versionUpdate) -> # TODO: update upload status - $http.get("/api/web/tracks/#{upload.trackId}/upload-status").then( + endpoint = "/api/web/tracks/#{upload.trackId}/upload-status" + + if versionUpdate + endpoint = "/api/web/tracks/#{upload.trackId}/version-upload-status" + + $http.get(endpoint).then( # handle success or still-processing (response)-> if response.status == 202 - $timeout(self.finishUploadWrapper(upload), 5000) + $timeout(self.finishUploadWrapper(upload, versionUpdate), 5000) else if response.status == 201 upload.isProcessing = false @@ -46,7 +51,73 @@ module.exports = angular.module('ponyfm').factory('upload', [ upload.error = 'There was an unknown error!' ) + uploadProcess: (upload, formData, trackId) -> + versionUpdate = false + if trackId > 0 + versionUpdate = true + 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 + + # TODO: Implement polling here + # event listener + xhr.onload = -> $rootScope.$apply -> + upload.isUploading = false + upload.isProcessing = true + + if xhr.status == 200 + # kick off polling + upload.trackId = $.parseJSON(xhr.responseText).id + self.finishUpload(upload, versionUpdate) + + else + error = + if xhr.getResponseHeader('content-type') == 'application/json' + 'Error: ' + $.parseJSON(xhr.responseText)?.errors?.track?.join ', ' + else + 'There was an unknown error!' + + upload.isProcessing = false + upload.error = error + $rootScope.$broadcast 'upload-error', [upload, error] + + accountTracks.refresh(null, true) + .done($rootScope.$broadcast('upload-finished', upload)) + + endpoint = '/api/web/tracks/upload' + + if versionUpdate + endpoint = '/api/web/tracks/' + trackId + '/version-upload' + + xhr.open 'POST', endpoint, true + xhr.setRequestHeader 'X-XSRF-TOKEN', $.cookie('XSRF-TOKEN') + xhr.send formData + + uploadNewVersion: (file, userSlug, trackId) -> + upload = + name: file.name + progress: 0 + uploadedSize: 0 + size: file.size + index: 0 + isUploading: true + isProcessing: false + trackId: trackId + success: false + error: null + + self.queue.push upload + $rootScope.$broadcast 'upload-added', upload + + formData = new FormData() + formData.append('track', file) + formData.append('user_slug', userSlug) + + self.uploadProcess(upload, formData, trackId) upload: (files, userSlug) -> _.each files, (file) -> @@ -65,44 +136,9 @@ module.exports = angular.module('ponyfm').factory('upload', [ 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 - - # TODO: Implement polling here - # event listener - xhr.onload = -> $rootScope.$apply -> - upload.isUploading = false - upload.isProcessing = true - - if xhr.status == 200 - # kick off polling - upload.trackId = $.parseJSON(xhr.responseText).id - self.finishUpload(upload) - - else - error = - if xhr.getResponseHeader('content-type') == 'application/json' - 'Error: ' + $.parseJSON(xhr.responseText)?.errors?.track?.join ', ' - else - 'There was an unknown error!' - - upload.isProcessing = false - upload.error = error - $rootScope.$broadcast 'upload-error', [upload, error] - - accountTracks.refresh(null, true) - .done($rootScope.$broadcast('upload-finished', upload)) - - # send the track to the server formData = new FormData() formData.append('track', file) formData.append('user_slug', userSlug) - xhr.open 'POST', '/api/web/tracks/upload', true - xhr.setRequestHeader 'X-XSRF-TOKEN', $.cookie('XSRF-TOKEN') - xhr.send formData + self.uploadProcess(upload, formData, 0) ])