Ruby on rails 4 Rails 4回形针背景作业

Ruby on rails 4 Rails 4回形针背景作业,ruby-on-rails-4,upload,paperclip,Ruby On Rails 4,Upload,Paperclip,当前逻辑 首先,我想我已经读了所有关于回形针的文章,尽管我学到了所有的信息,但我仍然感到口吃。。。所以我可以说你的帮助真的很宝贵 其次,我不使用延迟回形针,也不使用s3\U直接上传(但使用jquery directUpload) 我有4个不同图片的用户档案:logo,avatar,worka,workb 每种格式(logo、avatar、worka、workb)有3种样式(:small、:thumb、:medium) 用户可以更新其图片=>因此需要考虑更新操作 4个文件字段与其他经典字段(名称

当前逻辑

首先,我想我已经读了所有关于回形针的文章,尽管我学到了所有的信息,但我仍然感到口吃。。。所以我可以说你的帮助真的很宝贵

其次,我不使用延迟回形针,也不使用s3\U直接上传(但使用jquery directUpload)

我有4个不同图片的用户档案:logo,avatar,worka,workb

  • 每种格式(logo、avatar、worka、workb)有3种样式(:small、:thumb、:medium)
  • 用户可以更新其图片=>因此需要考虑更新操作
  • 4个文件字段与其他经典字段(名称、电子邮件等)的格式相同
  • 上传由jQuery DirectUpload+曲别针管理
当用户单击文件字段以添加图像时:

  • jQuery DirectUpload将文件上载到s3上的临时目录中
  • 带有url(键)的jQuery回调
  • url被分配为:temp到javascript中生成的隐藏字段
按下表单提交按钮时:

  • 在包含url(键)的
    @user.logo.temp
    的帮助下,我将通过直接上载上传的文件的url分配给曲别针
  • 回形针生成所有样式
我的表格

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), method: :put, html: { class: "form-horizontal directUpload", role: "form" }) do |f| %>
    <%= f.fields_for :logo do |l| %>
      <%=   l.file_field(:attachment, accept: 'image/gif,image/png,image/jpg,image/jpeg') %>
    <% end %>
    <input type="hidden" name="user[logo_attributes][temp]" value="https://bucketname.s3.amazonaws.com/temp/e4b46d01-5d69-483b.jpg">
  <% end %>

class Logo < Document
  S3_TEMP_URL_FORMAT = %r{\/\/bucketname\.s3\.amazonaws\.com\/(?<path>temp\/.+\/(?<filename>.+))\z}.freeze

  has_attached_file :attachment,
  styles: { medium: "300x300#" },
  convert_options: { medium: "-quality 75 -strip" },
  default_url: ":parent_type/:class/:style/missing.png",
  path: "/documents/:parent_type/:id_partition/:class/:style/:basename.:extension"

  validates_attachment :attachment,
  content_type: { content_type: ["image/gif", "image/png", "image/jpg", "image/jpeg"] },
  size: { less_than: 1.megabyte }

  validates :temp, 
  # presence: true,
  format: { with: S3_TEMP_URL_FORMAT }

  before_save :set_attachment
  after_save :set_remote_url
  before_post_process :stop_process

  def stop_process
    false
  end

  def styles_process
    self.attachment.assign(attachment)
    self.save
  end

  def set_attachment
    # puts "BEGIN -- SET ATTACHMENT"
    tries ||= 5
    s3_temp_url_data = S3_TEMP_URL_FORMAT.match(self.temp)
    s3 = AWS::S3.new
    s3_temp_head = s3.buckets[ENV['S3_BUCKET']].objects[s3_temp_url_data[:path]].head
    self.attachment_file_name = s3_temp_url_data[:filename]
    self.attachment_file_size = s3_temp_head.content_length 
    self.attachment_content_type = s3_temp_head.content_type
    self.attachment_updated_at = s3_temp_head.last_modified 
  rescue AWS::S3::Errors::NoSuchKey => e
    tries -= 1
    if tries > 0
      sleep(3)
      retry
    else
      false
    end
  end

  def set_remote_url    
    s3_temp_url_data = S3_TEMP_URL_FORMAT.match(self.temp)
    s3 = AWS::S3.new
    self.attachment = URI.parse(self.temp)
    self.save
    s3.buckets[ENV['S3_BUCKET']].objects.with_prefix(s3_temp_url_data[:path]).delete_all
  end
end
def update
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      # Here is the styles processing
      # This is where the Resque background job would go
      @user.logo.styles_process
      set_flash_message :notice, :updated
      redirect_to after_update_path_for(@user)
    else
      render :edit
    end
end
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), method: :put, html: { class: "form-horizontal directUpload", role: "form" }) do |f| %>
    <%= f.fields_for :logo do |l| %>
      <%=   l.file_field(:attachment, accept: 'image/gif,image/png,image/jpg,image/jpeg') %>
    <% end %>
    <input type="hidden" name="user[logo_attributes][temp]" value="https://bucketname.s3.amazonaws.com/temp/e4b46d01-5d69-483b.jpg">
  <% end %>