display the size of the attachment in the preview

instead of adding it during post processing
This commit is contained in:
Régis Hanol
2013-07-14 12:28:24 +02:00
parent 6f2ce93ab2
commit 3397c2235f
21 changed files with 197 additions and 25 deletions

View File

@ -224,7 +224,7 @@ Discourse.Utilities = {
if (this.isAnImage(upload.original_filename)) { if (this.isAnImage(upload.original_filename)) {
return '<img src="' + upload.url + '" width="' + upload.width + '" height="' + upload.height + '">'; return '<img src="' + upload.url + '" width="' + upload.width + '" height="' + upload.height + '">';
} else { } 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>';
} }
}, },

View File

@ -2,13 +2,19 @@
We always prefix with "js." to select exactly what we want passed We always prefix with "js." to select exactly what we want passed
through to the front end. through to the front end.
**/ **/
var oldI18nlookup = I18n.lookup; var oldI18nlookup = I18n.lookup;
I18n.lookup = function() { I18n.lookup = function(scope, options) {
// jshint doesn't like when we change the arguments directly... return oldI18nlookup.apply(this, ["js." + scope, options]);
var args = arguments; };
if (args.length > 0) { args[0] = "js." + args[0]; }
return oldI18nlookup.apply(this, args); /**
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]);
}; };
/** /**

View File

@ -1,5 +1,5 @@
class UploadSerializer < ApplicationSerializer class UploadSerializer < ApplicationSerializer
attributes :url, :original_filename, :width, :height attributes :url, :original_filename, :filesize, :width, :height
end end

View File

@ -7,6 +7,16 @@
cs: cs:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte: B
gb: GB
kb: KB
mb: MB
tb: TB
dates: dates:
tiny: tiny:
half_a_minute: "< 1m" half_a_minute: "< 1m"

View File

@ -6,6 +6,18 @@
da: da:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
share: share:
topic: 'del et link til dette emne' topic: 'del et link til dette emne'
post: 'del et link til dette indlæg' post: 'del et link til dette indlæg'

View File

@ -7,6 +7,18 @@
de: de:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
dates: dates:
short_date_no_year: "D MMM" short_date_no_year: "D MMM"
short_date: "D. MMM YYYY" short_date: "D. MMM YYYY"

View File

@ -7,6 +7,18 @@
en: en:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
dates: dates:
tiny: tiny:
half_a_minute: "< 1m" half_a_minute: "< 1m"

View File

@ -8,6 +8,18 @@
es: es:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
dates: dates:
tiny: tiny:
half_a_minute: "< 1m" half_a_minute: "< 1m"

View File

@ -11,6 +11,18 @@
fr: fr:
js: js:
number:
human:
storage_units:
format: "%n %u"
units:
byte:
one: "octet"
other: "octets"
kb: "ko"
mb: "Mo"
gb: "Go"
tb: "To"
dates: dates:
tiny: tiny:
half_a_minute: "< 1m" half_a_minute: "< 1m"

View File

@ -8,6 +8,18 @@
id: id:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Byte
gb: GB
kb: KB
mb: MB
tb: TB
share: share:
topic: "Bagikan tautan ke topik ini" topic: "Bagikan tautan ke topik ini"
post: "Bagikan tautan ke muatan ini" post: "Bagikan tautan ke muatan ini"

View File

@ -7,6 +7,18 @@
it: it:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Byte
gb: GB
kb: KB
mb: MB
tb: TB
share: share:
topic: 'condividi un link a questo topic' topic: 'condividi un link a questo topic'
post: 'condividi un link a questo post' post: 'condividi un link a questo post'

View File

@ -7,6 +7,18 @@
nb_NO: nb_NO:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: kB
mb: MB
tb: TB
share: share:
topic: 'del en link til dette emnet' topic: 'del en link til dette emnet'
post: 'del en link til dette innlegget' post: 'del en link til dette innlegget'

View File

@ -12,6 +12,18 @@
nl: nl:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
dates: dates:
tiny: tiny:
half_a_minute: "< 1m" half_a_minute: "< 1m"

View File

@ -8,6 +8,18 @@
pt: pt:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
share: share:
topic: 'partilhe um link para este tópico' topic: 'partilhe um link para este tópico'
post: 'partilhe um link para esta mensagem' post: 'partilhe um link para esta mensagem'

View File

@ -10,6 +10,20 @@
ru: ru:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
few: байта
many: байт
one: байт
other: байта
gb: ГБ
kb: КБ
mb: МБ
tb: ТБ
dates: dates:
tiny: tiny:
half_a_minute: '< 1мин' half_a_minute: '< 1мин'

View File

@ -8,6 +8,18 @@
sv: sv:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
share: share:
topic: 'dela en länk till denna tråd' topic: 'dela en länk till denna tråd'
post: 'dela en länk till denna tråd' post: 'dela en länk till denna tråd'

View File

@ -7,6 +7,18 @@
zh_CN: zh_CN:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
dates: dates:
short_date_no_year: "D MMM" short_date_no_year: "D MMM"
short_date: "D MMM, YYYY" short_date: "D MMM, YYYY"

View File

@ -7,6 +7,18 @@
zh_TW: zh_TW:
js: js:
number:
human:
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
share: share:
topic: '分享一個到本主題的鏈接' topic: '分享一個到本主題的鏈接'
post: '分享一個到本帖的鏈接' post: '分享一個到本帖的鏈接'

View File

@ -25,14 +25,10 @@ class CookedPostProcessor
attachments.each do |attachment| attachments.each do |attachment|
href = attachment['href'] href = attachment['href']
attachment['href'] = relative_to_absolute(href) attachment['href'] = relative_to_absolute(href)
if upload = Upload.get_from_url(href)
# update reverse index # update reverse index
if upload = Upload.get_from_url(href)
associate_to_post(upload) associate_to_post(upload)
# append the size
append_human_size!(attachment, upload)
end end
# mark as dirty
@dirty = true
end end
end end
@ -234,13 +230,6 @@ class CookedPostProcessor
end end
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? def dirty?
@dirty @dirty
end end

View File

@ -34,8 +34,6 @@ describe CookedPostProcessor do
cpp.html.should =~ /#{LocalStore.base_url}/ cpp.html.should =~ /#{LocalStore.base_url}/
# ensure name is present # ensure name is present
cpp.html.should =~ /archive.zip/ cpp.html.should =~ /archive.zip/
# ensure size is present
cpp.html.should =~ /<span class=\"size\">\(1.21 KB\)<\/span>/
# dirty # dirty
cpp.should be_dirty cpp.should be_dirty
# keeps the reverse index up to date # keeps the reverse index up to date
@ -79,8 +77,6 @@ describe CookedPostProcessor do
cpp.post_process_images cpp.post_process_images
# ensures absolute urls on uploaded images # ensures absolute urls on uploaded images
cpp.html.should =~ /#{LocalStore.base_url}/ cpp.html.should =~ /#{LocalStore.base_url}/
# dirty
cpp.should be_dirty
# keeps the reverse index up to date # keeps the reverse index up to date
post.uploads.reload post.uploads.reload
post.uploads.count.should == 1 post.uploads.count.should == 1

View File

@ -88,6 +88,7 @@ test("isAuthorizedUpload", function() {
var getUploadMarkdown = function(filename) { var getUploadMarkdown = function(filename) {
return utils.getUploadMarkdown({ return utils.getUploadMarkdown({
original_filename: filename, original_filename: filename,
filesize: 42,
width: 100, width: 100,
height: 200, height: 200,
url: "/upload/123/abcdef.ext" url: "/upload/123/abcdef.ext"
@ -96,7 +97,7 @@ var getUploadMarkdown = function(filename) {
test("getUploadMarkdown", function() { test("getUploadMarkdown", function() {
ok(getUploadMarkdown("lolcat.gif") === '<img src="/upload/123/abcdef.ext" width="100" height="200">'); 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() { test("isAnImage", function() {