【问题标题】:Patch Blob model in Active StorageActive Storage 中的补丁 Blob 模型
【发布时间】:2020-09-28 19:31:19
【问题描述】:

我想修补 Rails 6.0 以包含此 PR 的一部分:https://github.com/rails/rails/commit/4dba136c83cc808282625c0d5b195ce5e0bbaa68 我只使用直接上传,所以我现在只修补create_before_direct_upload!。这是我尝试过的:

  1. 在initializers/active_storage.rb
module BlobOverride
  class << self
    def create_before_direct_upload!(key: nil, filename:, byte_size:, checksum:, content_type: nil, metadata: nil)
      puts "In Blob Override Patch"
      byebug
      create! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata
    end
  end
end

 ActiveStorage::Blob.prepend(BlobOverride)

这会返回 undefined methodhas_one_attached'` 错误,我在这里找到了一个 github 问题:https://github.com/rails/rails/issues/38876 这基本上是说您不能使用从初始化程序加载模型。
2。然后我尝试以这种方式加载模块:

ActiveSupport.on_load(:active_storage_blob) do
  ActiveStorage::Blob.prepend(BlobOverride)
end

我没有收到错误,但我的补丁没有被击中。
3. 我试过这个:

Rails.application.config.to_prepare do
  require 'active_storage/blob'
  ActiveStorage::Blob.class_eval do
    def create_before_direct_upload!(key: nil, filename:, byte_size:, checksum:, content_type: nil, metadata: nil)
      puts "in create before direct upload patch"
      byebug
      create! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata
    end
  end
end

没有错误,未命中修补方法。

TLDR

如何修补活动存储上的 blob 模型以支持自定义密钥?由于某种原因,标准的猴子补丁无法正常工作。

【问题讨论】:

  • 运气好吗?我正在尝试做同样的事情。

标签: ruby-on-rails rails-activestorage


【解决方案1】:

这适用于 Rails 6.1:

ActiveRecord 现在有一个名为has_secure_token 的东西,ActiveStorage 在 Blob 上使用它。这个has_secure_token 正在使用以下代码设置before_create:

before_create { send("#{attribute}=", self.class.generate_unique_secure_token(length: length)) unless send("#{attribute}?") }

这又将self[:key] 设置为安全令牌

当它被这样调用时,self[:key] 方法只会在它不存在时设置它

ActiveStorage::Blob.class_eval do
  def key
    self[:key] ||= 'your custom key'
  end
end

我通过在config/application.rb 中执行以下操作来解决此问题

class Application < Rails::Application
  # ...
  # ...
  # ...

  Rails.application.config.to_prepare do
    require 'active_storage/blob'
    class ActiveStorage::Blob
      def self.generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
        gust = SecureRandom.base36(length)
        epoch = Time.now.to_i.to_s
        checksum = Digest::MD5.hexdigest(gust + epoch)

        folder = checksum.chars.first(9).each_slice(3).to_a.map(&:join).join('/')

        "#{folder}/#{gust}"
      end
    end

    # Rails 5 doesn't have secure_token for Blob, so this will monkey patch it
    ActiveStorage::Blob.class_eval do
      def key
        self[:key] ||= generate_key_with_folder
      end

      def generate_key_with_folder
        gust = self.class.generate_unique_secure_token
        epoch = Time.now.to_i.to_s
        checksum = Digest::MD5.hexdigest(gust + epoch)

        folder = checksum.chars.first(9).each_slice(3).to_a.map(&:join).join('/')

        "#{folder}/#{gust}"
      end
    end
  end
end

【讨论】:

    【解决方案2】:

    感谢@Arian 的answer,这是我在 Rails 5 应用程序中的实现。

    我用下面的代码创建了一个初始化器config/initializers/active_storage_custom_key.rb

    Rails.application.config.to_prepare do
      require 'active_storage/blob'
      ActiveStorage::Blob.class_eval do
        def key
          self[:key] ||= File.join(self.class.generate_unique_secure_token, filename.to_s)
        end
      end
    end
    

    这会生成格式为SZJLX7fLpsv1dzzj2PhRg7ve/my_filename.jpg的密钥

    【讨论】:

    • require 'active_storage/blob' 可以被移除。我正在使用 Rails7。
    猜你喜欢
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多