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)) {
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>';
}
},

View File

@ -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]);
};
/**

View File

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

View File

@ -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"

View File

@ -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'

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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'

View File

@ -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'

View File

@ -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"

View File

@ -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'

View File

@ -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мин'

View File

@ -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'

View File

@ -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"

View File

@ -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: '分享一個到本帖的鏈接'

View File

@ -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

View File

@ -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

View File

@ -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() {