在 Rails 中将文件大小字符串转换为千字节等效值

发布于 2024-09-07 13:14:01 字数 956 浏览 6 评论 0原文

我的目标是转换表单输入,例如“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不甘平庸 2024-09-14 13:14:01

您可以使用 Rails ActiveHelper number_to_ human_size

You can use Rails ActiveHelper number_to_human_size.

汹涌人海 2024-09-14 13:14:01
def quota_convert
  @regex = /([0-9]+) (.*)s?/
  @sizes = "kilobytes megabytes gigabytes"
  m = self.quota.match(@regex)
  if @sizes.include? m[2]
    m[1].to_f.send(m[2])
  end
end
  • 在正则表达式中添加了 ? 作为可选复数。
  • 将@sizes 更改为复数字符串。
  • 将 m[1](数字转换为浮点数)。
  • 直接发送消息m[2]
def quota_convert
  @regex = /([0-9]+) (.*)s?/
  @sizes = "kilobytes megabytes gigabytes"
  m = self.quota.match(@regex)
  if @sizes.include? m[2]
    m[1].to_f.send(m[2])
  end
end
  • Added ? for optional plural in the regex.
  • Changed @sizes to a string of plurals.
  • Convert m[1] (the number to a float).
  • Send the message m[2] directly
只涨不跌 2024-09-14 13:14:01

为什么不简单地创建一个散列,其中包含乘数的各种拼写作为键和数值作为值?无需评估,也无需正则表达式!

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!

趁年轻赶紧闹 2024-09-14 13:14:01

首先,将正则表达式更改为 @regex = /([0-9]+) (.*?)s?/ 将解决复数问题。这 ?表示匹配“s”的 0 或 1 个字符,并导致 .* 以非贪婪方式匹配(尽可能少的字符)。

至于大小,你可以有一个像这样的散列:

@hash = { 'kilobyte' => 1, 'megabyte' => 1024, 'gigabyte' => 1024*1024}

然后你的计算只是 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:

@hash = { 'kilobyte' => 1, 'megabyte' => 1024, 'gigabyte' => 1024*1024}

and then your calculation is just self.quota = m[1].to_i*@hash[m2]

EDIT: Changed values to base 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文