gpt4 book ai didi

ruby-on-rails - 载波 gem 。重新创建后如何重命名上传的图像版本?

转载 作者:行者123 更新时间:2023-12-04 01:10:05 26 4
gpt4 key购买 nike

我有 RailsCasts 中描述的类似模型:

应用程序/模型/resident.rb:

class Resident < ActiveRecord::Base
include PhotoConcern
end

应用程序/模型/employee.rb:
class Employee < ActiveRecord::Base
include PhotoConcern
end

应用程序/模型/关注/photo_concern.rb:
module PhotoConcern
extend ActiveSupport::Concern

included do
mount_uploader :photo, PhotoUploader

attr_accessor :photo_crop_x, :photo_crop_y, :photo_crop_w, :photo_crop_h

after_save :crop_photo

def crop_photo
photo.recreate_versions! if photo_crop_x.present?
end
end
end

应用程序/上传器/photo_uploader.rb:
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick

storage :file

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

version :cropped do
process :crop
end

version :thumb, from_version: :cropped do
process resize_to_fill: [100, 100]
end

version :avatar, from_version: :cropped do
process resize_to_fill: [200, 200]
end

def crop
return if model.photo_crop_x.blank?

resize_to_limit(500, nil)
resize_to_fit(500, nil)

manipulate! do |img|
size = model.photo_crop_w << 'x' << model.photo_crop_h
offset = '+' << model.photo_crop_x << '+' << model.photo_crop_y

img.crop("#{size}#{offset}")
img
end
end
end

应用程序/ View /员工/show.slim
= image_tag (@employee.photo.present? ? @employee.photo.url(:avatar) : "client_#{@employee.sex}.png"), class: 'img-circle img-responsive'

我想在裁剪后重命名版本文件,这样我的用户就不会为缓存而苦恼。 CarrierWave wiki 中描述了如何重命名文件以及 it's written “为了保存新生成的文件名,您必须在 recreate_versions 后调用模型上的 save!!”。

如何重命名版本文件?我打不通 save!在我的员工的 after_save再次因为有更多的钩子(Hook)不应该被调用两次。此外,PhotoConcern 包含在另一个类中。

相关维基文章:
  • How to: Create random and unique filenames for all versioned files
  • How to: Customize your version file names
  • How to: Use a timestamp in file names
  • 最佳答案

    In order to save the newly generated filename you have to call save! on the model after recreate_versions!.



    所以我相信你的疑惑的答案包含在 Carrierwave rubydocumentation中。

    recreate_versions!(*versions) ⇒ Object

    Recreate versions and reprocess them. This can be used to recreate versions if their parameters somehow have changed.



    此方法将存储 *versions如果没有省略,则 cached file将被存储。
    # File 'lib/carrierwave/uploader/versions.rb', line 216

    def recreate_versions!(*versions)
    # Some files could possibly not be stored on the local disk. This
    # doesn't play nicely with processing. Make sure that we're only
    # processing a cached file
    #
    # The call to store! will trigger the necessary callbacks to both
    # process this version and all sub-versions
    if versions.any?
    file = sanitized_file if !cached?
    # the file will be stored
    store_versions!(file, versions)
    else
    cache! if !cached?
    # If new_file is omitted, a previously cached file will be stored.
    store!
    end

    store! 是什么意思做?

    这是 the rubydoc page about store!

    store!(new_file = nil) ⇒ Object

    Stores the file by passing it to this Uploader's storage engine. If new_file is omitted, a previously cached file will be stored



    此方法包含在您的 class PhotoUploader < CarrierWave::Uploader::Base 中。它使用 with_callbacks使用回调 :store 存储您的文件.回调触发此方法。
    # File 'lib/carrierwave/uploader/store.rb', line 53

    def store!(new_file=nil)
    cache!(new_file) if new_file && ((@cache_id != parent_cache_id) || @cache_id.nil?)
    if !cache_only and @file and @cache_id
    with_callbacks(:store, new_file) do
    new_file = storage.store!(@file)
    if delete_tmp_file_after_storage
    @file.delete unless move_to_store
    cache_storage.delete_dir!(cache_path(nil))
    end
    @file = new_file
    @cache_id = nil
    end
    end
    end
    store_versions! 是什么意思方法呢?
    def store_versions!(new_file, versions=nil)
    if versions
    active = Hash[active_versions]
    versions.each { |v| active[v].try(:store!, new_file) } unless active.empty?
    else
    active_versions.each { |name, v| v.store!(new_file) }
    end
    end

    什么是 Carrierwave 回调以及如何使用它们?
     after :store, :store_versions!

    这个问题在 SO explainswiki通过 after :store, :my_method 解释回调是如何工作的在您的 version :low do 内 block ,您将执行 my_method仅在 after :store 上回调(仅适用于该版本)。
    :store回调对应 store!的执行.

    什么是 @filename属性? recreate_versions!如何编码文件名?
    @filename用方法 filename 定义在 lib/carrierwave/uploader/store.rb
    ##
    # Override this in your Uploader to change the filename.
    #
    # Be careful using record ids as filenames. If the filename is stored in the database
    # the record id will be nil when the filename is set. Don't use record ids unless you
    # understand this limitation.
    #
    # Do not use the version_name in the filename, as it will prevent versions from being
    # loaded correctly.
    #
    # === Returns
    #
    # [String] a filename
    #
    def filename
    @filename
    end

    来自carrierwave的指南建议使用 def filename在使用 recreate_version! 重新创建版本时重新创建唯一的文件名.

    该方法不保存到数据库,保存到数据库需要调用 save!关于拨款 Carrierwave回调,不会打扰您 Carrierwave gem

    我没有这个问题的解决方案,但是没有关于这个的文档,我们应该开始构建它。

    关于ruby-on-rails - 载波 gem 。重新创建后如何重命名上传的图像版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127126/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com