11const async = require ( 'async' ) ,
2- log = require ( '../../helpers/logger' ) ,
3- Game = require ( '../../models/game' ) ,
4- User = require ( '../../models/user' ) ;
2+ http = require ( 'http' ) ,
3+ https = require ( 'https' ) ,
4+ log = require ( '../../helpers/logger' ) ,
5+ Game = require ( '../../models/game' ) ,
6+ User = require ( '../../models/user' ) ;
57
68/**
79 * Abstraction to handle the page errors
@@ -22,17 +24,31 @@ function handleError(req, res, errors)
2224}
2325
2426function validateRequest ( req ) {
25- req . checkBody ( 'title' , 'Title is required' ) . notEmpty ( ) ;
27+ req . checkBody ( 'title' , 'Title is required' ) . notEmpty ( ) ;
2628 req . checkBody ( 'bundleId' , 'Bundle ID is required' ) . isBundleId ( ) ;
2729 req . checkBody ( 'slug' , 'Slug is required' ) . isSlug ( ) ;
2830 req . checkBody ( 'repository' , 'Repository needs to be a URL' ) . isURL ( ) ;
2931 req . checkBody ( 'location' , 'Location needs to be a URL' ) . isURL ( ) ;
3032 req . checkBody ( 'description' ) . optional ( ) ;
31- req . checkBody ( 'thumbnail' ) . optional ( ) ;
32- var errors = req . validationErrors ( ) ;
33+ req . checkBody ( 'thumbnail' ) . optional ( ) ;
34+ var errors = req . validationErrors ( ) ;
3335 return errors ? errors : false ;
3436}
3537
38+ /**
39+ * Convert bytes into a human readable format
40+ * source: https://stackoverflow.com/a/20732091/10236401
41+ * thanks andrew!
42+ *
43+ * @param {integer } size file size that we want to convert
44+ * @return {string } file size in human readable format
45+ */
46+ function niceFileSize ( size ) {
47+ const i = Math . floor ( Math . log ( size ) / Math . log ( 1024 ) ) ;
48+
49+ return ( size / Math . pow ( 1024 , i ) ) . toFixed ( 2 ) * 1 + ' ' + [ 'B' , 'kB' , 'MB' , 'GB' , 'TB' ] [ i ] ;
50+ }
51+
3652/**
3753 * Abstraction to render a page, takes care of all
3854 * of the access control and populate the page with
@@ -49,14 +65,14 @@ function renderPage(req, res, template, populate=null)
4965 [
5066 function ( done )
5167 {
52- var game = Game . getBySlug ( req . params . slug , done ) ;
53- if ( populate ) {
54- game . populate ( {
68+ var game = Game . getBySlug ( req . params . slug , done ) ;
69+ if ( populate ) {
70+ game . populate ( {
5571 path : populate ,
5672 options : { sort : { 'updated' : - 1 } }
5773 } ) ;
5874 } ;
59- } ,
75+ } ,
6076 function ( game , done )
6177 {
6278 if ( ! game ) {
@@ -66,7 +82,7 @@ function renderPage(req, res, template, populate=null)
6682 game . getAccess ( req . user , done ) ;
6783 }
6884 ] ,
69- function ( err , game , access )
85+ async function ( err , game , access )
7086 {
7187 if ( err )
7288 {
@@ -89,6 +105,18 @@ function renderPage(req, res, template, populate=null)
89105 : Number ( req . query . page )
90106 : 1 ;
91107
108+ // iterate game releases to add file sizes
109+ for ( let k = 0 ; k < game . releases . length ; k ++ ) {
110+ const compressedSize = parseInt ( game . releases [ k ] . releaseCompressedSize || 0 ) ;
111+ if ( compressedSize > 0 ) {
112+ game . releases [ k ] . releaseCompressedSize = niceFileSize ( compressedSize ) ;
113+ }
114+ const uncompressedSize = parseInt ( game . releases [ k ] . releaseUncompressedSize || 0 ) ;
115+ if ( uncompressedSize > 0 ) {
116+ game . releases [ k ] . releaseUncompressedSize = niceFileSize ( uncompressedSize ) ;
117+ }
118+ }
119+
92120 res . render ( template , {
93121 game : game ,
94122 page : pageIndex ,
0 commit comments