You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

avatar_controller.rb 2.8 kB

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. class AvatarController < ApplicationController
  2. #before_filter :set_cache_buster
  3. include AvatarHelper
  4. def upload
  5. # Make sure that API users get used to set this content type
  6. # as it won't trigger Rails' automatic parsing of the request body for parameters
  7. unless request.content_type == 'application/octet-stream'
  8. render :nothing => true, :status => 406
  9. return
  10. end
  11. unless request.raw_post.nil?
  12. @source_type = params[:source_type]
  13. @source_id = params[:source_id]
  14. @temp_file = request.raw_post
  15. if @temp_file.size > 0
  16. if @temp_file.respond_to?(:original_filename)
  17. @image_file = @temp_file.original_filename
  18. #image_file.force_encoding("UTF-8") if filename.respond_to?(:force_encoding)
  19. else
  20. @image_file=params[:filename]
  21. end
  22. end
  23. end
  24. if @temp_file && (@temp_file.size > 0)
  25. diskfile=disk_filename(@source_type,@source_id)
  26. @urlfile='/' << File.join("images","avatars",avatar_directory(@source_type),avatar_filename(@source_id,@image_file))
  27. logger.info("Saving avatar '#{diskfile}' (#{@temp_file.size} bytes)")
  28. path = File.dirname(diskfile)
  29. unless File.directory?(path)
  30. FileUtils.mkdir_p(path)
  31. end
  32. md5 = Digest::MD5.new
  33. File.open(diskfile, "wb") do |f|
  34. if @temp_file.respond_to?(:read)
  35. buffer = ""
  36. while (buffer = @temp_file.read(8192))
  37. f.write(buffer)
  38. md5.update(buffer)
  39. end
  40. else
  41. f.write(@temp_file)
  42. md5.update(@temp_file)
  43. end
  44. end
  45. # self.digest = md5.hexdigest
  46. end
  47. @temp_file = nil
  48. # @avatar = Avatar.new(:receive_file => request.raw_post)
  49. # @avatar.source_id = User.current.id
  50. # @avatar.image_file = params[:filename].presence || Redmine::Utils.random_hex(16)
  51. # saved = @avatar.save
  52. begin
  53. f = Magick::ImageList.new(diskfile)
  54. width = 300.0;
  55. proportion = (width/f[0].columns)
  56. height = (f[0].rows*proportion)
  57. f.resize_to_fill!(width,height)
  58. # f.scale!(width,height)
  59. f.write(diskfile)
  60. rescue Exception => e
  61. logger.error "[Error] avatar : avatar_controller#upload ===> #{e}"
  62. end
  63. respond_to do |format|
  64. format.js
  65. format.api {
  66. if saved
  67. render :action => 'upload', :status => :created
  68. else
  69. render_validation_errors(@avatar)
  70. end
  71. }
  72. end
  73. end
  74. private
  75. def set_cache_buster
  76. response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
  77. response.headers["Pragma"] = "no-cache"
  78. response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
  79. end
  80. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...