ruby-on-rails 使用Ruby on Rails的回形针将文件名Map到路径

4bbkushb  于 5个月前  发布在  Ruby
关注(0)|答案(1)|浏览(80)

我对Ruby on Rails一无所知,但我参与了重写一个应用程序的工作,这个应用程序是别人不久前用它开发的。(C/C++等),我很难理解Ruby on Rails。我想我不能理解它的原因是因为它的细节隐藏在一个library(paperclip).
短版:我想将数据库列中的文件名与文件在磁盘上的物理位置进行匹配。)
以我有限的知识,我知道以下几点...
该应用程序有一个Gemfile.gem与此条目:'回形针','4.3.0'.我看了paperclip的开发history,并意识到这是从四月2016.所以,它似乎很老.
我有一个PostgreSQL数据库,其字段如下:

  • 附件文件名
  • 附件内容类型
  • 附件文件大小
  • 附件更新于

所有这些都与文档中提到的变量相匹配。
数据库中attachment_file_name下的值只是文件名,没有路径。
在磁盘上,这些文件存储在像attachments/000/003/335/original/abc.txt这样的目录中,看起来和文档中的例子完全一样。所以,我想我相当确定正在使用paperclip
如何将文件名转换为上述路径?
更重要的是,如果我有一组几百个文件,我想打印出每个文件的完整路径。文件名中有重复的文件,导致不同的路径。所以我猜计算(哈希函数?)是使用文件名和文件的内容来完成的。
有人能提供关于如何做到这一点和/或我应该在代码中寻找什么的建议吗?
我想我的问题类似于存储库中的Issue,它将该问题的作者指向StackOverflow(当然,那是7年前的事了)。
谢谢!任何帮助将不胜感激!

vyswwuz2

vyswwuz21#

用一个例子更容易:

class User < ActiveRecord::Base
  has_attached_file :attachment

  validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
end

字符串
附加文件:

>> User.create!(attachment: File.open("avatar.png"))
  TRANSACTION (0.1ms)  begin transaction
  User Create (0.3ms)  INSERT INTO "users" ("attachment_file_name", "attachment_content_type", "attachment_file_size", "attachment_updated_at") VALUES (?, ?, ?, ?)  [["attachment_file_name", "avatar.png"], ["attachment_content_type", "image/png"], ["attachment_file_size", 8229], ["attachment_updated_at", "2023-12-01 05:14:12.369915"]]
  TRANSACTION (5.4ms)  commit transaction
=> #<User id: 1, attachment_file_name: "avatar.png", attachment_content_type: "image/png", attachment_file_size: 8229, attachment_updated_at: "2023-12-01 05:14:12.369915603 +0000">


这是存储文件的默认路径:

>> Paperclip::Attachment.default_options[:url]
=> "/system/:class/:attachment/:id_partition/:style/:filename"

x

$ ls public/system/**/*.png
public/system/users/attachments/000/000/001/original/avatar.png
public/
system/
users/       - :class        # `User.name.underscore.pluralize`
                             # https://github.com/thoughtbot/paperclip/blob/main/lib/paperclip/interpolations.rb#L88

attachments/ - :attachment   # pluralized attachment name
                             #                     vvvvvvvvvv
                             # `has_attached_file :attachment`
                             # https://github.com/thoughtbot/paperclip/blob/main/lib/paperclip/interpolations.rb#L191

000/000/001/ - :id_partition # id padded with zeros and split in 3 groups (9 digits total)
                             # https://github.com/thoughtbot/paperclip/blob/main/lib/paperclip/interpolations.rb#L174

original/    - :style        # "original" is default, this is what you've uploaded
                             # https://github.com/thoughtbot/paperclip/blob/main/lib/paperclip/interpolations.rb#L197

avatar.png   - :filename

的一种或多种
如果您只能从数据库端工作

sqlite> select * from users;
+----+----------------------+-------------------------+----------------------+----------------------------+
| id | attachment_file_name | attachment_content_type | attachment_file_size |   attachment_updated_at    |
+----+----------------------+-------------------------+----------------------+----------------------------+
| 1  | avatar.png           | image/png               | 8229                 | 2023-12-01 05:14:12.369915 |
+----+----------------------+-------------------------+----------------------+----------------------------+
public/
system/
users/       - :class        # most likely the same as table name

attachments/ - :attachment   # attachment_file_name
                             # ^^^^^^^^^^s

000/000/001/ - :id_partition # padded id - you'll need to work that one out

original/    - :style        # "original" - this is not in the db

avatar.png   - :filename     # attachment_file_name

的字符串

相关问题