Simfile quota stuff.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 5 Dec 2014 03:16:01 +0000 (11:16 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 5 Dec 2014 03:16:01 +0000 (11:16 +0800)
app/components/menu/menu.html
app/components/simfiles/simfile-directive.js
app/components/simfiles/simfile.html
app/components/simfiles/simfiles-service.js
app/pages/simfiles/simfiles.js

index 5d32bc6..359ba74 100644 (file)
@@ -1,4 +1,5 @@
 <ul class="menu">
     <li><a href="#/">Home</a></li>
     <li><a href="#/simfiles">Simfiles</a></li>
+    <li><a href="#/packs">Packs</a></li>
 </ul>
\ No newline at end of file
index cb8941f..0ae81c0 100644 (file)
@@ -3,7 +3,7 @@
 //don't put ,[] because that declares a new module. This gets the existing one
 angular.module('DivinElegy.components.simfiles').
         
-directive('simfile', function(
+directive('simfile', ['$rootScope', 'UserService', 'HelloService', 'rockEndpoint', function($rootScope, UserService, HelloService, rockEndpoint
 {
     return {
         restrict: 'E',
@@ -20,6 +20,57 @@ directive('simfile', function()
             size: '=',
             download: '='
         },
-        templateUrl: 'components/simfiles/simfile.html'
+        templateUrl: 'components/simfiles/simfile.html',
+        link: function($scope) {
+            var filesizeBytes = function(size)  
+            {  
+                //units are already bytes
+                if(!isNaN(size.substring(size.length-2, size.length-1)))
+                {
+                    return size.substring(0, size.length-1);
+                }
+
+                var units = size.substring(size.length-2, size.length);
+                var size = size.substring(0, size.length-2);
+                var fileUnits = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];  
+
+                for(var i=0; i<fileUnits.length; i++)
+                {
+                    if(fileUnits[i] == units) { break; }
+                    size = size*1000;
+                }
+
+                return Number(size);  
+            };
+
+            $scope.downloadFromDe = function(simfile)
+            {      
+                UserService.getCurrentUser().then(function(user)
+                {
+                    var size = filesizeBytes(simfile.size);
+                    var quotaRemaining = filesizeBytes(user.quotaRemaining);
+                console.log('here');
+                    if(quotaRemaining < size)
+                    {
+                        $rootScope.$broadcast('message.error', 'Sorry, you do not have enough quota to download that file. Quota resets at 00:00 UTC+0'); 
+                    } else {
+                        //TODO: Maybe access token should be in user obj?
+                        var url = rockEndpoint + '' + simfile.download + '?token=' + HelloService.getAccessToken(); //0th mirror will always be de
+                        $rootScope.$broadcast('message.warning', 'You are about to download ' + simfile.title + ' which is ' + simfile.size + '. Your current quota is ' + user.quotaRemaining + ' click <a ng-click="updateUserCache()" href="' + url + '">here</a> to confirm.'); 
+                    }
+                });
+            };
+
+            $rootScope.updateUserCache = function()
+            {
+                //TODO: This may not be the best way to do this, but basically when this function
+                //is called by ng-click, rock.de hasn't had time to log the download yet so if
+                //we update the cache straight away, we still get the old user quota. Waiting
+                //a second gives it time to sort itself out.
+                setTimeout(function() {
+                    UserService.updateCache();
+                },1000);
+            };
+        }
     };
-});
\ No newline at end of file
+}]);
\ No newline at end of file
index d5f7cf9..9b0f22a 100644 (file)
@@ -46,7 +46,7 @@
     </tr>
     <tr ng-if="download">
         <th>Download:</th>
-        <td><a href="{{rockEndpoint}}{{download}}">DivinElegy</a></td>
+        <td><a ng-click="downloadFromDe(simfile)" class="de-link">DivinElegy</a></td>
     </tr>
 </table>
 <div class="clearfix"></div>
\ No newline at end of file
index 577f1a7..d00d481 100644 (file)
@@ -1,6 +1,6 @@
 'use strict';
 
-angular.module("DivinElegy.components.simfiles", ['DivinElegy.components.config']).
+angular.module("DivinElegy.components.simfiles", ['DivinElegy.components.config', 'DivinElegy.components.user', 'DivinElegy.components.config', 'DivinElegy.components.hello']).
       
 factory("SimfileService", ['rockEndpoint', '$http', '$q', 'HelloService', function(rockEndpoint, $http, $q, HelloService)
 {
index 278fd16..ee80983 100644 (file)
@@ -15,7 +15,7 @@ angular.module("DivinElegy.pages.simfiles", ["DivinElegy.components.simfiles","D
     $scope.bgChangesFilterKeyword = null;
     $scope.bpmChangesFilterKeyword = null;
     $scope.simfileList = [];
-    
+        
     $scope.openListing = function(listing)
     {
         if($scope.isListingActive(listing))