github.com/GregorioDiStefano/go-file-storage@v0.0.0-20161001105139-5707ab351525/static/js/main.js (about) 1 var app = angular.module('myUpload', ['smoothScroll', 'ngFileUpload', 'ui.bootstrap']); 2 app.config(function($interpolateProvider) { 3 $interpolateProvider.startSymbol('[['); 4 $interpolateProvider.endSymbol(']]'); 5 }); 6 7 8 app.directive('customOnChange', function() { 9 return { 10 restrict: 'A', 11 link: function(scope, element, attrs) { 12 var onChangeHandler = scope.$eval(attrs.customOnChange); 13 element.bind('change', onChangeHandler); 14 } 15 }; 16 }); 17 18 var newPercent = 0; 19 app.controller('uploadCtrl', ['$scope', 'Upload', "$http", "$timeout", function($scope, Upload, $http, $timeout) { 20 21 removeProgress = function() { 22 setTimeout(function() { 23 $scope.$apply(function() { 24 newPercent = 0 25 $scope.data = { 26 progress: 0, 27 show: false 28 }; 29 }); 30 }, 200); 31 }; 32 33 $scope.data = { 34 progress: 0, 35 show: false 36 }; 37 38 (function progress() { 39 $timeout(function() { 40 if (newPercent > 0) { 41 $scope.data.show = true 42 $scope.data.progress = newPercent 43 if ($scope.data.progress == 100) { 44 console.log($scope.data.progress) 45 $scope.data.progress = "Syncing..." 46 } 47 } 48 progress(); 49 }, 50); 50 51 })(); 52 53 $scope.uploadFile = function(file) { 54 if (file) { 55 console.log("Uploading: ", file) 56 file.upload = Upload.http({ 57 method: 'PUT', 58 url: location.protocol + '//' + location.hostname + "/" + file.name, 59 data: file, 60 }).then(function(resp) { 61 swal({ 62 title: "Upload complete!", 63 text: "<p> Download URL: " + resp.data.downloadURL + "</p><p> Delete URL: " + resp.data.deleteURL + "</p>", 64 customClass: 'swal-wide', 65 html: true 66 }); 67 removeProgress() 68 }, function(resp) { 69 console.log('Error status: ' + resp.status); 70 if (resp.status == "429") { 71 swal({ 72 title: "Oops", 73 text: "Too many requests, please try again in an hour." 74 }); 75 removeProgress() 76 } 77 }, function(evt) { 78 var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); 79 newPercent = progressPercentage 80 }); 81 } 82 } 83 }]);