Browse code

Merge pull request #479 from fauxpark/filesize-patch

Add format_size Twig filter

Klaus Silveira authored on 07/07/2014 14:34:15
Showing 3 changed files
... ...
@@ -65,6 +65,7 @@ class Application extends SilexApplication
65 65
             $twig->addFilter(new \Twig_SimpleFilter('htmlentities', 'htmlentities'));
66 66
             $twig->addFilter(new \Twig_SimpleFilter('md5', 'md5'));
67 67
             $twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate')));
68
+            $twig->addFilter(new \Twig_SimpleFilter('format_size', array($app, 'formatSize')));
68 69
 
69 70
             return $twig;
70 71
         }));
... ...
@@ -97,6 +98,14 @@ class Application extends SilexApplication
97 97
         return $date->format($this['date.format']);
98 98
     }
99 99
 
100
+    public function formatSize($size)
101
+    {
102
+        $mod = 1000;
103
+        $units = array('B', 'kB', 'MB', 'GB');
104
+        for($i = 0; $size > $mod; $i++) $size /= $mod;
105
+        return round($size, 2) . $units[$i];
106
+    }
107
+
100 108
     public function getPath()
101 109
     {
102 110
         return $this->path . DIRECTORY_SEPARATOR;
... ...
@@ -37,7 +37,7 @@
37 37
                     </p>
38 38
 
39 39
                     <p>
40
-                        <strong>Total bytes:</strong> {{ stats.size }} bytes ({{ ((stats.size / 1024) / 1024) | number_format }} MB)
40
+                        <strong>Total bytes:</strong> {{ stats.size }} bytes ({{ stats.size | format_size }})
41 41
                     </p>
42 42
                 </td>
43 43
             </tr>
... ...
@@ -50,7 +50,7 @@
50 50
                     {%- endif -%}
51 51
                 ">{{ file.name }}</a></td>
52 52
                 <td>{{ file.mode }}</td>
53
-                <td>{% if file.size %}{{ (file.size / 1024) | number_format }} kb{% endif %}</td>
53
+                <td>{% if file.size %}{{ file.size | format_size }}{% endif %}</td>
54 54
             </tr>
55 55
             {% endfor %}
56 56
         </tbody>