From 3397c2235f5107f5c73eb17f7ea0e3852544524b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Sun, 14 Jul 2013 12:28:24 +0200
Subject: [PATCH] display the size of the attachment in the preview

instead of adding it during post processing
---
 .../discourse/components/utilities.js          |  2 +-
 .../discourse/helpers/i18n_helpers.js          | 18 ++++++++++++------
 app/serializers/upload_serializer.rb           |  2 +-
 config/locales/client.cs.yml                   | 10 ++++++++++
 config/locales/client.da.yml                   | 12 ++++++++++++
 config/locales/client.de.yml                   | 12 ++++++++++++
 config/locales/client.en.yml                   | 12 ++++++++++++
 config/locales/client.es.yml                   | 12 ++++++++++++
 config/locales/client.fr.yml                   | 12 ++++++++++++
 config/locales/client.id.yml                   | 12 ++++++++++++
 config/locales/client.it.yml                   | 12 ++++++++++++
 config/locales/client.nb_NO.yml                | 12 ++++++++++++
 config/locales/client.nl.yml                   | 12 ++++++++++++
 config/locales/client.pt.yml                   | 12 ++++++++++++
 config/locales/client.ru.yml                   | 14 ++++++++++++++
 config/locales/client.sv.yml                   | 12 ++++++++++++
 config/locales/client.zh_CN.yml                | 12 ++++++++++++
 config/locales/client.zh_TW.yml                | 12 ++++++++++++
 lib/cooked_post_processor.rb                   | 13 +------------
 spec/components/cooked_post_processor_spec.rb  |  4 ----
 test/javascripts/components/utilities_test.js  |  3 ++-
 21 files changed, 197 insertions(+), 25 deletions(-)

diff --git a/app/assets/javascripts/discourse/components/utilities.js b/app/assets/javascripts/discourse/components/utilities.js
index 9b010113c..567fea469 100644
--- a/app/assets/javascripts/discourse/components/utilities.js
+++ b/app/assets/javascripts/discourse/components/utilities.js
@@ -224,7 +224,7 @@ Discourse.Utilities = {
     if (this.isAnImage(upload.original_filename)) {
       return '<img src="' + upload.url + '" width="' + upload.width + '" height="' + upload.height + '">';
     } else {
-      return '<a class="attachment" href="' + upload.url + '">' + upload.original_filename + '</a>';
+      return '<a class="attachment" href="' + upload.url + '">' + upload.original_filename + '</a><span class="size">(' + I18n.toHumanSize(upload.filesize) + ')</span>';
     }
   },
 
diff --git a/app/assets/javascripts/discourse/helpers/i18n_helpers.js b/app/assets/javascripts/discourse/helpers/i18n_helpers.js
index b6b7af10f..4a9433514 100644
--- a/app/assets/javascripts/discourse/helpers/i18n_helpers.js
+++ b/app/assets/javascripts/discourse/helpers/i18n_helpers.js
@@ -2,13 +2,19 @@
  We always prefix with "js." to select exactly what we want passed
  through to the front end.
 **/
-
 var oldI18nlookup = I18n.lookup;
-I18n.lookup = function() {
-  // jshint doesn't like when we change the arguments directly...
-  var args = arguments;
-  if (args.length > 0) { args[0] = "js." + args[0]; }
-  return oldI18nlookup.apply(this, args);
+I18n.lookup = function(scope, options) {
+  return oldI18nlookup.apply(this, ["js." + scope, options]);
+};
+
+/**
+ Default format for storage units
+**/
+var oldI18ntoHumanSize = I18n.toHumanSize;
+I18n.toHumanSize = function(number, options) {
+  options = options || {};
+  options.format = I18n.t("number.human.storage_units.format");
+  return oldI18ntoHumanSize.apply(this, [number, options]);
 };
 
 /**
diff --git a/app/serializers/upload_serializer.rb b/app/serializers/upload_serializer.rb
index 9a0a7753b..1d3812e20 100644
--- a/app/serializers/upload_serializer.rb
+++ b/app/serializers/upload_serializer.rb
@@ -1,5 +1,5 @@
 class UploadSerializer < ApplicationSerializer
 
-  attributes :url, :original_filename, :width, :height
+  attributes :url, :original_filename, :filesize, :width, :height
 
 end
diff --git a/config/locales/client.cs.yml b/config/locales/client.cs.yml
index b55cb2e59..ef0106645 100644
--- a/config/locales/client.cs.yml
+++ b/config/locales/client.cs.yml
@@ -7,6 +7,16 @@
 
 cs:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte: B
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     dates:
       tiny:
         half_a_minute: "< 1m"
diff --git a/config/locales/client.da.yml b/config/locales/client.da.yml
index 6421b039d..6fc9a4aaf 100644
--- a/config/locales/client.da.yml
+++ b/config/locales/client.da.yml
@@ -6,6 +6,18 @@
 
 da:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     share:
       topic: 'del et link til dette emne'
       post: 'del et link til dette indlæg'
diff --git a/config/locales/client.de.yml b/config/locales/client.de.yml
index 9d01f04e1..32ca6ca00 100644
--- a/config/locales/client.de.yml
+++ b/config/locales/client.de.yml
@@ -7,6 +7,18 @@
 
 de:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     dates:
       short_date_no_year: "D MMM"
       short_date: "D. MMM YYYY"
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 5f2e17691..b294740f8 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -7,6 +7,18 @@
 
 en:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     dates:
       tiny:
         half_a_minute: "< 1m"
diff --git a/config/locales/client.es.yml b/config/locales/client.es.yml
index 1b0d59606..cf1147e94 100644
--- a/config/locales/client.es.yml
+++ b/config/locales/client.es.yml
@@ -8,6 +8,18 @@
 
 es:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     dates:
       tiny:
         half_a_minute: "< 1m"
diff --git a/config/locales/client.fr.yml b/config/locales/client.fr.yml
index 030b2134b..bebe7881f 100644
--- a/config/locales/client.fr.yml
+++ b/config/locales/client.fr.yml
@@ -11,6 +11,18 @@
 
 fr:
   js:
+    number:
+      human:
+        storage_units:
+          format: "%n %u"
+          units:
+            byte:
+              one:   "octet"
+              other: "octets"
+            kb: "ko"
+            mb: "Mo"
+            gb: "Go"
+            tb: "To"
     dates:
       tiny:
         half_a_minute: "< 1m"
diff --git a/config/locales/client.id.yml b/config/locales/client.id.yml
index 25943e3e5..f14e35210 100644
--- a/config/locales/client.id.yml
+++ b/config/locales/client.id.yml
@@ -8,6 +8,18 @@
 
 id:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Byte
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     share:
       topic: "Bagikan tautan ke topik ini"
       post: "Bagikan tautan ke muatan ini"
diff --git a/config/locales/client.it.yml b/config/locales/client.it.yml
index 0301373d6..cb9d18fd8 100644
--- a/config/locales/client.it.yml
+++ b/config/locales/client.it.yml
@@ -7,6 +7,18 @@
 
 it:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Byte
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     share:
       topic: 'condividi un link a questo topic'
       post: 'condividi un link a questo post'
diff --git a/config/locales/client.nb_NO.yml b/config/locales/client.nb_NO.yml
index 38e14f79e..193c48e38 100644
--- a/config/locales/client.nb_NO.yml
+++ b/config/locales/client.nb_NO.yml
@@ -7,6 +7,18 @@
 
 nb_NO:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: kB
+            mb: MB
+            tb: TB
     share:
       topic: 'del en link til dette emnet'
       post: 'del en link til dette innlegget'
diff --git a/config/locales/client.nl.yml b/config/locales/client.nl.yml
index 68c545a88..574a9f64b 100644
--- a/config/locales/client.nl.yml
+++ b/config/locales/client.nl.yml
@@ -12,6 +12,18 @@
 
 nl:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     dates:
       tiny:
         half_a_minute: "< 1m"
diff --git a/config/locales/client.pt.yml b/config/locales/client.pt.yml
index 6e8da9d3e..9315f73c1 100644
--- a/config/locales/client.pt.yml
+++ b/config/locales/client.pt.yml
@@ -8,6 +8,18 @@
 
 pt:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     share:
       topic: 'partilhe um link para este tópico'
       post: 'partilhe um link para esta mensagem'
diff --git a/config/locales/client.ru.yml b/config/locales/client.ru.yml
index 26d969c8c..6add65a8e 100644
--- a/config/locales/client.ru.yml
+++ b/config/locales/client.ru.yml
@@ -10,6 +10,20 @@
 
 ru:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              few: байта
+              many: байт
+              one: байт
+              other: байта
+            gb: ГБ
+            kb: КБ
+            mb: МБ
+            tb: ТБ
     dates:
       tiny:
         half_a_minute: '< 1мин'
diff --git a/config/locales/client.sv.yml b/config/locales/client.sv.yml
index c6e6e9606..332ab576d 100644
--- a/config/locales/client.sv.yml
+++ b/config/locales/client.sv.yml
@@ -8,6 +8,18 @@
 
 sv:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     share:
       topic: 'dela en länk till denna tråd'
       post: 'dela en länk till denna tråd'
diff --git a/config/locales/client.zh_CN.yml b/config/locales/client.zh_CN.yml
index d7e1aa1bc..aa4bd13c8 100644
--- a/config/locales/client.zh_CN.yml
+++ b/config/locales/client.zh_CN.yml
@@ -7,6 +7,18 @@
 
 zh_CN:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     dates:
       short_date_no_year: "D MMM"
       short_date: "D MMM, YYYY"
diff --git a/config/locales/client.zh_TW.yml b/config/locales/client.zh_TW.yml
index 1b8308a96..a81583abb 100644
--- a/config/locales/client.zh_TW.yml
+++ b/config/locales/client.zh_TW.yml
@@ -7,6 +7,18 @@
 
 zh_TW:
   js:
+    number:
+      human:
+        storage_units:
+          format: ! '%n %u'
+          units:
+            byte:
+              one: Byte
+              other: Bytes
+            gb: GB
+            kb: KB
+            mb: MB
+            tb: TB
     share:
       topic: '分享一個到本主題的鏈接'
       post: '分享一個到本帖的鏈接'
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb
index 84db92074..211272f0b 100644
--- a/lib/cooked_post_processor.rb
+++ b/lib/cooked_post_processor.rb
@@ -25,14 +25,10 @@ class CookedPostProcessor
     attachments.each do |attachment|
       href = attachment['href']
       attachment['href'] = relative_to_absolute(href)
+      # update reverse index
       if upload = Upload.get_from_url(href)
-        # update reverse index
         associate_to_post(upload)
-        # append the size
-        append_human_size!(attachment, upload)
       end
-      # mark as dirty
-      @dirty = true
     end
   end
 
@@ -234,13 +230,6 @@ class CookedPostProcessor
     end
   end
 
-  def append_human_size!(attachment, upload)
-    size = Nokogiri::XML::Node.new("span", @doc)
-    size["class"] = "size"
-    size.content = "(#{number_to_human_size(upload.filesize)})"
-    attachment.add_next_sibling(size)
-  end
-
   def dirty?
     @dirty
   end
diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb
index e216c8b6c..e33994dbc 100644
--- a/spec/components/cooked_post_processor_spec.rb
+++ b/spec/components/cooked_post_processor_spec.rb
@@ -34,8 +34,6 @@ describe CookedPostProcessor do
         cpp.html.should =~ /#{LocalStore.base_url}/
         # ensure name is present
         cpp.html.should =~ /archive.zip/
-        # ensure size is present
-        cpp.html.should =~ /<span class=\"size\">\(1.21 KB\)<\/span>/
         # dirty
         cpp.should be_dirty
         # keeps the reverse index up to date
@@ -79,8 +77,6 @@ describe CookedPostProcessor do
         cpp.post_process_images
         # ensures absolute urls on uploaded images
         cpp.html.should =~ /#{LocalStore.base_url}/
-        # dirty
-        cpp.should be_dirty
         # keeps the reverse index up to date
         post.uploads.reload
         post.uploads.count.should == 1
diff --git a/test/javascripts/components/utilities_test.js b/test/javascripts/components/utilities_test.js
index 416b2fde2..08a74179b 100644
--- a/test/javascripts/components/utilities_test.js
+++ b/test/javascripts/components/utilities_test.js
@@ -88,6 +88,7 @@ test("isAuthorizedUpload", function() {
 var getUploadMarkdown = function(filename) {
   return utils.getUploadMarkdown({
     original_filename: filename,
+    filesize: 42,
     width: 100,
     height: 200,
     url: "/upload/123/abcdef.ext"
@@ -96,7 +97,7 @@ var getUploadMarkdown = function(filename) {
 
 test("getUploadMarkdown", function() {
   ok(getUploadMarkdown("lolcat.gif") === '<img src="/upload/123/abcdef.ext" width="100" height="200">');
-  ok(getUploadMarkdown("important.txt") === '<a class="attachment" href="/upload/123/abcdef.ext">important.txt</a>');
+  ok(getUploadMarkdown("important.txt") === '<a class="attachment" href="/upload/123/abcdef.ext">important.txt</a><span class="size">(42 Bytes)</span>');
 });
 
 test("isAnImage", function() {