class Ckeditor::Picture < Ckeditor::Asset
  before_create :before_create

  has_attached_file :data,{
    storage: :aliyun,    
    # path: 'ckeditor/detail/:id/:hash.:extension',
    path: 'ckeditor/detail/:id/:style.:filename',
    url: ':aliyun_upload_url',
    styles: { content: '780>', thumb: '100>'}    
    # hash_secret:'3c6acb54d3c7b788d853304142d669a9de78780ee672226009a35192d00d1dbc34f39e81f0f7f5248ce5f8e69c6e26d680bd0af9e8998b40333052ae7159d26e'
  }

  validates_attachment_presence :data
  validates_attachment_size :data, less_than: 2.megabytes
  validates_attachment_content_type :data, content_type: /\Aimage/

  #解决中文文件名不能正常上传问题，上传之前重命名
  def before_create    
    extension = File.extname(filename).downcase      
    extension = ".png" if extension.blank?
    self.data.instance_write(:file_name, "#{Time.now.strftime("%Y%m%d%H%M%S")}#{rand(1000)}#{extension}")          
  end 

  def url_content    
    url(:content)
  end

  rails_admin do
    weight -20
  end
end
