在 Rails 中将文件大小字符串转换为千字节等效值
我的目标是转换表单输入,例如“100 MB”或“1 GB”,并将其转换为可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:
def quota_convert
@regex = /([0-9]+) (.*)s/
@sizes = %w{kilobyte megabyte gigabyte}
m = self.quota.match(@regex)
if @sizes.include? m[2]
eval("self.quota = #{m[1]}.#{m[2]}")
end
end
这有效,但仅当输入是倍数(“千兆字节”,但不是“千兆字节”)并且由于使用eval而显得疯狂不安全时代码>.所以,功能正常,但我今晚睡不好。
有什么指导吗?
编辑: ------
好吧。由于某种原因,带有 (.*?) 的正则表达式在我的设置中无法正常工作,但我已经使用 Rails 的东西解决了这个问题。另外,我意识到字节对我来说会更好。
def quota_convert
@regex = /^([0-9]+\.?[0-9]*?) (.*)/
@sizes = { 'kilobyte' => 1024, 'megabyte' => 1048576, 'gigabyte' => 1073741824}
m = self.quota.match(@regex)
if @sizes.include? m[2].singularize
self.quota = m[1].to_f*@sizes[m[2].singularize]
end
end
这可以捕获“1 兆字节”、“1.5 兆字节”和大多数其他内容(我希望如此)。无论如何,它都会使其成为单一版本。然后它进行乘法并给出神奇的答案。
这是合法的吗?
再次编辑:请参阅下面的答案。比我的废话干净多了。
My objective is to convert form input, like "100 megabytes" or "1 gigabyte", and converts it to a filesize in kilobytes I can store in the database. Currently, I have this:
def quota_convert
@regex = /([0-9]+) (.*)s/
@sizes = %w{kilobyte megabyte gigabyte}
m = self.quota.match(@regex)
if @sizes.include? m[2]
eval("self.quota = #{m[1]}.#{m[2]}")
end
end
This works, but only if the input is a multiple ("gigabytes", but not "gigabyte") and seems insanely unsafe due to the use of eval
. So, functional, but I won't sleep well tonight.
Any guidance?
EDIT: ------
All right. For some reason, the regex with (.*?) isn't working correctly on my setup, but I've worked around it with Rails stuff. Also, I've realized that bytes would work better for me.
def quota_convert
@regex = /^([0-9]+\.?[0-9]*?) (.*)/
@sizes = { 'kilobyte' => 1024, 'megabyte' => 1048576, 'gigabyte' => 1073741824}
m = self.quota.match(@regex)
if @sizes.include? m[2].singularize
self.quota = m[1].to_f*@sizes[m[2].singularize]
end
end
This catches "1 megabyte", "1.5 megabytes", and most other things (I hope). It then makes it the singular version regardless. Then it does the multiplication and spits out magic answers.
Is this legit?
EDIT AGAIN: See answer below. Much cleaner than my nonsense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Rails ActiveHelper number_to_ human_size。
You can use Rails ActiveHelper number_to_human_size.
?
作为可选复数。?
for optional plural in the regex.为什么不简单地创建一个散列,其中包含乘数的各种拼写作为键和数值作为值?无需评估,也无需正则表达式!
why don't you simply create a hash that contains various spellings of the multiplier as the key and the numerical value as the value? No eval necessary and no regexs either!
首先,将正则表达式更改为
@regex = /([0-9]+) (.*?)s?/
将解决复数问题。这 ?表示匹配“s”的 0 或 1 个字符,并导致 .* 以非贪婪方式匹配(尽可能少的字符)。至于大小,你可以有一个像这样的散列:
然后你的计算只是
self.quota = m[1].to_i*@hash[m2]
编辑:将值更改为基数 2
First of all, changing your regex to
@regex = /([0-9]+) (.*?)s?/
will fix the plural issue. The ? says match either 0 or 1 characters for the 's' and it causes .* to match in a non-greedy manner (as few characters as possible).As for the size, you could have a hash like this:
and then your calculation is just
self.quota = m[1].to_i*@hash[m2]
EDIT: Changed values to base 2