picture.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. class Ckeditor::Picture < Ckeditor::Asset
  2. before_create :before_create
  3. has_attached_file :data,{
  4. storage: :aliyun,
  5. # path: 'ckeditor/detail/:id/:hash.:extension',
  6. path: 'ckeditor/detail/:id/:style.:filename',
  7. url: ':aliyun_upload_url',
  8. styles: { content: '780>', thumb: '100>'}
  9. # hash_secret:'3c6acb54d3c7b788d853304142d669a9de78780ee672226009a35192d00d1dbc34f39e81f0f7f5248ce5f8e69c6e26d680bd0af9e8998b40333052ae7159d26e'
  10. }
  11. validates_attachment_presence :data
  12. validates_attachment_size :data, less_than: 2.megabytes
  13. validates_attachment_content_type :data, content_type: /\Aimage/
  14. #解决中文文件名不能正常上传问题,上传之前重命名
  15. def before_create
  16. extension = File.extname(filename).downcase
  17. extension = ".png" if extension.blank?
  18. self.data.instance_write(:file_name, "#{Time.now.strftime("%Y%m%d%H%M%S")}#{rand(1000)}#{extension}")
  19. end
  20. def url_content
  21. url(:content)
  22. end
  23. rails_admin do
  24. weight -20
  25. end
  26. end